Don't double-close the child pty fd (fixes parallel Bad file descriptor) (#3975)#3976
Merged
gaborbernat merged 2 commits intoJul 8, 2026
Merged
Conversation
get_stream_file_no closed the child pty fd once the child inherited it, then closed it again on generator teardown. The second close is a harmless suppressed EBADF in a serial run, but under parallel execution the freed fd number can be reused by a sibling run in between, so the second close corrupts the sibling's descriptor -> intermittent 'Bad file descriptor' / 'Input/output error'. Track whether the child fd was already closed and skip it in the teardown loop, so each fd is closed exactly once (the master fd is still released). Fixes tox-dev#3975.
for more information, see https://pre-commit.ci
gaborbernat
approved these changes
Jul 8, 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.
Summary
Fixes #3975 — a regression in 4.56.2 (from #3974) where parallel runs intermittently fail with
Bad file descriptor/Input/output error.Root cause
In
get_stream_file_no(the pty path used when running under a tty), the child fd is closed once the child has inherited it, and then closed again on generator teardown:In a serial run the second
os.close(child_fd)is a harmless suppressedEBADF. But under parallel execution, between (1) and (2) the freed fd number can be reused by a sibling run — so (2) closes that run's descriptor instead, causing the intermittentBad file descriptor/Input/output error.Fix
Track whether the child fd was already closed and skip it in the teardown loop, so each fd is closed exactly once. The master fd is still released (no leak), and the early-teardown path (before the child fd is inherited) still closes both.
Tests
test_get_stream_file_no_closes_each_pty_fd_once: it fails onmain(the child fd is closed twice:[22, 22, 11]) and passes here.test_local_subprocess_tty_closes_master_fd,test_pty_closes_fds_when_termios_fails) still pass; fulltests/execute/local_subprocess/test_local_subprocess.pyis green (50 passed, 1 skipped).ruff checkclean. Changelog fragment added.Disclosure: prepared with AI assistance; reviewed and verified locally.