Skip to content

fix(gawk): exit a BEGIN-only awk program without reading input - #2095

Open
atharvamhaske wants to merge 1 commit into
rivet-dev:mainfrom
atharvamhaske:fix/awk-begin-only-exits
Open

atharvamhaske wants to merge 1 commit into
rivet-dev:mainfrom
atharvamhaske:fix/awk-begin-only-exits

Conversation

@atharvamhaske

@atharvamhaske atharvamhaske commented Nov 8, 2026 •

Copy link
Copy Markdown

Fixes #2088.

awk 'BEGIN { print "AWK" }' printed its output and then waited to read stdin until EOF. vm.process.exec leaves stdin open, so the call ran until its timeout and returned timed_out with exit 137.

POSIX awk exits after a program made only of BEGIN actions without reading input. The wrapper in software/gawk/native/crates/awk/src/lib.rs now checks the parsed program. If every rule is a BEGIN rule, it runs the program with no input. awk-rs returns 0 for a plain getline and never feeds it the main input, so skipping the input loses nothing.

A BEGIN-only program also no longer opens the files named on the command line. awk 'BEGIN { print "x" }' /nonexistent now prints x and exits 0 instead of failing with exit 2. macOS awk does the same.

Testing

  • cargo test -p agentos-awk: the new begin_only_programs_do_not_read_input test checks that BEGIN-only programs skip input and that programs with an END rule, a main rule or a pattern still read it.
  • I built cmd-awk on the host and ran awk 'BEGIN { print "AWK" }' with its stdin pipe held open. The patched binary printed AWK and exited 0 in 0.68 s. The binary built from main had not exited after 10 minutes.
  • printf 'a b\nc d\n' | awk '{ print $2 } END { print NR }' still prints b, d, 2.

I did not rerun the issue's VM repro. The issue also notes that stdin: '' does not help. That points to vm.process.exec not closing the guest's stdin when given an empty string, which this PR does not change.

`awk 'BEGIN { print "AWK" }'` printed its output and then blocked reading
stdin until EOF. Through `vm.process.exec` stdin stays open, so the call
ran until its timeout and returned `timed_out` with exit 137.

POSIX awk exits after a program made only of BEGIN actions without
reading input. The wrapper now runs such a program with no input. awk-rs
never feeds the main input to a plain `getline`, so this loses nothing.
Named input files are not opened either, as with other awk versions.

Fixes rivet-dev#2088

This branch has not been deployed

No deployments
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.

process.exec: BEGIN-only awk prints output then times out, including stdin: ''

1 participant