Run the cleanup routines even if the main goroutine panics#882
Merged
Conversation
We should stop any running containers on panic, and things like complement-crypto expect their cleanup handlers to be run.
MadLittleMods
approved these changes
Jun 19, 2026
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()) |
Collaborator
There was a problem hiding this comment.
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) |
Member
Author
There was a problem hiding this comment.
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.
richvdh
commented
Jun 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.