Skip to content

cmd/compile: yet another incorrect recover behavior due to defer a method wrapper #73920

Closed

Description

@cherrymui

Go version

Go 1.20 - Go 1.24

Output of go env in your module/workspace:

darwin/arm64 (or any)

What did you do?

https://go.dev/play/p/D621WgLz6y2

package main

func callRecover() {
	if recover() != nil {
		println("recovered")
	}
}

type T int

func (*T) M() { callRecover() }

type S struct{ *T } // has a wrapper (*S).M wrapping (*T.M)

var p = &S{new(T)}

var fn = (*S).M // using a function pointer to force using the wrapper

func main() {
	defer fn(p)
	panic("XXX")
}

What did you see happen?

The panic is recovered. The program prints "recovered" and exits normally.

What did you expect to see?

Similar to #73917, it is expected to not recover.

This is pretty much the same as #73917, except that it defers a method call with a pointer receiver, whereas it is a value receiver in #73917. I'm filing a separate issue as

  • it started to fail from a different commit,
  • this actually passes on tip, but fails with Go 1.24.

Similarly, it panics as expected if inlining is disabled.

$ go run -gcflags=-l x3.go
panic: XXX

goroutine 1 [running]:
main.main()
	/tmp/x3.go:21 +0x68
exit status 2

Go 1.19 got it right. Go 1.18 and later fail. Bisection points to CL https://go.dev/cl/422235 , which apparently has a lot of effects (I could bisect further with GOEXPERIMENT=unified, but I'll defer it for later). Before that CL, the wrapper (*S).M does a tail call to (*T).M with no inlining. After that CL, the wrapper (*S).M inlines (*T).M, while keeping the WRAPPER attribute. Similar to #73916 and #73917, this causes the panic to be treated one frame too deep.

It's been the same behavior up to Go 1.24. On tip, it changes, since CL https://go.dev/cl/650455 . Before that CL, tail call is not used for that type of wrappers, because the wrapped function (*T).M is inlineable (and actually being inlined). After that CL, we start with emitting an OTAILCALL without the WRAPPER attribute, and later inlines the wrapped function into it, still without the WRAPPER attribute. This corrects the panic stack depth issue (while leaving the issue of the wrapper not being omitted from traceback #73747).

The proposed fix for #73747, CL 675916 (as of PS 1), sets the WRAPPER attribute, which would reintroduce this bug.

Activity

  1. added
    BugReportIssues describing a possible bug in the Go implementation.
    on May 29, 2025
  2. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on May 30, 2025
  3. added this to the Backlog milestone on May 30, 2025
  4. zigo101 commented on May 30, 2025

    @zigo101
    Contributor

    Go 1.18 and later fail.

    typo? It should be 1.20.

  5. gopherbot commented on May 30, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/677675 mentions this issue: cmd/compile: Modify inline tree to avoid parent node for tail call site.

  6. modified the milestone: Backlog on Jun 4, 2025
  7. gopherbot commented on Jul 2, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/685375 mentions this issue: runtime: iterate through inlinings when processing recover()

  8. cuonglm commented on Jul 31, 2025

    @cuonglm
    Member

    I think we can close this issue now

    cc @randall77 @cherrymui

  9. randall77 commented on Jul 31, 2025

    @randall77
    Contributor

    Agreed, fixed by CL 685375. I wrote "Fixex #73920" instead of "Fixes #73920" in that CL :)

  10. locked and limited conversation to collaborators on Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions