Skip to content

testing: panic swallowed if t.Cleanup calls t.Fatal #51395

Closed

Description

What version of Go are you using (go version)?

$ go version
go version go1.17.7 darwin/amd64

Does this issue reproduce with the latest release?

Yes, including go1.18rc1.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="${HOME}/Library/Caches/go-build"
GOENV="${HOME}/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="${HOME}/.gvm/pkgsets/go1.17.7/global/pkg/mod"
GOOS="darwin"
GOPATH="${HOME}/.gvm/pkgsets/go1.17.7/global"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="${HOME}/.gvm/gos/go1.17.7"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="${HOME}/.gvm/gos/go1.17.7/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.7"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/rn/xy02hv_5403f5ln4wbs47hsc0000gp/T/go-build1816554494=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Called t.Fatal from a t.Cleanup function for a test that panicked, see: https://go.dev/play/p/Q3zMx79r8gA

func TestPanicSwallowedByCleanup(t *testing.T) {
	t.Cleanup(func() {
		// This causes the panic to never be logged:
		t.Fatal("problem during cleanup")
	})
	panic("kaboom")
}

What did you expect to see?

The panic encountered in the test to be reported with a full stack trace, as would be the case if t.Error was called from the t.Cleanup, see: https://go.dev/play/p/iV4-JV4ArBf

=== RUN   TestPanicWithCleanup
    prog.go:10: problem during cleanup
--- FAIL: TestPanicWithCleanup (0.00s)
panic: kaboom [recovered]
	panic: kaboom

goroutine 6 [running]:
testing.tRunner.func1.2({0x4b6440, 0x4ee1a8})
	/usr/local/go-faketime/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
	/usr/local/go-faketime/src/testing/testing.go:1212 +0x218
panic({0x4b6440, 0x4ee1a8})
	/usr/local/go-faketime/src/runtime/panic.go:1038 +0x215
main.TestPanicWithCleanup(0xc000001ba0)
	/tmp/sandbox3107448856/prog.go:12 +0x65
testing.tRunner(0xc000001ba0, 0x4d4d80)
	/usr/local/go-faketime/src/testing/testing.go:1259 +0x102
created by testing.(*T).Run
	/usr/local/go-faketime/src/testing/testing.go:1306 +0x35a

Program exited.

What did you see instead?

The original panic is completely omitted, we only see the t.Fatal output:

=== RUN   TestPanicSwallowedByCleanup
    prog.go:10: problem during cleanup
--- FAIL: TestPanicSwallowedByCleanup (0.00s)
FAIL

Program exited.

Note, this is possibly related to: #49929

Activity

  1. seankhliao commented on Feb 28, 2022

    @seankhliao
    Member

    Duplicate of #29207

  2. locked and limited conversation to collaborators on Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions