From b68dfbe6021d7c03b78d826aa79168dbcd67eb5a Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Wed, 17 Dec 2025 08:47:05 +0000 Subject: [PATCH] mitogen/parent: Bail out if STDIN or STDOUT is closed Bail out if STDIN or STDOUT is closed or unavailable, as these streams are required for the communication with the parent process. Without this check, the later `os.pipe()` calls in the first_stage may return file descriptors 0 and 1, leading to a confusing and hard-to-diagnose situation. -SSH command size: 838 +SSH command size: 850 -mitogen.parent 99240 96.9KiB 51244 50.0KiB 51.6% 12956 12.7KiB 13.1% +mitogen.parent 99496 97.2KiB 51275 50.1KiB 51.5% 12964 12.7KiB 13.0% Signed-off-by: Marc Hartmayer --- mitogen/parent.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mitogen/parent.py b/mitogen/parent.py index 97681653..fc83ef79 100644 --- a/mitogen/parent.py +++ b/mitogen/parent.py @@ -1420,6 +1420,11 @@ class Connection(object): # "[1234 refs]" during exit. @staticmethod def _first_stage(): + # Bail out in case STDIN or STDOUT is not accessible (e.g. closed). + # Otherwise, os.pipe() could reuse file descriptors 0 or 1, leading to + # unexpected behavior that is difficult to diagnose. + os.fstat(0) + os.fstat(1) R,W=os.pipe() r,w=os.pipe() if os.fork():