Fail with a clear error when device monitor stdin is not a TTY - #5503
Merged
ivankravets merged 2 commits intoAug 25, 2026
Merged
Conversation
`pio device monitor` crashed with a raw termios traceback
("Inappropriate ioctl for device") whenever stdin was piped or
redirected instead of a real terminal, e.g. `echo "" | pio device
monitor`. This happened because pyserial's Miniterm.Console tries to
call termios.tcgetattr() on stdin during Terminal construction, which
only works on a real TTY.
Detect non-TTY stdin before constructing the Terminal and raise a
clear UserSideException instead, pointing users toward talking to the
serial port directly (e.g. with pyserial) if they're trying to
automate device I/O from a script.
Fixes platformio#5113
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the UX and reliability of pio device monitor by preventing a low-level termios crash when stdin is not an interactive TTY (e.g., when piped or redirected), and instead raising a clear, actionable UserSideException.
Changes:
- Add an early stdin TTY check in
new_terminal()to fail fast with a user-friendly error. - Add a regression test that mocks non-TTY stdin and asserts the expected
UserSideException.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| platformio/device/monitor/terminal.py | Adds early stdin TTY validation and raises a clear UserSideException before constructing the miniterm console. |
| tests/commands/test_device_monitor.py | Adds a regression test covering the non-TTY stdin behavior and expected error message. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pio device monitorcrashes with a raw termios traceback (Inappropriate ioctl for device) whenever stdin is piped or redirected instead of a real terminal, e.g.echo "" | pio device monitor. This happens because pyserial'sMiniterm.Consolecallstermios.tcgetattr()on stdin duringTerminalconstruction, which only works when stdin is an actual TTY.This PR detects non-TTY stdin before constructing the
Terminaland raises a clearUserSideExceptioninstead, pointing users toward talking to the serial port directly (e.g. with pyserial) if they're trying to automate device I/O from a script, which was the reporter's use case.Fixes #5113
Changes
platformio/device/monitor/terminal.py: checksys.stdin.isatty()at the top ofnew_terminal()before any serial/terminal setup happenstests/commands/test_device_monitor.py: regression test that mocks a non-TTY stdin and asserts the clear error is raisedTest plan
pytest tests/commands/test_device_monitor.py -v— all 4 tests pass (3 existing + 1 new)make lint(pylint) — 10.00/10 on both changed filesblack --check/isort --check-only— clean, no changes neededcodespell— clean