]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: clarify finalizer semantics for tiny objects
authorChangkun Ou <hi@changkun.de>
Mon, 26 Jul 2021 12:23:26 +0000 (14:23 +0200)
committerMichael Knyszek <mknyszek@google.com>
Tue, 15 Nov 2022 17:33:54 +0000 (17:33 +0000)
This change clarifies that a finalizer is not guaranteed to run,
not only for zero bytes objects but also tiny objects (< 16bytes).

Fixes #46827

Change-Id: I193e77f6f024c79110604f86bcb1a28b16cf98ec
Reviewed-on: https://go-review.googlesource.com/c/go/+/337391
Run-TryBot: Changkun Ou <mail@changkun.de>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Joedian Reid <joedian@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/runtime/mfinal.go

index a1d08d9293b8d0508bd62dff87e06df5bf1575f5..257e9d15602a43a011cab2faa91f921bd4563922 100644 (file)
@@ -312,12 +312,21 @@ func runfinq() {
 // bufio.Writer, because the buffer would not be flushed at program exit.
 //
 // It is not guaranteed that a finalizer will run if the size of *obj is
-// zero bytes.
+// zero bytes, because it may share same address with other zero-size
+// objects in memory. See https://go.dev/ref/spec#Size_and_alignment_guarantees.
 //
 // It is not guaranteed that a finalizer will run for objects allocated
 // in initializers for package-level variables. Such objects may be
 // linker-allocated, not heap-allocated.
 //
+// Note that because finalizers may execute arbitrarily far into the future
+// after an object is no longer referenced, the runtime is allowed to perform
+// a space-saving optimization that batches objects together in a single
+// allocation slot. The finalizer for an unreferenced object in such an
+// allocation may never run if it always exists in the same batch as a
+// referenced object. Typically, this batching only happens for tiny
+// (on the order of 16 bytes or less) and pointer-free objects.
+//
 // A finalizer may run as soon as an object becomes unreachable.
 // In order to use finalizers correctly, the program must ensure that
 // the object is reachable until it is no longer required.