Repository navigation
pb-cli: pb run sends piped stdin to the program - #17
Merged
Merged
Conversation
dlech
force-pushed
the
fix/run-piped-stdin
branch
from
November 26, 2026 01:32
97d2e51 to
742baee
Compare
`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
force-pushed
the
fix/run-piped-stdin
branch
from
November 26, 2026 01:52
742baee to
629d6dc
Compare
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.
printf 'a\n' | pb run prog.pyleft a program that callsinput()waiting forever. Without a terminal,pb runstreamed 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 waysshsends it.Related: pybricks/pybricksdev#17 (the same request for
pybricksdev run).Behaviour
--wait. A terminal is never read this way: a background job reading its terminal is stopped withSIGTTIN.\r. The hub'sinput()ends a line on\rand silently drops\n, so piped text would otherwise never finish a line.\r\nsplit across two reads counts as one line ending.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_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.pb run -n/pb start -n(--no-input,PB_NO_INPUT) leave stdin alone. This is for apbcommand inside awhile readloop, which otherwise hands the rest of the loop's input to the first program, the same trap as withsshand the same way out.This is a behaviour change for released
pybricks-cli(1.0.0b1 and 1.0.0b2): apb runinside such a loop used to work only because stdin was ignored, and now needs-n. The changelog lists forwarding under Changed and-nunder Added, in Unreleased.How it waits
No timer polling anywhere.
/dev/null,/dev/zeroand/dev/urandomwithEPERM(measured); libuv's source shows macOS's kqueue refusing some/devfiles withEINVAL. The/dev/nulland/dev/zerotests pass on macOS in CI.ReOpenFilewithFILE_FLAG_OVERLAPPEDfails withERROR_PIPE_BUSY(measured). So a worker thread waits in a zero-byteReadFile, which consumes nothing, andCancelSynchronousIoreleases 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.PeekNamedPipeon it, or a child process inheriting it, waits for data. Nothing inpbdoes either during a session; subprocesses must be given their own stdin.pbcode). libuv has the same exposure.Testing
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.tests/e2e/test_repl.pyagainst the simulated hub: piped input arrives with\rendings and the program outlives it, a pipe held open doesn't hold the command, input redirected from a file, a slow reader withHUB_BUSYresumes without losing or duplicating bytes, firmware 4.0 is reported once and the program carries on, and-nleaves stdin for the next reader.main.\nand\r\nend a line-ninside awhile readloop