Repository navigation
Fix Python controller hang at exit with stdout/stderr redirection on Windows - #7049
Open
rin-katsuragi wants to merge 1 commit into
Open
rin-katsuragi wants to merge 1 commit into
rin-katsuragi wants to merge 1 commit into
Conversation
…berbotics#7040) On Windows, Python controllers started with --stdout-redirect or --stderr-redirect hung indefinitely upon script completion. The helper thread in StdStreamRedirect was non-daemon and blocked in readline() on the pipe, while Python waits for non-daemon threads before running atexit handlers where the write end was supposed to close. Resolve the deadlock by making the helper thread a daemon thread, adding an explicit close() method registered with atexit, and restoring original stream handles upon cleanup.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Shutdown can still deadlock when pending output fills the native redirect pipe.
1 open finding
What changed in this PR
Addresses #7040 by updating Windows Python controller output-redirection shutdown.
Changes:
- Makes redirect helpers daemon threads and registers explicit cleanup at exit.
- Restores original streams before closing and joining helpers.
- Documents the fix in the changelog.
| File | Description |
|---|---|
| lib/controller/python/controller/robot.py | Updates redirect-thread lifecycle and stream cleanup. |
| docs/reference/changelog-r2025.md | Records the Windows controller shutdown fix. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| elif self._stream == 2: | ||
| sys.stderr = self._orig | ||
| self._w.close() | ||
| self._thread.join() |
This branch has not been deployed
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.

Description
On Windows, Python controllers run with
--stdout-redirector--stderr-redirect(or withWEBOTS_STDOUT_REDIRECT/WEBOTS_STDERR_REDIRECTset) hung indefinitely upon script completion.This was caused by a circular deadlock in
StdStreamRedirect:daemon=False) and blocked onself._r.readline()on the anonymous pipe.atexitcallbacks.self._wremained open), Python hung permanently beforeatexitcould close it.Fix:
daemon=True) so Python proceeds into theatexitstage.close()method registered directly viaatexit.register(...). Closing the write end sends EOF down the pipe, allowingself._thread.join()to safely flush all remaining output toucrtbasebefore process exit.sys.stdout/sys.stderrto the original streams before closing the pipe to preventValueError: I/O operation on closed fileduring late interpreter shutdown.Related Issues
Fixes #7040
Tasks
Additional context
Verified on Windows 11 with Python 3.13 / 3.14: scripts now flush completely and terminate with exit code
0immediately. Conforms to PEP8 and repository source check standards.