Skip to content

Rebind log handlers to the console when the loading screen closes - #9226

Merged
kellyguo11 merged 3 commits into
isaac-sim:mainfrom
kellyguo11:kellyg/fix-loading-screen-log-handlers
Nov 27, 2026
Merged

kellyguo11 merged 3 commits into
isaac-sim:mainfrom
kellyguo11:kellyg/fix-loading-screen-log-handlers

Conversation

@kellyguo11

Copy link
Copy Markdown
Contributor

Description

On Windows and macOS, the loading screen hides startup output by replacing sys.stdout / sys.stderr with a temporary spool file (Linux redirects file descriptors instead and is unaffected). Any logging.StreamHandler created while the screen is up — e.g. by ensure_console_handlers or configure_logging — binds to that spool. Once startup finishes the spool is closed, so every later record fails with ValueError: I/O operation on closed file and Python prints a --- Logging error --- traceback. The run itself is unaffected.

Seen running examples/sensors/foveated_camera.py on Windows, where the first record after startup was the Newton site view's "Fabric runtime unavailable; Newton site poses will not be mirrored to prims." INFO message.

Fix: when LoadingScreen._restore puts the original streams back, it now repoints every logging stream handler that still writes to the spool at the matching real stream. sys.stderr is a thin alias of the spool during startup so handlers created on stderr go back to stderr rather than stdout.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Release backport

  • Backport this pull request to the active release branch after it merges into main

Checklist

  • I have read and understood the contribution guidelines
  • I have run the pre-commit checks with uv run isaaclab --format (ruff check / format)
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have validated the changed behavior with focused coverage and applied the test-audit criteria to any test changes — added test_restore_rebinds_log_handlers_created_during_stream_swapping, which fails before the fix and passes after
  • I have added a changelog fragment under source/<pkg>/changelog.d/ for every touched package
  • I have added my name to the CONTRIBUTORS.md or my name already exists there

On Windows and macOS the loading screen swaps sys.stdout/sys.stderr for a
temporary spool. Logging handlers created during startup captured the spool,
so every record logged after it closed printed a "--- Logging error ---"
traceback with "I/O operation on closed file". Point those handlers back at
the real streams when the console is restored.
@kellyguo11
kellyguo11 requested a review from a team November 25, 2026 19:42
@github-actions github-actions Bot added bug Something isn't working isaac-lab Related to Isaac Lab team labels Nov 25, 2026
@greptile-apps

greptile-apps Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Restores logging output after the loading screen closes on Windows and macOS.

  • Startup log handlers keep writing to the right console stream afterward.

Tests were inspected, not run.

Reviews (1) · Last reviewed commit: "Rebind log handlers to the console when ..." · Reviewed by Greptile

@isaaclab-review-bot isaaclab-review-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isaac Lab Review Bot

Needs attention: Compatibility and deprecation (1), Implementation (2), Style consistency (4), Test quality (2). Other reviewed categories look good.

  • PR description: The title, platform scope, stderr-routing rationale, and reported regression match the diff; replace “every logging stream handler” with “handlers attached to registered loggers”; no performance claim requires benchmarking.
  • Design and architecture: Architecture looks good.
  • Scope and over-engineering: No over-engineering issues.
  • API: API changes look good.
  • Compatibility and deprecation: Breaking changes: redirected stderr no longer supports iteration or context-manager operations because getattr does not forward special methods; explicitly forward the former file operations.
  • Implementation: Dictionary membership hashes unrelated handler streams and can abort restoration with TypeError; compare streams by identity instead.
  • Style consistency: The added declarations violate the prescribed private-function/class order; move _StreamAlias below the private functions.
  • Test quality: The distinct post-startup logging regression lacks kitless CI selection; add an explicit pytest.mark.kitless marker.

Significant concerns. Posted 9 inline findings.

Automated review; human maintainers own approval decisions.

elif self._saved_streams is not None:
sys.stdout.flush()
sys.stderr.flush()
spooled_streams = (sys.stdout, sys.stderr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Critical · Implementation — Match the installed spool streams

If stdout or stderr is replaced again during startup, these current globals are not the streams earlier handlers captured. Those handlers remain bound to the closed spool. Retain and match the installed streams.

loggers = [logging.getLogger(), *logging.Logger.manager.loggerDict.values()]
for logger in loggers:
for handler in getattr(logger, "handlers", ()):
if isinstance(handler, logging.StreamHandler) and handler.stream in replacements:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Critical · Implementation — Compare handler streams by identity

A StreamHandler can hold an unhashable stream. Dictionary membership then raises TypeError on an unrelated handler, interrupting _restore before spool cleanup. Compare stream objects by identity instead.

sys.stdout = self._spool
sys.stderr = self._spool
# a distinct object lets _restore tell handlers bound to stderr from those bound to stdout
sys.stderr = _StreamAlias(self._spool)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Warning · Compatibility — Preserve stderr's file protocol

Special methods do not pass through getattr. Iterating redirected sys.stderr or entering it as a context manager now raises TypeError, unlike the former file object. Forward those operations on the alias.

return spooled


class _StreamAlias:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Warning · Style Consistency — Order private functions before classes

_StreamAlias precedes _rebind_log_handlers and the remaining private functions, contrary to the prescribed file order. Move the class below the private functions.

class _StreamAlias:
"""Forward every attribute to *target* while being a distinct object from it."""

def __init__(self, target: IO[str]):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Warning · Style Consistency — Annotate no-value signatures

The new constructor and regression test omit -> None despite the contribution rule for no-value functions. Annotate both signatures.



def _rebind_log_handlers(replacements: dict[Any, IO[str]]) -> None:
"""Point every logging stream handler writing to a key of *replacements* at the mapped stream."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Warning · Style Consistency — Remove signature-paraphrasing docstring

The helper docstring merely restates its mapping argument and operation, adding no constraint or intent. Remove it or document the non-obvious ownership boundary.

"""Point every logging stream handler writing to a key of *replacements* at the mapped stream."""
loggers = [logging.getLogger(), *logging.Logger.manager.loggerDict.values()]
for logger in loggers:
for handler in getattr(logger, "handlers", ()):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Warning · Style Consistency — Filter logger placeholders explicitly

loggerDict also contains PlaceHolder objects. getattr with an empty default obscures that distinction and violates the direct-access rule. Filter for logging.Logger, then read logger.handlers.

assert loading_screen.sys.stderr is original_stderr


def test_restore_rebinds_log_handlers_created_during_stream_swapping(monkeypatch: pytest.MonkeyPatch):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Warning · Test Quality — Route regression through kitless CI

This regression needs no Isaac Sim, but the file contains no pytest.mark.kitless text for CI selection. Mark the test, or the merged lifecycle test, explicitly.



def test_restore_rebinds_log_handlers_created_during_stream_swapping(monkeypatch: pytest.MonkeyPatch):
original_stdout = _OpenStringIO()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Warning · Test Quality — Extend the existing lifecycle test

The preceding test already constructs both streams and runs redirect/restore. Add the handler-output assertions there instead of maintaining a second copy of that lifecycle setup.

…dlers

Keep the installed spool streams instead of reading sys.stdout/sys.stderr at
restore time, compare handler streams by identity so an unhashable stream cannot
abort the restore, and skip logger placeholders explicitly. Fold the regression
into the existing stream-swapping test and mark it kitless.
@hujc7

hujc7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

run-ci

@isaaclab-bot isaaclab-bot Bot added ci:run-docker Trigger the on-demand Docker and GPU CI workflow and removed ci:run-docker Trigger the on-demand Docker and GPU CI workflow labels Nov 25, 2026
@AntoineRichard

Copy link
Copy Markdown
Collaborator

run-ci

@isaaclab-bot isaaclab-bot Bot added ci:run-docker Trigger the on-demand Docker and GPU CI workflow and removed ci:run-docker Trigger the on-demand Docker and GPU CI workflow labels Nov 26, 2026
@kellyguo11
kellyguo11 merged commit 19cff01 into isaac-sim:main Nov 27, 2026
65 of 96 checks passed
isaaclab-bot Bot pushed a commit that referenced this pull request Nov 27, 2026
)

# Description

On Windows and macOS, the loading screen hides startup output by
replacing `sys.stdout` / `sys.stderr` with a temporary spool file (Linux
redirects file descriptors instead and is unaffected). Any
`logging.StreamHandler` created while the screen is up — e.g. by
`ensure_console_handlers` or `configure_logging` — binds to that spool.
Once startup finishes the spool is closed, so every later record fails
with `ValueError: I/O operation on closed file` and Python prints a `---
Logging error ---` traceback. The run itself is unaffected.

Seen running `examples/sensors/foveated_camera.py` on Windows, where the
first record after startup was the Newton site view's `"Fabric runtime
unavailable; Newton site poses will not be mirrored to prims."` INFO
message.

Fix: when `LoadingScreen._restore` puts the original streams back, it
now repoints every logging stream handler that still writes to the spool
at the matching real stream. `sys.stderr` is a thin alias of the spool
during startup so handlers created on stderr go back to stderr rather
than stdout.

## Type of change

- Bug fix (non-breaking change which fixes an issue)

## Release backport

- [x] <!-- backport-active-release --> Backport this pull request to the
active release branch after it merges into `main`

## Checklist

- [x] I have read and understood the [contribution
guidelines](https://isaac-sim.github.io/IsaacLab/main/source/refs/contributing.html)
- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`uv run isaaclab --format` (ruff check / format)
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have validated the changed behavior with focused coverage and
applied the [test-audit
criteria](https://github.com/isaac-sim/IsaacLab/blob/HEAD/skills/developer/test-audit/SKILL.md)
to any test changes — added
`test_restore_rebinds_log_handlers_created_during_stream_swapping`,
which fails before the fix and passes after
- [x] I have added a changelog fragment under
`source/<pkg>/changelog.d/` for every touched package
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------

Co-authored-by: Antoine RICHARD <antoiner@nvidia.com>

(cherry picked from commit 19cff01)
@isaaclab-bot

isaaclab-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Backported to release/3.1.0 as 968fb36.

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

Labels

bug Something isn't working isaac-lab Related to Isaac Lab team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants