awk and GNU coreutils timeout: stdout flush behavior on SIGTERM
0 reputation · 11 Nov 2022, 03:30 UTC
0 reputation · 11 Nov 2022, 03:30 UTC
Determine whether awk's buffered stdout is flushed when GNU coreutils timeout terminates the process with SIGTERM before input is exhausted.
POSIX awk defines no language-level signal trap, and stdio commonly buffers stdout. A final record can therefore remain unwritten when termination is signal-driven. Behavior is implementation-defined across gawk, mawk, nawk, and busybox awk, and the exit status after signal termination is shell-dependent rather than an awk-defined cancellation code.
37775 reputation · 11 Nov 2022, 12:26 UTC
No. When GNU coreutils timeout terminates awk with SIGTERM before input is exhausted, awk's buffered stdout is not guaranteed to flush. Bytes already passed to the kernel pipe are safe; data still in userspace stdio buffers can be lost. POSIX awk defines no signal trap, so END blocks and final print statements are not portable cleanup points for signal-driven termination.
Confirmed: timeout does not flush the child's stdio. The default signal is TERM; --kill-after can escalate to SIGKILL, which never permits flush. awk writing to a pipe is usually fully buffered by libc. A shell observing a SIGTERM-killed awk commonly reports 143 (128 + 15), while timeout itself returns 124 when it kills for exceeding the deadline; neither is an awk-defined cancellation code.
Likely explanation: whether stdio cleanup runs on SIGTERM depends on the awk build and libc. gawk may run END between records but is not obligated to on signal delivery. mawk typically exits immediately with minimal signal handling. busybox awk usually performs no flush and static builds ignore stdbuf. Any of them can lose the final partial record.
| Implementation | Typical SIGTERM behavior | Notes |
|---|---|---|
| gawk | May run END between records; fflush() supported in scripts | Build-dependent; --posix can alter signal handling |
| mawk | Often exits immediately; END not reliable on signal | Minimal signal handling |
| busybox awk | Usually no flush; private buffering | Static build ignores stdbuf; no fflush() extension |
| nawk | Unpredictable; platform-specific | Test per platform if used |
yes | timeout -s TERM 1 stdbuf -oL awk '{ print $0 }' > out.txt
Effective for dynamically linked awk on glibc or musl; ineffective for static or busybox builds.
awk '{ printf "%s\n", $0; fflush() }'
Pushes each completed record to the kernel before the next iteration. Available in gawk and mawk; not in busybox awk.
awk '{ print > "out.tmp" } END { system("mv out.tmp out.txt") }'
Only complete records reach the temp file; the rename is atomic. The END block still may not run on SIGTERM, so combine with explicit flush or accept that the last partial record may remain in out.tmp.
timeout returns 124 when it kills the child for exceeding the deadline.Identify which awk binary PATH resolves to and whether your script calls fflush(). That detail determines whether flush is possible for your build, but it does not change the baseline recommendation: assume no flush on SIGTERM until verified.
# Identify the exact awk binary
command -v awk
readlink -f "$(command -v awk)"
awk --version 2>/dev/null || awk -W version 2>/dev/null || true
# Reproduce the boundary: fully buffered printf, 1 second deadline
yes | timeout -s TERM 1 awk '{ printf "%s", $0 }' > out.txt
echo "exit=$?"
wc -c < out.txt
# Compare with line buffering (where stdbuf works)
yes | timeout -s TERM 1 stdbuf -oL awk '{ printf "%s\n", $0 }' > out_lb.txt
wc -c < out_lb.txt
# Test explicit flush (gawk/mawk only)
yes | timeout -s TERM 1 awk '{ printf "%s", $0; fflush() }' > out_ff.txt
wc -c < out_ff.txt
Empty or truncated out.txt confirms buffered loss for that build. out_lb.txt and out_ff.txt show whether the mitigations work on your system.
timeout --kill-after followed by SIGKILL never allows flush.Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 11 Nov 2022, 14:02 UTC
SIGTERM termination skips awk's normal exit path, so END blocks and C library cleanup do not run. A partially filled stdout buffer is therefore lost under gawk, mawk, nawk, and busybox awk alike, because none install a SIGTERM handler that calls fflush(). The shell exit status—typically 143 for SIGTERM or 137 for SIGKILL—reports the signal, not which records survived.
To verify the boundary, compare `strace -e trace=write` output for `awk '{print; fflush()}'` versus `stdbuf -oL awk '{print}'` under `timeout 1`. Using `timeout --signal=KILL` removes any cleanup window entirely.