]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use runtime.AddCleanup instead of runtime.SetFinalizer
authorCarlos Amedee <carlos@golang.org>
Mon, 6 Jan 2025 18:15:51 +0000 (13:15 -0500)
committerCarlos Amedee <carlos@golang.org>
Fri, 14 Feb 2025 15:29:47 +0000 (07:29 -0800)
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>

src/cmd/go/internal/base/limit.go
src/cmd/go/internal/lockedfile/lockedfile.go

index b4160bde021c0d5a2ecb9d14a4cbc45599304602..4317432527a433f112314e19040898b49b2398b0 100644 (file)
@@ -52,7 +52,7 @@ func AcquireNet() (release func(), err error) {
        }
 
        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() {
                if checker.released {
@@ -62,7 +62,7 @@ func AcquireNet() (release func(), err error) {
                if hasToken {
                        <-netLimitSem
                }
-               runtime.SetFinalizer(checker, nil)
+               cleanup.Stop()
        }, nil
 }
 
@@ -78,7 +78,3 @@ type netTokenChecker struct {
        // “tiny allocator”.
        unusedAvoidTinyAllocator string
 }
-
-func (c *netTokenChecker) panicUnreleased() {
-       panic("internal error: net token acquired but not released")
-}
index 82e1a89675e7042aab6429990491e0e6f4ab6219..8bd2ffbe8f7860c1f5d49392fc9c0518a5b1feda 100644 (file)
@@ -24,6 +24,8 @@ import (
 type File struct {
        osFile
        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.
@@ -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
        // 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
-       // 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.
-       runtime.SetFinalizer(f, func(f *File) {
-               panic(fmt.Sprintf("lockedfile.File %s became unreachable without a call to Close", f.Name()))
-       })
+       f.cleanup = runtime.AddCleanup(f, func(fileName string) {
+               panic(fmt.Sprintf("lockedfile.File %s became unreachable without a call to Close", fileName))
+       }, f.Name())
 
        return f, nil
 }
@@ -91,7 +93,7 @@ func (f *File) Close() error {
        f.closed = true
 
        err := closeFile(f.osFile.File)
-       runtime.SetFinalizer(f, nil)
+       f.cleanup.Stop()
        return err
 }