Repository navigation
feat: let lua skip stdin an embedder keeps open - #264
Conversation
… for it `lua -e` and `lua <file>` read stdin to its end before the script runs, and `lua` with no script reads its source from stdin. When an embedder feeds stdin from outside the shell and keeps the channel open, each such command waits until the deadline. `Process::set_outside_reader` installs a channel reader like `set_channel_reader` and marks the fd as input fed from outside the shell. The mark moves with the fd through `transfer_fd` and `dup2`, and anything else installed on the fd clears it. Lua reads marked input as empty, as source and as `io.read` input. A pipe or a redirect inside the shell still reaches Lua. A default shell, and an embedder that uses `set_channel_reader`, behave as before.
|
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested, on the approach. The mark follows stdin correctly everywhere we probed, and the suite passes. The trouble is how it fixes the hang: a marked stdin reads as empty, so io.read() returns nil even when the caller's data is already waiting. head -1 on the same fd returns that data.
The root cause is that lua drains stdin before the script runs, whether or not the script reads it (lua.rs:96-107). If io.read/io.lines read fd 0 lazily, the hang goes away for every open channel stdin, including today's set_channel_reader embedders. That also drops the new public method and the fd bookkeeping in os.rs. The VM already runs exec_async and uses create_async_function for io.open/io.popen, so the pieces are there.
@ferdingler: does Box need io.read to see caller input? Does bare lua have to return right away? If neither, lazy read is a better fit.
This adds public API (Process::set_outside_reader). The repo has no needs-api-review label; design is the closest.
✅ What I verified
- Head
256262a.cargo test --workspace --all-targets --no-fail-fast: 2538 passed, 0 failed. - One earlier run failed
compound_pipe_stderr_timeout_reported_once. That test is flaky onmaintoo (1/20 runs on46eb349, 4/20 on this PR). It's a 500 ms timing test on a path this PR doesn't touch. No existing issue; not filed. - Repro, with
OUTSIDE\nwaiting on an open outside stdin:lua -e 'print(io.read())'printsnil.head -1printsOUTSIDE.- Logs:
strands-agents/shell/pr/264/repro-outside-stdin.txt.
- Mutation test: I removed the
outside_fds.removeinset_channel_reader(os.rs:564). After that,{ echo hi; } \| lua -e 'print(io.read())'printsnil, and all 232lua_integrationtests still pass (see inline). - Probe matrix of about 50 cases with outside stdin held open. Lua returns without blocking in all of them:
$(…), functions,{…}, if/for/while/case,&+wait,sh -c,sh script,eval, here-docs, redirects,lua \| cat, and nested subshells. Pipes and redirects over the mark still reach Lua.<&0andexec 3<&0already fail on channel stdin onmain, sodup_fdnever moves the mark.
Questions (non-blocking)
- Bare
luaon outside stdin runs an empty script and exits 0, whichtests/lua_integration.rs:1441pins. Real Lua would read the script or open a REPL. Would an error, or therepl_modepath, be clearer than a silent no-op? - If the marker stays: would a field on
FdKind::ChannelReaderbe simpler than a parallelHashSet<Fd>? It would travel with the fd throughtransfer_fd,dup2,take_readerandrestore_fdfor free, and remove the eight sync sites. Todaytake_reader/restore_fddon't touch the set; that's only safe because the one caller,read.rs:60, restores the same reader. Also,outsidenames who set the fd, not what it does. Everyset_channel_reader(0, …)is already outside input. Something likemay_stay_openwould say what matters.
Reading order for human reviewers
src/builtins/lua.rs:66-107: the behaviour change.src/os.rs:574-584: the new API.- The remaining
outside_fdssync sites inos.rs. tests/lua_integration.rs:1420-1512.
Appendix: non-blocking (2)
- The
outside_fds.removeinalloc_fd(os.rs:506) can't fire, becausenext_fdonly grows. The propagate branch indup2is never hit either, sincefromis always a freshly allocated fd. - Pre-existing: a pipeline after an open channel stdin leaves fd 0 gone (
read: bad fd 0). That's #249; not re-raised.
Artifacts: cargo-test.log, repro-outside-stdin.txt, adversarial-probe.rs, adversarial-probe-output.txt (portal, under pr/264).
|
On your questions: Box needs neither @strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. Both test findings are fixed in 6c38ca9, and I'm withdrawing the design point. FdKind is a public enum in pub mod os and isn't marked #[non_exhaustive], so a new ChannelReader field would break anyone who builds or matches it. That's a good reason to keep the mark on Process.
- Missing test for the
set_channel_readerclear: fixed. With that clear removed,lua_reads_a_pipe_or_redirect_over_outside_inputnow fails (lua_integration.rs:1473). - Test that pinned the hang: fixed, it's dropped. Dropping the
alloc_fdclear is safe too:next_fdstarts at 3 and only goes up, so it can never hand out fd 0. - Lazy
io.read: withdrawn, since Box doesn't need it. It can come later as its own change.
✅ What I verified at 6c38ca9
cargo test --workspace --all-targets --no-fail-fast: 2537 passed, 0 failed. That's one fewer than last time because the hang test is gone.cargo fmt --checkpasses.cargo clippy --all-targetsshows the same warnings as onmain(46eb349), nothing new.- Mutation test at this head: with the clear in
set_channel_readerremoved, 230 tests pass and 1 fails, and the failure is the new compound-pipeline case.
Merges strands-agents#257 and strands-agents#264. The deadline stop after a compound piped into a pipeline now runs after the caller's stdin is given back. Stdin set aside around a substitution or a pipeline stage keeps the outside-input marker, so Lua still reads it as empty.
Description
When an embedder feeds the shell's stdin from its own side and keeps it open (Box streams a caller's stdin this way),
lua -eandlua script.luahang until the deadline. Lua reads all of stdin before it runs the script, so it waits for input that never ends, even when the script never reads anything.This adds an opt-in way for an embedder to mark stdin as "fed from outside, may stay open". Lua treats that input as empty, the same as before the embedder fed anything, while pipes and redirects inside the shell still reach Lua as usual. Nothing changes for a default shell or for an embedder that uses the existing channel reader. Box keeps this in its vendored copy today, and it needs it upstream before it can drop that copy.
I added tests for the new marker (Lua finishes and sees empty input, including inside a subshell and after
read), for pipes and redirects over it, and for the unchanged default, where Lua still reads a channel stdin to its end. Without the Lua change the main test hangs and fails at its timeout. The full Rust suite passes and clippy shows no new warnings. I did not run the Python or Node tests, since the bindings are untouched.Related Issues
None. Box uses this when it builds against upstream Shell.
Documentation PR
None.
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.