Skip to content

fix: use detach() to preserve stdout buffer on teardown#96

Merged
cristipufu merged 1 commit intomainfrom
fix/interceptor-detach-stdout-buffer
Feb 27, 2026
Merged

fix: use detach() to preserve stdout buffer on teardown#96
cristipufu merged 1 commit intomainfrom
fix/interceptor-detach-stdout-buffer

Conversation

@cristipufu
Copy link
Member

@cristipufu cristipufu commented Feb 27, 2026

Summary

  • Fixes ValueError when writing to stdout after interceptor teardown on Windows piped mode (cp1252 encoding)
  • Replaces self.utf8_stdout.close() with self.utf8_stdout.detach() in _interceptor.py teardown to avoid destroying the shared sys.stdout.buffer
  • Adds del self.utf8_stdout to guard against double-teardown since detach() is not idempotent

Context

When stdout uses non-UTF-8 encoding (e.g. cp1252 on Windows in piped mode), the interceptor creates a TextIOWrapper around sys.stdout.buffer. close() cascades to close the shared underlying buffer, making sys.stdout unusable after teardown. detach() disconnects the wrapper without closing the buffer.

Test plan

  • Verify CLI runs correctly when stdout is piped (non-TTY, cp1252 encoding)
  • Verify no ValueError from click.echo() after interceptor teardown
  • Verify logging output still works correctly during interceptor lifecycle

🤖 Generated with Claude Code

Development Package

  • Add this package as a dependency in your pyproject.toml:
[project]
dependencies = [
  # Exact version:
  "uipath-runtime==0.9.2.dev1000960357",

  # Any version from PR
  "uipath-runtime>=0.9.2.dev1000960000,<0.9.2.dev1000970000"
]

[[tool.uv.index]]
name = "testpypi"
url = "https://test.pypi.org/simple/"
publish-url = "https://test.pypi.org/legacy/"
explicit = true

[tool.uv.sources]
uipath-runtime = { index = "testpypi" }

Copilot AI review requested due to automatic review settings February 27, 2026 08:08
@cristipufu cristipufu requested a review from a team as a code owner February 27, 2026 08:08

This comment was marked as outdated.

@cristipufu cristipufu force-pushed the fix/interceptor-detach-stdout-buffer branch 3 times, most recently from 3981996 to 13e7849 Compare February 27, 2026 08:22
@cristipufu cristipufu self-assigned this Feb 27, 2026
When stdout uses non-UTF-8 encoding (e.g. cp1252 on Windows piped mode),
the interceptor creates a TextIOWrapper around sys.stdout.buffer. Using
close() on teardown destroyed the shared buffer, causing ValueError in
subsequent stdout writes (e.g. click.echo()).

- Replace close() with detach() to disconnect the wrapper without
  closing the underlying buffer
- Reorder teardown: detach utf8_stdout before closing the handler,
  so StreamHandler.close() cannot cascade a close through the wrapper
- Add del to guard against double-teardown (detach is not idempotent)
- Bump version to 0.9.2

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cristipufu cristipufu force-pushed the fix/interceptor-detach-stdout-buffer branch from 13e7849 to 04562e6 Compare February 27, 2026 08:24
@cristipufu cristipufu merged commit e5f8721 into main Feb 27, 2026
76 checks passed
@cristipufu cristipufu deleted the fix/interceptor-detach-stdout-buffer branch February 27, 2026 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants