Skip to content

feat: let lua skip stdin an embedder keeps open - #264

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/lua-outside-input
Nov 13, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/lua-outside-input

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

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 -e and lua script.lua hang 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.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

… 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.
@ferdingler
ferdingler requested a review from a team as a code owner November 13, 2026 02:38
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 on main too (1/20 runs on 46eb349, 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\n waiting on an open outside stdin:
    • lua -e 'print(io.read())' prints nil.
    • head -1 prints OUTSIDE.
    • Logs: strands-agents/shell/pr/264/repro-outside-stdin.txt.
  • Mutation test: I removed the outside_fds.remove in set_channel_reader (os.rs:564). After that, { echo hi; } \| lua -e 'print(io.read())' prints nil, and all 232 lua_integration tests 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. <&0 and exec 3<&0 already fail on channel stdin on main, so dup_fd never moves the mark.
Questions (non-blocking)
  • Bare lua on outside stdin runs an empty script and exits 0, which tests/lua_integration.rs:1441 pins. Real Lua would read the script or open a REPL. Would an error, or the repl_mode path, be clearer than a silent no-op?
  • If the marker stays: would a field on FdKind::ChannelReader be simpler than a parallel HashSet<Fd>? It would travel with the fd through transfer_fd, dup2, take_reader and restore_fd for free, and remove the eight sync sites. Today take_reader/restore_fd don't touch the set; that's only safe because the one caller, read.rs:60, restores the same reader. Also, outside names who set the fd, not what it does. Every set_channel_reader(0, …) is already outside input. Something like may_stay_open would say what matters.
Reading order for human reviewers
  1. src/builtins/lua.rs:66-107: the behaviour change.
  2. src/os.rs:574-584: the new API.
  3. The remaining outside_fds sync sites in os.rs.
  4. tests/lua_integration.rs:1420-1512.
Appendix: non-blocking (2)
  • The outside_fds.remove in alloc_fd (os.rs:506) can't fire, because next_fd only grows. The propagate branch in dup2 is never hit either, since from is 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).

Comment thread src/builtins/lua.rs Resolved
Comment thread tests/lua_integration.rs Resolved
Comment thread tests/lua_integration.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

On your questions: Box needs neither io.read seeing caller input nor a bare lua that waits, so I'm keeping the marker. It matches what Box does today, and on-demand reading can be its own follow-up. I'm also keeping the name and the empty-script behaviour for bare lua, since that's what Box does now. On a field in FdKind::ChannelReader: its fields are public, so adding one would break anyone who builds or matches it. That's why the mark lives on Process. The test nits are in 6c38ca9.

@strandly-the-agent please review

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_reader clear: fixed. With that clear removed, lua_reads_a_pipe_or_redirect_over_outside_input now fails (lua_integration.rs:1473).
  • Test that pinned the hang: fixed, it's dropped. Dropping the alloc_fd clear is safe too: next_fd starts 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 --check passes. cargo clippy --all-targets shows the same warnings as on main (46eb349), nothing new.
  • Mutation test at this head: with the clear in set_channel_reader removed, 230 tests pass and 1 fails, and the failure is the new compound-pipeline case.

@ferdingler
ferdingler merged commit b4beb0c into strands-agents:main Nov 13, 2026
31 checks passed
@ferdingler
ferdingler deleted the fix/lua-outside-input branch November 13, 2026 03:43
ferdingler added a commit to ferdingler/shell that referenced this pull request Nov 13, 2026
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.
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.

2 participants