Add a panic mode to refs and refsvfs2.

This mode causes a panic rather than just a warning
when leaks are detected during a call to DoLeakCheck().

PiperOrigin-RevId: 421885173
This commit is contained in:
Lucas Manning
2022-01-14 12:19:49 -08:00
committed by gVisor bot
parent 3cf606d9a4
commit b5355d8bf9
2 changed files with 18 additions and 1 deletions
+9 -1
View File
@@ -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.
+9
View File
@@ -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)
}
})