Skip to content

runtime: add debug mode for finalizers and cleanups #72949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mknyszek opened this issue Mar 19, 2025 · 6 comments
Closed

runtime: add debug mode for finalizers and cleanups #72949

mknyszek opened this issue Mar 19, 2025 · 6 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Implementation Issues describing a semantics-preserving change to the Go implementation.
Milestone

Comments

@mknyszek
Copy link
Contributor

Finalizers are notoriously difficult to work with due to all the subtleties you need to be aware of. Cleanups have resolved several finalizer issues, but still carry some of the same issues.

To help detect misuse, I'd like to propose a new debug mode (GODEBUG) that runs a partial GC cycle during mark termination to detect reference cycles for finalizers and references from cleanup roots to the objects they're attached to. This debug mode can also include three additional checkers:

  • Detect when a finalizer or cleanup function fully blocks the goroutine it's on and report it as a potential issue.
  • Detect when a finalizer or cleanup is attached to a tiny alloc block. This will require additional state tracking.

I'll also note that execution traces would be a great place to analyze inefficiencies due to finalizers and/or cleanups.

https://go.dev/cl/634599 is a start.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Mar 19, 2025
@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@gabyhelp gabyhelp added the Implementation Issues describing a semantics-preserving change to the Go implementation. label Mar 19, 2025
@mknyszek mknyszek self-assigned this Mar 19, 2025
@mknyszek mknyszek added this to the Go1.25 milestone Mar 19, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/634599 mentions this issue: runtime: add new GODEBUG checkfinalizer

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/662135 mentions this issue: runtime: annotate checkfinalizers reports with source and type info

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/662038 mentions this issue: runtime: add scan trace for checkfinalizers>1

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/662039 mentions this issue: runtime: add package doc for checkfinalizer mode

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/662037 mentions this issue: runtime: mark and identify tiny blocks in checkfinalizers mode

gopherbot pushed a commit that referenced this issue May 20, 2025
This new debug mode detects cleanup/finalizer leaks using checkmark
mode. It runs a partial GC using only specials as roots. If the GC can
find a path from one of these roots back to the object the special is
attached to, then the object might never be reclaimed. (The cycle could
be broken in the future, but it's almost certainly a bug.)

This debug mode is very barebones. It contains no type information and
no stack location for where the finalizer or cleanup was created.

For #72949.

Change-Id: Ibffd64c1380b51f281950e4cfe61f677385d42a5
Reviewed-on: https://go-review.googlesource.com/c/go/+/634599
Reviewed-by: Carlos Amedee <[email protected]>
Reviewed-by: Michael Pratt <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
gopherbot pushed a commit that referenced this issue May 20, 2025
This change adds a new special kind called CheckFinalizer which is used
to annotate finalizers and cleanups with extra information about where
that cleanup or finalizer came from.

For #72949.

Change-Id: I3c1ace7bd580293961b7f0ea30345a6ce956d340
Reviewed-on: https://go-review.googlesource.com/c/go/+/662135
Reviewed-by: Carlos Amedee <[email protected]>
Reviewed-by: Michael Pratt <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
gopherbot pushed a commit that referenced this issue May 20, 2025
This change adds support for identifying cleanups and finalizers
attached to tiny blocks to checkfinalizers mode. It also notes a subtle
pitfall, which is that the cleanup arg, if tiny-allocated, could end up
co-located with the object with the cleanup attached! Oops...

For #72949.

Change-Id: Icbe0112f7dcfc63f35c66cf713216796a70121ce
Reviewed-on: https://go-review.googlesource.com/c/go/+/662037
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
Reviewed-by: Michael Pratt <[email protected]>
Reviewed-by: Carlos Amedee <[email protected]>
gopherbot pushed a commit that referenced this issue May 20, 2025
This change dumps a scan trace (each pointer marked and where it came
from) for the partial GC cycle performed by checkfinalizers mode when
checkfinalizers>1. This is useful for quickly understanding why certain
values are reachable without having to pull out tools like viewcore.

For #72949.

Change-Id: Ic583f80e9558cdfe1c667d27a1d975008dd39a9c
Reviewed-on: https://go-review.googlesource.com/c/go/+/662038
Reviewed-by: Michael Pratt <[email protected]>
Reviewed-by: Carlos Amedee <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
@github-project-automation github-project-automation bot moved this from Todo to Done in Go Compiler / Runtime May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Implementation Issues describing a semantics-preserving change to the Go implementation.
Projects
Development

No branches or pull requests

3 participants