From 0efd2c33e2fbc3192f2aa1290307653087a72f0e Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Mon, 15 Dec 2025 09:10:57 +0000 Subject: [PATCH] mitogen: first_stage: Add comments about closed STDERR Signed-off-by: Marc Hartmayer --- mitogen/parent.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mitogen/parent.py b/mitogen/parent.py index fc83ef79..29ac8f55 100644 --- a/mitogen/parent.py +++ b/mitogen/parent.py @@ -1415,9 +1415,6 @@ class Connection(object): # W: write side of interpreter stdin. # r: read side of core_src FD. # w: write side of core_src FD. - - # Final os.close(STDERR_FILENO) to avoid --py-debug build corrupting stream with - # "[1234 refs]" during exit. @staticmethod def _first_stage(): # Bail out in case STDIN or STDOUT is not accessible (e.g. closed). @@ -1425,6 +1422,8 @@ class Connection(object): # unexpected behavior that is difficult to diagnose. os.fstat(0) os.fstat(1) + # If STDERR (2) is already closed, it might be possible that one of the + # returned pipe FDs is 2. R,W=os.pipe() r,w=os.pipe() if os.fork(): @@ -1460,6 +1459,10 @@ class Connection(object): f.write(C) f.close() os.write(1,'MITO001\n'.encode()) + # Final os.close(STDERR_FILENO) to avoid `--py-debug` build corrupting + # stream with "[1234 refs]" during exit. + # If STDERR is already closed an OSError is raised, but no one cares + # as STDERR is closed and the exit status is not forwarded. os.close(2) def get_python_argv(self):