cmd/go: use runtime.AddCleanup instead of runtime.SetFinalizer

Replace the usage of runtime.SetFinalizer with runtime.AddCleanup.
This changes a test and how when the Go command panics when a file is
left locked.

Updates #70907

Change-Id: I8d8c56d16486728f9bd4b910b81796ae506bda74
Reviewed-on: https://go-review.googlesource.com/c/go/+/640736
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Carlos Amedee 2025-01-06 13:15:51 -05:00
parent 954e2c0b06
commit a8487dadeb
2 changed files with 9 additions and 11 deletions

View file

@ -52,7 +52,7 @@ func AcquireNet() (release func(), err error) {
} }
checker := new(netTokenChecker) checker := new(netTokenChecker)
runtime.SetFinalizer(checker, (*netTokenChecker).panicUnreleased) cleanup := runtime.AddCleanup(checker, func(_ int) { panic("internal error: net token acquired but not released") }, 0)
return func() { return func() {
if checker.released { if checker.released {
@ -62,7 +62,7 @@ func AcquireNet() (release func(), err error) {
if hasToken { if hasToken {
<-netLimitSem <-netLimitSem
} }
runtime.SetFinalizer(checker, nil) cleanup.Stop()
}, nil }, nil
} }
@ -78,7 +78,3 @@ type netTokenChecker struct {
// “tiny allocator”. // “tiny allocator”.
unusedAvoidTinyAllocator string unusedAvoidTinyAllocator string
} }
func (c *netTokenChecker) panicUnreleased() {
panic("internal error: net token acquired but not released")
}

View file

@ -24,6 +24,8 @@ import (
type File struct { type File struct {
osFile osFile
closed bool closed bool
// cleanup panics when the file is no longer referenced and it has not been closed.
cleanup runtime.Cleanup
} }
// osFile embeds a *os.File while keeping the pointer itself unexported. // osFile embeds a *os.File while keeping the pointer itself unexported.
@ -48,11 +50,11 @@ func OpenFile(name string, flag int, perm fs.FileMode) (*File, error) {
// Although the operating system will drop locks for open files when the go // Although the operating system will drop locks for open files when the go
// command exits, we want to hold locks for as little time as possible, and we // command exits, we want to hold locks for as little time as possible, and we
// especially don't want to leave a file locked after we're done with it. Our // especially don't want to leave a file locked after we're done with it. Our
// Close method is what releases the locks, so use a finalizer to report // Close method is what releases the locks, so use a cleanup to report
// missing Close calls on a best-effort basis. // missing Close calls on a best-effort basis.
runtime.SetFinalizer(f, func(f *File) { f.cleanup = runtime.AddCleanup(f, func(fileName string) {
panic(fmt.Sprintf("lockedfile.File %s became unreachable without a call to Close", f.Name())) panic(fmt.Sprintf("lockedfile.File %s became unreachable without a call to Close", fileName))
}) }, f.Name())
return f, nil return f, nil
} }
@ -91,7 +93,7 @@ func (f *File) Close() error {
f.closed = true f.closed = true
err := closeFile(f.osFile.File) err := closeFile(f.osFile.File)
runtime.SetFinalizer(f, nil) f.cleanup.Stop()
return err return err
} }