Skip to content

runtime: AddCleanup does not panic if arg equals ptr #71316

Closed
@cherrymui

Description

Go version

tip (go1.24-40b3c0e58a Fri Jan 17 08:40:47 2025 -0800 darwin/arm64)

Output of go env in your module/workspace:

any

What did you do?

https://go.dev/play/p/2SwFBThx4xJ?v=gotip

In particular,runtime.AddCleanup(t, func(a *T) { println("cleanup", a.x); ch <- 1 }, t) where the first and third arguments are equal.

What did you see happen?

It does not panic at the AddCleanup call. The cleanup never runs.

What did you expect to see?

runtime.AddCleanup is documented that it panics if arg is equal to ptr. E.g.
the code above should panic at the AddCleanup call.

The source code of AddCleanup includes

	// Check that arg is not equal to ptr.
	// TODO(67535) this does not cover the case where T and *S are the same
	// type and ptr and arg are equal.
	if unsafe.Pointer(&arg) == unsafe.Pointer(ptr) {
		throw("runtime.AddCleanup: ptr is equal to arg, cleanup will never run")
	}

But it is checking the address of arg not equal to ptr, not arg itself. And &arg cannot equal to ptr, because it's the address of an argument of a new frame. So this condition can never trigger. Also, it is a throw, not a panic as documented.

Is this what the TODO is for?

cc @cagedmantis @mknyszek

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.release-blocker

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions