Skip to content

Commit a8487da

Browse files
committed
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 954e2c0 commit a8487da

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func AcquireNet() (release func(), err error) {
5252
}
5353

5454
checker := new(netTokenChecker)
55-
runtime.SetFinalizer(checker, (*netTokenChecker).panicUnreleased)
55+
cleanup := runtime.AddCleanup(checker, func(_ int) { panic("internal error: net token acquired but not released") }, 0)
5656

5757
return func() {
5858
if checker.released {
@@ -62,7 +62,7 @@ func AcquireNet() (release func(), err error) {
6262
if hasToken {
6363
<-netLimitSem
6464
}
65-
runtime.SetFinalizer(checker, nil)
65+
cleanup.Stop()
6666
}, nil
6767
}
6868

@@ -78,7 +78,3 @@ type netTokenChecker struct {
7878
// “tiny allocator”.
7979
unusedAvoidTinyAllocator string
8080
}
81-
82-
func (c *netTokenChecker) panicUnreleased() {
83-
panic("internal error: net token acquired but not released")
84-
}

src/cmd/go/internal/lockedfile/lockedfile.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
type File struct {
2525
osFile
2626
closed bool
27+
// cleanup panics when the file is no longer referenced and it has not been closed.
28+
cleanup runtime.Cleanup
2729
}
2830

2931
// 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) {
4850
// Although the operating system will drop locks for open files when the go
4951
// command exits, we want to hold locks for as little time as possible, and we
5052
// especially don't want to leave a file locked after we're done with it. Our
51-
// Close method is what releases the locks, so use a finalizer to report
53+
// Close method is what releases the locks, so use a cleanup to report
5254
// missing Close calls on a best-effort basis.
53-
runtime.SetFinalizer(f, func(f *File) {
54-
panic(fmt.Sprintf("lockedfile.File %s became unreachable without a call to Close", f.Name()))
55-
})
55+
f.cleanup = runtime.AddCleanup(f, func(fileName string) {
56+
panic(fmt.Sprintf("lockedfile.File %s became unreachable without a call to Close", fileName))
57+
}, f.Name())
5658

5759
return f, nil
5860
}
@@ -91,7 +93,7 @@ func (f *File) Close() error {
9193
f.closed = true
9294

9395
err := closeFile(f.osFile.File)
94-
runtime.SetFinalizer(f, nil)
96+
f.cleanup.Stop()
9597
return err
9698
}
9799

0 commit comments

Comments
 (0)