Skip to content

runtime: add debug mode for finalizers and cleanups #72949

@mknyszek

Description

@mknyszek

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.

Metadata

Metadata

Assignees

Labels

ImplementationIssues describing a semantics-preserving change to the Go implementation.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions