Commit c48f7008 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb/internal/weak: Assert that the object was not moved in the finalizer

This is good to do since we are relying on non-moving property of
current GC. It never triggered but it removed one uncertainty while
debugging GC crash issue. Good to have it in place.

/reviewed-by @levin.zimmermann
/reviewed-on !11
parent 55491547
...@@ -31,9 +31,12 @@ ...@@ -31,9 +31,12 @@
package weak package weak
import ( import (
"fmt"
"runtime" "runtime"
"sync" "sync"
"unsafe" "unsafe"
_ "go4.org/unsafe/assume-no-moving-gc"
) )
...@@ -76,6 +79,12 @@ func NewRef[T any](obj *T) *Ref[T] { ...@@ -76,6 +79,12 @@ func NewRef[T any](obj *T) *Ref[T] {
var release func(*T) var release func(*T)
release = func(obj *T) { release = func(obj *T) {
// assert that the object was not moved
iptr := (uintptr)(unsafe.Pointer(obj))
if w.iptr != iptr {
panic(fmt.Sprintf("weak: release: object moved: w.iptr=%x obj=%x", w.iptr, iptr))
}
// GC decided that the object is no longer reachable and // GC decided that the object is no longer reachable and
// scheduled us to run as finalizer. During the time till we // scheduled us to run as finalizer. During the time till we
// actually run, Ref.Get might have been come to run and // actually run, Ref.Get might have been come to run and
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment