Skip to content

Commit b4e992b

Browse files
committed
reflect: use runtime.AddCleanup instead of runtime.SetFinalizer
Replace a usage of runtime.SetFinalizer with runtime.AddCleanup in the TestCallReturnsEmpty test. There is an additional use of SetFinalizer in the reflect package which depends on object resurrection and needs further refactoring to replace. Updates #70907 Change-Id: I4c0e56c35745a225776bd611d026945efdaf96f5 Reviewed-on: https://go-review.googlesource.com/c/go/+/667595 Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent afd6b0b commit b4e992b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/reflect/all_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,18 +2240,18 @@ func TestCallReturnsEmpty(t *testing.T) {
22402240
// Issue 21717: past-the-end pointer write in Call with
22412241
// nonzero-sized frame and zero-sized return value.
22422242
runtime.GC()
2243-
var finalized uint32
2243+
var cleanedUp atomic.Uint32
22442244
f := func() (emptyStruct, *[2]int64) {
2245-
i := new([2]int64) // big enough to not be tinyalloc'd, so finalizer always runs when i dies
2246-
runtime.SetFinalizer(i, func(*[2]int64) { atomic.StoreUint32(&finalized, 1) })
2245+
i := new([2]int64) // big enough to not be tinyalloc'd, so cleanup always runs when i dies
2246+
runtime.AddCleanup(i, func(cu *atomic.Uint32) { cu.Store(uint32(1)) }, &cleanedUp)
22472247
return emptyStruct{}, i
22482248
}
2249-
v := ValueOf(f).Call(nil)[0] // out[0] should not alias out[1]'s memory, so the finalizer should run.
2249+
v := ValueOf(f).Call(nil)[0] // out[0] should not alias out[1]'s memory, so the cleanup should run.
22502250
timeout := time.After(5 * time.Second)
2251-
for atomic.LoadUint32(&finalized) == 0 {
2251+
for cleanedUp.Load() == 0 {
22522252
select {
22532253
case <-timeout:
2254-
t.Fatal("finalizer did not run")
2254+
t.Fatal("cleanup did not run")
22552255
default:
22562256
}
22572257
runtime.Gosched()

0 commit comments

Comments
 (0)