Skip to content

[stdlib] Fix input() dropping piped lines after the first - #7378

Closed
Alex-Wengg wants to merge 1 commit into
modular:mainfrom
Alex-Wengg:fix/7198-input-piped
Closed

Alex-Wengg wants to merge 1 commit into
modular:mainfrom
Alex-Wengg:fix/7198-input-piped

Conversation

@Alex-Wengg

@Alex-Wengg Alex-Wengg commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue

Fixes #7198

Type of change

  • Bug fix (non-breaking change that fixes an issue)

Motivation

input() returns the first line of piped stdin and then raises EOF on the next call. For example, printf 'a\nb\n' | mojo t.mojo with two input() calls prints a and then fails. Reading line by line from a pipe or a redirected file is a common way to use input(), so any script that does this is broken.

What changed

Every input() call opened a new buffered FILE on dup(stdin). getdelim filled 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 with setvbuf(..., _IONBF), so getdelim stops reading at the newline. It also uses with so the stream is closed, which fixes a FILE and file-descriptor leak on every call.
  • The setvbuf binding in std.sys._libc passed only stream and buffer to C, dropping mode and size. 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 read syscall per byte. That's the same trade-off C stdio makes for unbuffered streams, and it's negligible for line-at-a-time input().

Testing

  • Added test/builtin/test_input_multiline.mojo as a lit test that pipes three lines into the program and reads them with three input() calls, then checks that a fourth call raises EOF. With the current stdlib it fails with EOF on the second line; with this change it passes.
  • The existing test_stdin.mojo and test_issue_3908.mojo still pass.
  • Interactive input from a terminal (pty) still works.
  • ./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

  • The linked issue above has been reviewed by a maintainer and is agreed-upon, or this is a trivial fix that does not need prior approval
  • PR is small and focused
  • I ran mojo format on the changed files
  • I added or updated tests to cover my changes
  • If AI tools assisted with this contribution, I have included an Assisted-by: trailer in my commit message or this PR description

Assisted-by: AI

🤖 Generated with Claude Code

@Alex-Wengg
Alex-Wengg requested review from a team as code owners October 23, 2026 05:19
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@github-actions github-actions Bot added mojo-stdlib Tag for issues related to standard library waiting-on-review mojo-docs labels Oct 23, 2026
@Alex-Wengg
Alex-Wengg marked this pull request as draft October 23, 2026 05:24
@Alex-Wengg
Alex-Wengg force-pushed the fix/7198-input-piped branch from cb39a5b to a2da960 Compare October 24, 2026 23:55
@Alex-Wengg
Alex-Wengg marked this pull request as ready for review November 1, 2026 23:26
@willwray

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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 setvbuf binding 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]>
@willwray

Copy link
Copy Markdown
Contributor

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Nov 13, 2026

@willwray willwray left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@modularbot

Copy link
Copy Markdown
Collaborator

✅🟣 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.

@modularbot modularbot added the merged-internally Indicates that this pull request has been merged internally label Nov 13, 2026
@willwray

Copy link
Copy Markdown
Contributor

Merged internally, so will show up in the next nightly

@modularbot

Copy link
Copy Markdown
Collaborator

Landed in 26b974c! Thank you for your contribution 🎉

@Alex-Wengg Alex-Wengg closed this Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

imported-internally Signals that a given pull request has been imported internally. merged-internally Indicates that this pull request has been merged internally mojo-docs mojo-stdlib Tag for issues related to standard library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] stdlib: input() loses piped input after the first line

4 participants