Skip to content

Fail with a clear error when device monitor stdin is not a TTY - #5503

Merged
ivankravets merged 2 commits into
platformio:developfrom
JacksonBopp:fix/monitor-non-tty-stdin
Aug 25, 2026
Merged

ivankravets merged 2 commits into
platformio:developfrom
JacksonBopp:fix/monitor-non-tty-stdin

Conversation

@JacksonBopp

Copy link
Copy Markdown
Contributor

Summary

pio device monitor crashes 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's Miniterm.Console calls termios.tcgetattr() on stdin during Terminal construction, which only works when stdin is an actual TTY.

This PR detects non-TTY stdin before constructing the Terminal and raises 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, which was the reporter's use case.

Fixes #5113

Changes

  • platformio/device/monitor/terminal.py: check sys.stdin.isatty() at the top of new_terminal() before any serial/terminal setup happens
  • tests/commands/test_device_monitor.py: regression test that mocks a non-TTY stdin and asserts the clear error is raised

Test 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 files
  • black --check / isort --check-only — clean, no changes needed
  • codespell — clean

`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
@CLAassistant

CLAassistant commented Aug 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI 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.

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.

Comment thread platformio/device/monitor/terminal.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@ivankravets ivankravets added this to the 6.2.0 milestone Aug 25, 2026
@ivankravets
ivankravets merged commit 2dad4c2 into platformio:develop Aug 25, 2026
1 check passed
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.

pio monitor fails when stdin is a pipe

4 participants