Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions izidic.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,15 @@ func (dic *container) Service(name string) (any, error) {
// for at least one service was attempted more than once, which implies a
// dependency cycle.
const funcName = "github.com/fgm/izidic.(*container).Service"
// We need a vastly oversized value to cover the case of deeply nested dic.Service() calls.
pcs := make([]uintptr, 1e6)
n := runtime.Callers(1, pcs)
pcs = pcs[:n]
pcs := make([]uintptr, 64)
for {
n := runtime.Callers(1, pcs)
if n < len(pcs) {
pcs = pcs[:n]
break
}
pcs = make([]uintptr, 2*len(pcs))
}
frames := runtime.CallersFrames(pcs)
serviceCalls := 0
for {
Expand Down
23 changes: 23 additions & 0 deletions izidic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package izidic_test
import (
"errors"
"fmt"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -98,6 +99,28 @@ func TestContainer_Service(t *testing.T) {
}
}

func TestContainer_Service_AvoidsOversizedCycleDetectorAllocation(t *testing.T) {
const maxAllocatedBytes = 1 << 20

runtime.GC()
var before runtime.MemStats
runtime.ReadMemStats(&before)

dic := izidic.New()
dic.Register("s", s1)
_, err := dic.Service("s")
if err != nil {
t.Fatal(err)
}

var after runtime.MemStats
runtime.ReadMemStats(&after)
allocatedBytes := after.TotalAlloc - before.TotalAlloc
if allocatedBytes > maxAllocatedBytes {
t.Fatalf("Service allocated %d bytes, expected at most %d", allocatedBytes, maxAllocatedBytes)
}
}

func TestContainer_MustService_Missing(t *testing.T) {
const expectedFormat = "service not found: %q"
defer func() {
Expand Down
Loading