diff --git a/pkg/refs/refcounter.go b/pkg/refs/refcounter.go index 1bbcae045..e906e838f 100644 --- a/pkg/refs/refcounter.go +++ b/pkg/refs/refcounter.go @@ -238,6 +238,9 @@ const ( // LeaksLogTraces indicates that a trace collected during allocation // should be logged when leaks are found. LeaksLogTraces + + // LeaksPanic indidcates that a panic should be issued when leaks are found. + LeaksPanic ) // Set implements flag.Value. @@ -249,6 +252,8 @@ func (l *LeakMode) Set(v string) error { *l = LeaksLogWarning case "log-traces": *l = LeaksLogTraces + case "panic": + *l = LeaksPanic default: return fmt.Errorf("invalid ref leak mode %q", v) } @@ -271,8 +276,11 @@ func (l LeakMode) String() string { return "log-names" case LeaksLogTraces: return "log-traces" + case LeaksPanic: + return "panic" + default: + panic(fmt.Sprintf("invalid ref leak mode %d", l)) } - panic(fmt.Sprintf("invalid ref leak mode %d", l)) } // leakMode stores the current mode for the reference leak checker. diff --git a/pkg/refsvfs2/refs_map.go b/pkg/refsvfs2/refs_map.go index fb8984dd6..a1146a344 100644 --- a/pkg/refsvfs2/refs_map.go +++ b/pkg/refsvfs2/refs_map.go @@ -53,6 +53,12 @@ func leakCheckEnabled() bool { return refs_vfs1.GetLeakMode() != refs_vfs1.NoLeakChecking } +// leakCheckPanicEnabled returns whether DoLeakCheck() should panic when leaks +// are detected. +func leakCheckPanicEnabled() bool { + return refs_vfs1.GetLeakMode() == refs_vfs1.LeaksPanic +} + // Register adds obj to the live object map. func Register(obj CheckedObject) { if leakCheckEnabled() { @@ -131,6 +137,9 @@ func DoLeakCheck() { for obj := range liveObjects { msg += obj.LeakMessage() + "\n" } + if leakCheckPanicEnabled() { + panic(msg) + } log.Warningf(msg) } })