Skip to content

Run the cleanup routines even if the main goroutine panics#882

Merged
richvdh merged 2 commits into
mainfrom
rav/cleanup_on_panic
Jun 20, 2026
Merged

Run the cleanup routines even if the main goroutine panics#882
richvdh merged 2 commits into
mainfrom
rav/cleanup_on_panic

Conversation

@richvdh

@richvdh richvdh commented Jun 19, 2026

Copy link
Copy Markdown
Member

We should stop any running containers on panic, and things like complement-crypto expect their cleanup handlers to be run.

There is a comment in complement-crypto to this effect (https://github.com/matrix-org/complement-crypto/blob/6ba55b6fb878dd44872edc44d9632241d253a8cc/internal/cc/instance.go#L42), and that expectation seems to have been broken by the transition to WithCleanup.

We should stop any running containers on panic, and things like
complement-crypto expect their cleanup handlers to be run.
@richvdh richvdh requested review from a team as code owners June 19, 2026 11:24
Comment thread test_main.go
Comment on lines +80 to +90
// Run the cleanup functions even on panic.
runAndCleanup := func() int {
defer testPackage.Cleanup()
if opts.cleanup != nil {
defer opts.cleanup(testPackage.Config)
}

return m.Run()
}
testPackage.Cleanup()
os.Exit(exitCode)

os.Exit(runAndCleanup())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this also work? (less layers)

Suggested change
// Run the cleanup functions even on panic.
runAndCleanup := func() int {
defer testPackage.Cleanup()
if opts.cleanup != nil {
defer opts.cleanup(testPackage.Config)
}
return m.Run()
}
testPackage.Cleanup()
os.Exit(exitCode)
os.Exit(runAndCleanup())
defer testPackage.Cleanup()
if opts.cleanup != nil {
defer opts.cleanup(testPackage.Config)
}
exitCode := m.Run()
os.Exit(exitCode)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Per https://pkg.go.dev/os#Exit, deferred functions are not run on os.Exit. The extra layer gives the deferred functions a chance to run.

I'll add a comment to clarify this.

Comment thread test_main.go
@richvdh richvdh merged commit fd276da into main Jun 20, 2026
@richvdh richvdh deleted the rav/cleanup_on_panic branch June 20, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants