github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/internal/testutils/fdtrace/fd.go (about) 1 package fdtrace 2 3 import ( 4 "fmt" 5 "os" 6 "runtime" 7 "testing" 8 9 "github.com/cilium/ebpf/internal/sys" 10 ) 11 12 // TestMain runs m with sys.FD leak tracing enabled. 13 func TestMain(m *testing.M) { 14 // fn can either be invoked asynchronously by the gc or during disabling of 15 // the leak tracer below. Don't terminate the program immediately, instead 16 // capture a boolean that will be used to set the exit code. This avoids races 17 // and gives all events the chance to be written to stderr. 18 var leak bool 19 sys.OnLeakFD(func(fs *runtime.Frames) { 20 fmt.Fprintln(os.Stderr, "leaked fd created at:") 21 fmt.Fprintln(os.Stderr, sys.FormatFrames(fs)) 22 leak = true 23 }) 24 25 ret := m.Run() 26 27 sys.OnLeakFD(nil) 28 29 if leak { 30 ret = 99 31 } 32 33 os.Exit(ret) 34 }