Skip to content

Commit afb5fca

Browse files
committed
test: fix flaky test for issue24491
runtime.GC() doesn't guarantee the finalizer has run, so use a channel instead to make sure finalizer was run in call to "after()". Fixes #41361 Change-Id: I69c801e29aea49757ea72c52e8db13239de19ddc Reviewed-on: https://go-review.googlesource.com/c/go/+/254401 Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Trust: Cuong Manh Le <[email protected]>
1 parent 5f1b12b commit afb5fca

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

test/fixedbugs/issue24491b.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@ package main
1111

1212
import (
1313
"runtime"
14-
"sync/atomic"
1514
"unsafe"
1615
)
1716

18-
var done uint32
17+
var done = make(chan bool)
1918

2019
func setup() unsafe.Pointer {
2120
s := "ok"
22-
runtime.SetFinalizer(&s, func(p *string) { atomic.StoreUint32(&done, 1) })
21+
runtime.SetFinalizer(&s, func(p *string) { close(done) })
2322
return unsafe.Pointer(&s)
2423
}
2524

2625
//go:noinline
2726
//go:uintptrescapes
2827
func before(p uintptr) int {
2928
runtime.GC()
30-
if atomic.LoadUint32(&done) != 0 {
29+
select {
30+
case <-done:
3131
panic("GC early")
32+
default:
3233
}
3334
return 0
3435
}
3536

3637
func after() int {
3738
runtime.GC()
38-
if atomic.LoadUint32(&done) == 0 {
39-
panic("GC late")
40-
}
39+
runtime.GC()
40+
<-done
4141
return 0
4242
}
4343

0 commit comments

Comments
 (0)