fix: ensure container removal on teardown even if bash hangs or dies#2887
Merged
Conversation
`OCIContainer.__enter__` was hardened in pypa#2879 to always clean up on failure, but `__exit__` was left unguarded. If the container/bash had already died, `bash_stdin.write("exit 0")` raises `BrokenPipeError`; if bash refuses to exit, `process.wait(timeout=30)` raises `TimeoutExpired`. Either propagated out of `__exit__` before `_remove_container()` ran, leaking the container (and, on timeout, the `start` process too). Wrap the teardown so a broken pipe or timeout instead forces the process down (kill + wait) and always falls through to container removal, while still respecting CIBW_DEBUG_KEEP_CONTAINER. Pipe closes are now also guarded so a flush-on-close against a dead pipe can't mask cleanup. Add non-docker unit tests for the clean-exit, already-dead-bash, and shutdown-timeout paths. Assisted-by: ClaudeCode:claude-opus-4.8
agriyakhetarpal
approved these changes
Jun 4, 2026
Member
agriyakhetarpal
left a comment
There was a problem hiding this comment.
I feel this change is narrowly scoped, and since it doesn't change any other behaviour on successes, I am +1 for this. I can't think of any problems with it. LGTM!
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.
This is a fix for item 4 in #2885. First pass tried to introduce a double nested try, I reduced it to a single try.
🤖 AI text below 🤖
OCIContainer.__enter__was hardened in #2879 to always clean up onfailure, but
__exit__was left unguarded. If the container/bash hadalready died,
bash_stdin.write("exit 0")raisesBrokenPipeError; ifbash refuses to exit,
process.wait(timeout=30)raisesTimeoutExpired.Either propagated out of
__exit__before_remove_container()ran,leaking the container (and, on timeout, the
startprocess too).Wrap the teardown so a broken pipe or timeout instead forces the process
down (kill + wait) and always falls through to container removal, while
still respecting CIBW_DEBUG_KEEP_CONTAINER. Pipe closes are now also
guarded so a flush-on-close against a dead pipe can't mask cleanup.
Add non-docker unit tests for the clean-exit, already-dead-bash, and
shutdown-timeout paths.
Assisted-by: ClaudeCode:claude-opus-4.8