Repository navigation
[stdlib] Fix input() dropping piped lines after the first - #7378
Alex-Wengg wants to merge 1 commit into
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
cb39a5b to
a2da960
Compare
|
@copilot resolve the merge conflicts in this pull request |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the piped-input fix.
No open issues 🎉
What changed in this PR
Fixes #7198 by preserving consecutive lines of piped input in Mojo’s input() and closing each per-call stream to prevent resource leaks.
Changes:
- Makes
input()streams unbuffered and closes them after reading. - Corrects the
setvbufbinding to accept a null buffer and forward all four arguments. - Adds a regression test for consecutive piped lines and EOF, plus a changelog entry.
| File | Description |
|---|---|
Mojo/stdlib/test/builtin/test_input_multiline.mojo |
Tests three consecutive piped lines followed by EOF. |
Mojo/stdlib/test/builtin/BUILD.bazel |
Registers the regression test. |
Mojo/stdlib/std/sys/_libc.mojo |
Corrects the setvbuf signature and argument forwarding. |
Mojo/stdlib/std/io/io.mojo |
Uses an unbuffered, scoped stream for input(). |
Mojo/docs/site/nightly-changelog.md |
Documents the piped-input fix. |
Review effort: Balanced
`input()` opens a new buffered stream on a dup of stdin on every call. `getdelim` filled that stream's buffer with everything available, so for piped input the remaining lines were consumed and lost when the stream closed, and the next call raised `EOF`. Make the per-call stream unbuffered so it stops at the newline. This also fixes the `setvbuf` binding, which dropped its `mode` and `size` arguments and took a non-nullable buffer, and restores the `BufferMode` constants that were removed while unused. Fixes modular#7198 Assisted-by: AI Signed-off-by: Alex-Wengg <[email protected]>
a2da960 to
da02051
Compare
|
!sync |
willwray
left a comment
There was a problem hiding this comment.
Good spot and nice fix, thanks. sync'd internally and merging.
The file-descriptor leak had been fixed on main so the claim is removed from the commit message, and "Generated with..." is removed to leave just "Assisted-by: AI" as suggested in AI_TOOL_POLICY.md
The changelog entry is also shortened to just state the change and remove the issue link.
|
✅🟣 This contribution has been merged 🟣✅ Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the main branch during the next Mojo nightly release, typically within the next 24-48 hours. We use Copybara to merge external contributions, click here to learn more. |
|
Merged internally, so will show up in the next nightly |
|
Landed in 26b974c! Thank you for your contribution 🎉 |
Linked issue
Fixes #7198
Type of change
Motivation
input()returns the first line of piped stdin and then raisesEOFon the next call. For example,printf 'a\nb\n' | mojo t.mojowith twoinput()calls printsaand then fails. Reading line by line from a pipe or a redirected file is a common way to useinput(), so any script that does this is broken.What changed
Every
input()call opened a new bufferedFILEondup(stdin).getdelimfilled that stream's buffer with everything the pipe had, the stream was never closed, and the extra lines it had read were lost. The next call opened a fresh stream at EOF.input()makes its per-call stream unbuffered withsetvbuf(..., _IONBF), sogetdelimstops reading at the newline. It also useswithso the stream is closed, which fixes aFILEand file-descriptor leak on every call.setvbufbinding instd.sys._libcpassed onlystreamandbufferto C, droppingmodeandsize. It also took a non-nullable buffer, although passing NULL is the normal use. It had no callers before this change.Unbuffered reads issue one
readsyscall per byte. That's the same trade-off C stdio makes for unbuffered streams, and it's negligible for line-at-a-timeinput().Testing
test/builtin/test_input_multiline.mojoas a lit test that pipes three lines into the program and reads them with threeinput()calls, then checks that a fourth call raisesEOF. With the current stdlib it fails withEOFon the second line; with this change it passes.test_stdin.mojoandtest_issue_3908.mojostill pass../bazelw test //Mojo/stdlib/test/builtin:test_input_multiline.mojo.test //Mojo/stdlib/test/builtin:test_stdin.mojo.test //Mojo/stdlib/test/builtin:test_issue_3908.mojo.test //Mojo/stdlib/test/io/...: 6/6 pass on macOS arm64. I have not tested on Linux.Checklist
mojo formaton the changed filesAssisted-by:trailer in my commit message or this PR descriptionAssisted-by: AI
🤖 Generated with Claude Code