Skip to content

pb-cli: pb run sends piped stdin to the program - #17

Merged
dlech merged 1 commit into
mainfrom
fix/run-piped-stdin
Nov 26, 2026
Merged

dlech merged 1 commit into
mainfrom
fix/run-piped-stdin

Conversation

@dlech

@dlech dlech commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

printf 'a\n' | pb run prog.py left a program that calls input() waiting forever. Without a terminal, pb run streamed the hub's output but never read its own stdin. Now stdin that is a pipe, a file or a device goes to the program, the way ssh sends it.

Related: pybricks/pybricksdev#17 (the same request for pybricksdev run).

Behaviour

  • What is forwarded: piped or redirected stdin, when there is no terminal or with --wait. A terminal is never read this way: a background job reading its terminal is stopped with SIGTTIN.
  • Line endings become \r. The hub's input() ends a line on \r and silently drops \n, so piped text would otherwise never finish a line. \r\n split across two reads counts as one line ending.
  • Nothing is sent until the hub reports the program running, because the hub empties its input buffer as a program starts.
  • A slow reader slows the pipe; it doesn't lose input. The service gives up on a write after 10 s of HUB_BUSY, which suits a keystroke. For piped input the forwarder carries on from the byte the hub stopped at, for as long as the program runs.
  • Firmware 4.0 is the exception. Since 442ffe8, the service marks a write that 4.0's undersized buffer can never take with firmware_limit. The forwarder says once that input wasn't sent, including the service's "update the hub's firmware" message, and stops forwarding; the program carries on.
  • The end of the input is not the end of the program. Nothing is sent to signal it.
  • An error reading stdin is not caught. It ends the session, which stops the program, instead of leaving the program waiting for input that can no longer come.
  • pb run -n / pb start -n (--no-input, PB_NO_INPUT) leave stdin alone. This is for a pb command inside a while read loop, which otherwise hands the rest of the loop's input to the first program, the same trap as with ssh and the same way out.

This is a behaviour change for released pybricks-cli (1.0.0b1 and 1.0.0b2): a pb run inside such a loop used to work only because stdin was ignored, and now needs -n. The changelog lists forwarding under Changed and -n under Added, in Unreleased.

How it waits

No timer polling anywhere.

  • POSIX: a pipe, socket or device is waited on by the event loop. A device the selector refuses is always ready and is read like a file. Linux's epoll refuses /dev/null, /dev/zero and /dev/urandom with EPERM (measured); libuv's source shows macOS's kqueue refusing some /dev files with EINVAL. The /dev/null and /dev/zero tests pass on macOS in CI.
  • Windows: the shell's anonymous pipe has nothing an event loop will wait on. There is no overlapped I/O, the handle is signalled regardless of data, and ReOpenFile with FILE_FLAG_OVERLAPPED fails with ERROR_PIPE_BUSY (measured). So a worker thread waits in a zero-byte ReadFile, which consumes nothing, and CancelSynchronousIo releases it. This is libuv's approach. Its race is closed the libuv way too: the release keeps cancelling until the thread says it is past the read, because a cancel can land on other I/O of that thread and report success. An independent review reproduced that bug, and the fix has a regression test. Overlapped handles, and pipes that can't be read (such as the write end), are left alone.
  • Known limits on Windows, documented in the module:
    • While that read waits, the pipe handle is busy: another thread's PeekNamedPipe on it, or a child process inheriting it, waits for data. Nothing in pb does either during a session; subprocesses must be given their own stdin.
    • If a garbage-collection finalizer does blocking I/O in the instant before the read starts, Windows itself can make the cancel call wait for that read (measured with plain threads, no pb code). libuv has the same exposure.

Testing

  • Unit tests in packages/pb-cli/tests/test_piped.py: line endings, POSIX devices, real pipes and files, waking a read already in progress, 200 cancels at spread-out moments, no spinning on Windows, overlapped handles, a pipe's write end, the cancel-hits-other-I/O regression, and read errors propagating. The regression test fails against the old release logic and passed 60 out of 60 runs on Python 3.12 under CPU load, where an earlier version of it was flaky.
  • End-to-end tests in tests/e2e/test_repl.py against the simulated hub: piped input arrives with \r endings and the program outlives it, a pipe held open doesn't hold the command, input redirected from a file, a slow reader with HUB_BUSY resumes without losing or duplicating bytes, firmware 4.0 is reported once and the program carries on, and -n leaves stdin for the next reader.
  • Full gate on Windows 11 ARM64, and pytest plus pyright in WSL Ubuntu 24.04, on the branch rebased onto current main.
  • On a Prime hub (firmware 4.1.3), before the rebase:
    • both \n and \r\n end a line
    • the program keeps running after its input ends
    • 10 KB reaches a program that waits 12 s before reading, both from a file and through a pipe
    • -n inside a while read loop
    • all 7 hardware tests pass

@dlech
dlech force-pushed the fix/run-piped-stdin branch from 97d2e51 to 742baee Compare November 26, 2026 01:32
`printf 'a\n' | pb run prog.py` left a program that calls input() waiting
for ever: without a terminal, the session ran with the terminal parts left
out, and sending input to the hub was one of them. Now stdin that is a pipe,
a file or a device goes to the program, the way ssh sends it. A terminal is
still left alone, because reading one from a background job stops the whole
process with SIGTTIN, and `pb run x.py > log &` is exactly that.

Three things the hub needs that a terminal never had to think about:

- Line endings become \r. The hub's input() ends a line on a carriage return,
  which is what Enter sends in raw mode, and drops a line feed without a
  trace, so piped text with \n or \r\n would never have finished a line.
- Nothing is sent until the hub says the program is running, because the
  hub empties its input buffer as a program starts.
- A refusal is waited out rather than reported. The service gives up after
  ten seconds of HUB_BUSY, which is right for a keystroke and wrong for a
  pipe: a program may do a minute's work before its first input(). The
  forwarder carries on from the byte the hub stopped at, for as long as the
  program runs, so a slow reader slows the pipe instead of losing it. The
  exception is firmware 4.0, whose HUB_BUSY carries firmware_limit: it has no
  room for a write the size it reports, now or ever, so that is said once and
  the input stops, and the program carries on.

The end of the input is not the end of the program, and nothing is sent to
say so. An error reading stdin is not caught: each one means something is
broken, and carrying on would leave the program waiting for input that can no
longer come.

Waiting never polls. On POSIX a pipe, socket or device is waited on by the
event loop; a device the selector refuses (epoll's EPERM for /dev/null, and
the EINVAL macOS's kqueue gives some /dev files) is always ready and read like
a file. On Windows nothing an event loop has will wait on a shell's
anonymous pipe, so a worker thread waits in a zero-byte ReadFile, which takes
nothing, and CancelSynchronousIo releases it. That is libuv's approach, and so
is closing its race: the release keeps cancelling until the thread says it is
past the read, because a cancel can land on some other I/O of the thread's
and report success. Overlapped handles, which that read cannot wait on, and a
pipe that cannot be read are left alone. One corner stays open, as it does
for libuv: Windows itself can make the cancel call wait for a read the thread
starts the instant a cancelled one ends, which only a finalizer doing
blocking I/O in that window can lead to.

`pb run -n` and `pb start -n` leave stdin alone, for a pb command inside a
loop that reads its own input -- the same trap as ssh, with the same way out.
pybricks-cli has been released since this was started, so forwarding is
listed as a change: such a loop used to work, by accident of stdin being
ignored, and now needs -n.

Checked on a Prime hub on 4.1.3: both line endings, input ending before the
program does, 10 KB through both a file and a pipe to a program that waits
twelve seconds before reading, and -n inside a while-read loop.
@dlech
dlech force-pushed the fix/run-piped-stdin branch from 742baee to 629d6dc Compare November 26, 2026 01:52
@dlech
dlech merged commit 1ab3106 into main Nov 26, 2026
23 checks passed
@dlech
dlech deleted the fix/run-piped-stdin branch November 26, 2026 03:36
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.

1 participant