Skip to content

fix: stop logger teardown from closing process stdout#31

Open
raulmol wants to merge 1 commit into
microsoft:mainfrom
raulmol:fix/logger-closes-stdout
Open

fix: stop logger teardown from closing process stdout#31
raulmol wants to merge 1 commit into
microsoft:mainfrom
raulmol:fix/logger-closes-stdout

Conversation

@raulmol

@raulmol raulmol commented Jun 11, 2026

Copy link
Copy Markdown

Fixes #30.

setup_logger wraps sys.stdout.buffer in a new TextIOWrapper per call; after clean_logger drops the RichHandler, garbage collection of that wrapper closes the underlying buffer — the process's real stdout. The next setup_logger call in the same process (organize stage after setup stage) then raises ValueError: I/O operation on closed file, and run_organize's own console.print crashes with the same error while reporting it, masking the root cause.

Change

launch/utilities/logger.py only: create the UTF-8-wrapped rich Console once at module level (_get_shared_console) and reuse it for every RichHandler, so the wrapper is never collected and stdout stays open across logger setup/teardown cycles. Behavior (UTF-8 re-encoding with errors="replace", soft wrap) is unchanged.

Verification

import gc, sys, tempfile
from pathlib import Path
from launch.utilities.logger import setup_logger, clean_logger

d = Path(tempfile.mkdtemp())
for i in range(2):
    lg = setup_logger(f"inst{i}", [d / f"l{i}.log"], printing=True)
    lg.info("cycle %d", i)
    clean_logger(lg)
    gc.collect()

sys.stdout.buffer.write(b"stdout alive\n")  # raised ValueError before this fix

Also verified end-to-end on a private monorepo task: before the fix, the organize stage failed immediately after a successful setup stage with I/O operation on closed file; with the fix it proceeds into the organize agents.

setup_logger wraps sys.stdout.buffer in a new TextIOWrapper on every
call. After clean_logger removes the RichHandler, the wrapper becomes
unreachable and its garbage collection closes the underlying buffer,
i.e. the process's real stdout. Any later setup_logger call in the same
process (e.g. the organize stage after the setup stage) then raises
'ValueError: I/O operation on closed file'.

Share a single module-level console that is created once and never
collected, so stdout stays open across logger setup/teardown cycles.
@raulmol

raulmol commented Jun 11, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

setup_logger closes process stdout, breaking the organize stage in single-process runs

1 participant