Skip to content

The agent that grows with you

fix(terminal): skip stdout.close() while a Windows drain thread is still reading (#67362)

teknium1428777a66954
diff --git a/tools/environments/base.py b/tools/environments/base.py
index b212ca7c13d..b9a51f78f2d 100644
--- a/tools/environments/base.py
+++ b/tools/environments/base.py
@@ -422,10 +422,14 @@ class BaseEnvironment(ABC):
# The drain thread exits promptly after bash does (~300ms idle check);
# a long join here would itself indicate a bug in the drain loop.
drain_thread.join(timeout=2)
- try:
- proc.stdout.close()
- except Exception:
- pass
+ # Windows: close() serializes on the CRT's per-fd lock that a drain thread still
+ # inside os.read() holds, so it would block until the grandchild exits (#67362).
+ # Leave the fd to the daemon thread / Popen.__del__ in that case.
+ if not (os.name == "nt" and drain_thread.is_alive()):
+ try:
+ proc.stdout.close()
+ except Exception:
+ pass
trace.natural_exit(proc.returncode)
# Join the stdin writer before reading its error list: a child that exits without