Skip to content

Fail the run on write errors to --out-file, --json-out-file or stdout - #729

Merged
fcostaoliveira merged 6 commits into
masterfrom
fix/719-output-write-errors
Oct 16, 2026
Merged

fcostaoliveira merged 6 commits into
masterfrom
fix/719-output-write-errors

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

If writing the results fails (disk full, quota, I/O error), memtier prints no error and exits 0. A script that checks the exit code then accepts a missing or truncated results file as a good run. This affects --out-file, --json-out-file, and the default stdout output (> /dev/full). In the JSON case memtier even logs Json file closed.

Root cause

  • --out-file: main() closed the file with an unchecked fclose(outfile). The fprintf() results and ferror() were never checked either, so the buffered ENOSPC was lost. Results on stdout were never flushed or checked at all.
  • --json-out-file: ~json_handler() called fclose(m_json_file) without checking it and logged Json file closed. whatever happened.

Fix

  • New finish_output_file(FILE *, bool do_close) in file_io.cpp. It runs fflush(), then ferror(), then (optionally) fclose(), and returns 0 or an errno value. If an earlier write failed and its errno is gone, it returns EIO.
  • json_handler::close() closes the nesting and the file and returns the error. main() calls it. The destructor still calls it for the early-exit paths (which ignore the result). Json file closed. is logged only when the close succeeds.
  • On failure main() prints error: failed writing --out-file PATH: REASON (or --json-out-file PATH, or results to stdout) and returns 1. That is the code already used for a run that did not complete (g_worker_failed). A connection-stage abort still returns 2 first.
  • A stdout reader that went away is not an error. memtier ignores SIGPIPE, so memtier ... | head makes the writes fail with EPIPE. That run exited 0 before and still does.
    • The errno cannot be relied on. stdio may drop the data of a failed write: musl does, and glibc does when stdout is line-buffered or unbuffered. The final fflush() then succeeds, only ferror() is left, and the EPIPE is gone. With default buffering the same thing happens when the output is a multiple of BUFSIZ.
    • That synthetic EIO can also hide real data loss on a live reader: EAGAIN on a full non-blocking pipe (stdout shared with a parent that set O_NONBLOCK), or a socket error. So EIO alone is not enough to exempt the run.
    • The rule is in stdout_reader_gone(). It runs before fclose(), while the fd is still open. The output must be stdout, or an --out-file that is the same file as fd 1 (by st_dev/st_ino, e.g. /dev/stdout), and it must be a pipe or socket (fstat). Then:
      • EPIPE or ECONNRESET means the reader is gone, so the run is not failed.
      • For EIO, the kernel decides: poll(POLLOUT, 0) reports POLLERR on a pipe with no reader and POLLHUP on a closed socket. A full pipe with a live reader reports neither, so the run fails.
    • Every other case fails the run: regular files, devices, a closed stdout (EBADF), a live reader that lost data, any other --out-file (e.g. a FIFO whose reader exits), and --json-out-file.
  • The output check must stay after the last write to the results file; a comment in main() says so.
  • README documents the new exit-status rule under "Interrupting a run" / "Output write errors", including that a connection-stage abort still exits 2 when an output write also fails. It also says what is not covered yet: the HDR histogram files, the --client-stats CSV files and --failed-keys-file still close unchecked. That is left for a follow-up. --help and the man page (generated from --help) have no exit-status section, so they are unchanged.
  • Nits deferred from the Fail at startup on an unwritable --out-file; open output files only after option validation #717 review: the long comment above the output-file opens is shortened, the truncate-after-JSON note now sits next to the ftruncate() call, and the "allways" typo is fixed.

Tests

The PR evolved in three steps, all on this branch:

  • first version (009624d)
  • file-type exemption (4f644df): a gone reader is recognised by the file type, not by an EPIPE errno that may be lost.
  • poll check (efac7e3): the synthetic EIO is exempt only when poll() reports that the reader is gone.

468f3af, ef2dcd2 and 33f437e add only tests, comments and README text. There are 11 new test functions in tests/tests_oss_simple_flow.py. Tests skip when /dev/full, stdbuf or /dev/stdout is missing. stdbuf runs set ASAN_OPTIONS=verify_asan_link_order=0 so ASan builds accept its LD_PRELOAD.

test guards master e09ee54 this PR
test_out_file_write_error (--out-file /dev/full) the write check FAIL (rc 0) PASS
test_json_out_file_write_error (--json-out-file /dev/full) the JSON close check FAIL (rc 0) PASS
test_stdout_write_error (stdout on /dev/full) the stdout check FAIL (rc 0) PASS
test_connection_stage_abort_keeps_rc_2_on_write_error (wrong password, --connection-stage-timeout=1, --out-file /dev/full: rc 2 and the output error printed) rc 2 takes precedence FAIL (no output error) PASS
test_out_file_fifo_reader_exits_is_an_error (--out-file FIFO with --test-time 2; the reader is closed once memtier prints Writing results to FIFO..., i.e. after its open(): rc 1, Broken pipe) the stdout identity check FAIL (rc 0) PASS
test_stdout_closed_pipe_is_not_an_error (stdout pipe, reader already closed) EPIPE exemption PASS PASS
test_stdout_closed_pipe_unbuffered_is_not_an_error (same under stdbuf -o0 / -oL) lost-EPIPE handling PASS PASS
test_stdout_reader_exits_early (stdbuf -oL memtier | head -n 1). A best-effort smoke test: it is timing-dependent and caught the first version's bug in 27/30 runs. lost-EPIPE handling PASS
test_out_file_dev_stdout_closed_pipe_is_not_an_error (--out-file /dev/stdout, closed pipe) same-file-as-stdout rule PASS PASS
test_stdout_closed_socket_is_not_an_error (stdout socketpair with closed peer, and a TCP connection reset by its peer, with default buffering and stdbuf -o0) S_ISSOCK, ECONNRESET PASS PASS
test_stdout_full_nonblocking_pipe_is_an_error (full O_NONBLOCK stdout pipe with a live reader, default buffering (EAGAIN) and stdbuf -oL (synthetic EIO): rc 1) errno filter, poll check FAIL (rc 0) PASS

The TCP-reset case binds an ephemeral 127.0.0.1 port, so it cannot collide with other tests.

Mutation check (--env oss). I ran the 4 newest tests against mutants of the final code, and each mutant is killed:

mutant killed by
no stdout identity check (any FIFO/socket --out-file exempt) test_out_file_fifo_reader_exits_is_an_error
no errno filter (any error on a stdout pipe exempt) test_stdout_full_nonblocking_pipe_is_an_error
ECONNRESET not exempt test_stdout_closed_socket_is_not_an_error
S_ISSOCK dropped test_stdout_closed_socket_is_not_an_error
output failure returns 1 before the connection-stage abort returns 2 test_connection_stage_abort_keeps_rc_2_on_write_error

Earlier steps of this PR, also measured:

  • The first version fails test_stdout_closed_pipe_unbuffered_is_not_an_error, test_stdout_reader_exits_early and test_out_file_dev_stdout_closed_pipe_is_not_an_error in 3/3 runs.
  • The file-type exemption fails test_stdout_full_nonblocking_pipe_is_an_error (stdbuf case) in 3/3 runs.

Rates:

  • All 11 tests pass in 8/8 runs on --env oss and in 3/3 runs with an ASan build (--enable-sanitizers, detect_leaks=1, no sanitizer reports). They also pass on --env oss-cluster --shards-count 3.
  • The FIFO test still passes with a simulated 1.5 s startup delay. That delay is a local check only, not in the PR; under it the old fixed-sleep version would close the reader before memtier's open().
  • All of tests_oss_simple_flow.py passes (58/58). The 11 tests pass again after rebasing on cde91ea.

Manual matrix with this PR (the first version's result in brackets):

stdbuf -oL memtier ... | head -2         rc=0   (first version: rc=1 Input/output error)
stdbuf -o0 memtier ... | true            rc=0   (first version: rc=1 Input/output error)
memtier ... --out-file /dev/stdout | true  rc=0 (first version: rc=1 Broken pipe)
memtier ... > /dev/full                  rc=1 error: failed writing results to stdout: No space left on device
stdbuf -o0 memtier ... > /dev/full       rc=1 error: failed writing results to stdout: Input/output error
memtier ... --out-file /dev/full         rc=1 error: failed writing --out-file /dev/full: No space left on device
memtier ... --json-out-file /dev/full    rc=1 error: failed writing --json-out-file /dev/full: No space left on device
memtier ... >&-                          rc=1 error: failed writing results to stdout: Bad file descriptor

macOS: CI there only builds and runs make check, and the stdbuf tests skip. BSD stdio keeps unwritten data in the buffer after a failed write, so the final fflush() reports EPIPE again and the EIO/poll() branch is not normally reached. POLLERR on a pipe without a reader is Linux behaviour; neither POSIX nor macOS poll(2) documents it. If the EIO branch is reached on macOS and poll() reports neither POLLERR nor POLLHUP, the run fails (rc 1). That is the safe direction: data loss is never hidden. Not checked by hand on macOS.

Release notes

  • memtier_benchmark now exits with status 1 and prints error: failed writing --out-file PATH: REASON (or --json-out-file PATH, or results to stdout) when the results cannot be written, for example on a full disk. This also applies after a graceful Ctrl+C/SIGTERM. Before, such a run exited 0 and left a missing or truncated results file.
  • If stdout (or an --out-file that is stdout, such as /dev/stdout) is a pipe or socket whose reader has gone away (e.g. | head), the run still exits 0.
  • A run whose stdout is a non-blocking pipe that fills up while its reader is still alive (for example under some CI runners or a Node.js parent that shares the pipe) used to exit 0 with part of the results silently missing. It now exits 1 with Resource temporarily unavailable (or Input/output error with line-buffered stdout). The output really was incomplete; if a CI job starts failing this way, write the results with --out-file instead.
  • Minor visible change: stdout is now flushed before Saving JSON output file / Json file closed. are printed, so with 2>&1 the report lines now come before those two stderr lines.
  • A run with stdout closed (>&-) and the results going to stdout now exits 1 (Bad file descriptor); it used to exit 0.

Affected versions

1.0.0 (--out-file) / 1.2.9 (--json-out-file) through 2.5.2 (latest) and master.

Fixes #719

🤖 Generated with Claude Code


Note

Medium Risk
Changes exit-code behavior for scripts and CI that assumed exit 0 on write failures; the stdout exemption logic is subtle but well-tested.

Overview
memtier_benchmark now fails the run when results cannot be written, instead of exiting 0 with missing or truncated output. A shared finish_output_file() flushes and optionally closes output streams and surfaces write/close errors; main() uses it for --out-file and stdout, and json_handler::close() uses it for --json-out-file (logging Json file closed. only on success). Failures print a clear stderr message and return exit code 1, including after graceful interrupt; connection-stage abort still returns 2 first.

The stdout pipe/socket reader-gone case (| head, etc.) stays exit 0 via stdout_reader_gone(): stdout or --out-file that is the same inode as stdout, on a pipe/socket, with EPIPE/ECONNRESET, or synthetic EIO only when poll() shows the reader is gone. Other paths (regular files, FIFOs, full live pipes) still fail.

README documents output write errors and exit status. 12 integration tests cover /dev/full, FIFO reader exit, closed pipes/sockets, and non-blocking full pipes.

Reviewed by Cursor Bugbot for commit ef2dcd2. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. I went through the diff against master's teardown in main(). The core fix looks right to me. Nothing writes to outfile after the new check, the JSON close now happens before delete jsonhandler with its result checked, and the early-exit paths still close the file through the destructor as they did before. Each rule has a test that fails on master, which is the bar this repo has met before for output fixes (e.g. #364). A few small points:

  • Compat: the new exit 1 only happens when results really were lost, and the | head case still exits 0, so I think this is the less-breaking choice. The full O_NONBLOCK stdout pipe is the one case I'd expect a real user to hit, e.g. under a CI runner or a Node parent. Those runs used to exit 0 with output silently missing and will now exit 1 with Resource temporarily unavailable. I agree that's the honest result. I just wonder whether it deserves its own line in the release notes, since "my CI job started failing" is how people will find it.
  • Did you consider retrying on EAGAIN (poll for POLLOUT, then write again) instead of failing the run there? That would deliver the results instead of reporting them lost. I think it's fine to leave it out of this PR. I'm asking because it would be a less-breaking option for that one case.
  • The README says HDR, --client-stats and --failed-keys-file writes aren't checked yet. Since finish_output_file() is now a shared helper, it looks easy to reuse there in a follow-up. Is that planned?
  • Nitpicking: stdout_reader_gone() is only about the output stream, so it might sit better next to finish_output_file() in file_io.cpp than in memtier_benchmark.cpp. Not a big deal either way.

This looks good to merge to me once the release-note point is settled. The EAGAIN question doesn't need to block it.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6614b92. Configure here.

Comment thread file_io.cpp Resolved
@fcostaoliveira
fcostaoliveira force-pushed the fix/719-output-write-errors branch from 6614b92 to b1f2969 Compare October 16, 2026 18:22
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Good catch. It's real, and it isn't only a BSD issue: glibc already drops the data of a failed write when stdout is line-buffered or unbuffered. In 6614b92, stdbuf -oL memtier ... | head -2 and stdbuf -o0 memtier ... | true exited 1 with "Input/output error", and with default buffering the same happened when the output was a multiple of BUFSIZ.

Fixed in b1f2969. The exemption no longer depends on the errno. A gone reader is now recognised when the output is stdout (or an --out-file that is the same file as fd 1, such as /dev/stdout), it is a pipe or socket (fstat), and the error is EPIPE or the unknown-cause EIO. A pipe or socket write never fails with EIO on its own.

Regular files, devices and any other --out-file still fail the run. New tests cover a pre-closed pipe under stdbuf -o0 and -oL, stdbuf -oL memtier | head -n 1, and --out-file /dev/stdout. All three fail on 6614b92 and pass now, so they also guard against the libc buffering behaviour on any platform where stdbuf exists.

@fcostaoliveira
fcostaoliveira force-pushed the fix/719-output-write-errors branch from b1f2969 to bd11821 Compare October 16, 2026 18:46
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Addressed in bd11821, rebased on e09ee54:

  • test_stdout_reader_exits_early: you're right that it can pass without a write ever failing. I kept it, but the docstring now says it is a best-effort smoke test (it still caught the lost-EPIPE bug in 27 of 30 runs) and that the closed-pipe tests are the deterministic guard.
  • test_stdout_closed_pipe_is_not_an_error now uses _closed_pipe_run().
  • Nothing writes to outfile/stdout after the check. The data-verification output is the last write. I added a comment in main() saying the check has to stay after the last write to the results file.

Also in this push: the stdout-pipe exemption for the synthetic EIO now requires poll(POLLOUT) to report POLLERR/POLLHUP, i.e. the reader is really gone. Before, a full non-blocking stdout pipe with a live reader lost the data and exited 0 under stdbuf -oL. That case now exits 1, and test_stdout_full_nonblocking_pipe_is_an_error covers it.

@fcostaoliveira fcostaoliveira changed the title Fail the run when --out-file or --json-out-file cannot be written Fail the run on write errors to --out-file, --json-out-file or stdout Oct 16, 2026
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Addressed in e5f3683, rebased on e09ee54:

  • macOS poll(): I didn't check this by hand; I have no macOS box. As you say, macOS CI only builds and runs make check, and the stdbuf tests skip there, so only the plain EPIPE path runs.
    • BSD-derived stdio should keep the unwritten data in the buffer after a failed write. The final fflush() then reports EPIPE again, and the EIO/poll() branch is not normally reached on macOS. That branch is exercised on glibc and musl, which drop the data.
    • POLLERR on the write end of a pipe without a reader is Linux behaviour. Neither POSIX nor macOS poll(2) documents it.
    • If the EIO branch is reached on macOS and poll() reports neither POLLERR nor POLLHUP, the fallback is that the run fails with rc 1. A | head there could then exit 1, but data loss is never hidden. A second look from someone with a macOS box is still welcome.
  • Writes after the check: no. The data-verification output is the last write to outfile/stdout, and nothing returns between it and the check. The comment stays.
  • _closed_pipe_run() and the blank lines: the helper is now above its first caller (with _start_memtier()/_finish_memtier()), the extra blank lines are gone, and fcntl is a module-level import.
  • Closed stdout (>&-) exiting 1: this is listed in the release notes.

New tests, each checked against a mutant that it kills:

  • an --out-file FIFO whose reader exits gives rc 1 (identity check);
  • a full non-blocking stdout pipe with a live reader gives rc 1 with default buffering too, not only under stdbuf (errno filter);
  • a stdout socketpair with a closed peer, and a TCP connection reset by its peer, give rc 0 (S_ISSOCK, ECONNRESET);
  • a connection-stage abort with --out-file /dev/full gives rc 2 and still prints the output error.

fcostaoliveira and others added 5 commits October 16, 2026 21:03
main() closed the results file with an unchecked fclose(), and
~json_handler() did the same for the JSON file and then logged
"Json file closed.". A buffered ENOSPC/EDQUOT/EIO was lost, so a run
whose results were missing or truncated exited 0, and scripts that
check the exit code accepted it.

Flush and close both files through a new finish_output_file() helper
that checks fflush(), ferror() and fclose(). json_handler gets a
close() method that returns the error; main() calls it instead of
relying on the destructor. On failure memtier prints
"error: failed writing --out-file PATH: REASON" (or --json-out-file)
and exits 1, the code already used for a run that did not complete.

The default stdout output is checked too (`> /dev/full` exited 0), but
EPIPE on stdout is not an error: SIGPIPE is ignored, so `| head` makes
the writes fail with EPIPE, and that run used to and still exits 0.
An explicit --out-file is held to the stricter rule.

Also tidy two nits from the #717 review: move the
truncate-after-JSON note next to the ftruncate() call, and fix the
"allways" typo.

Fixes #719

Co-Authored-By: Claude Opus 5.5 <[email protected]>
When stdout is line-buffered or unbuffered (stdbuf -oL / -o0), glibc
drops the data of a failed write. The final fflush() then succeeds,
only ferror() is set, finish_output_file() returns EIO, and the EPIPE
exemption missed: `stdbuf -oL memtier | head` exited 1 with
"Input/output error". With default buffering this happened whenever
the output was a multiple of BUFSIZ.

Decide "the reader went away" from the file type instead: stdout (or
an --out-file that is the same file as stdout, e.g. /dev/stdout) on a
pipe or socket, with EPIPE or the unknown-cause EIO. A pipe or socket
write never fails with EIO itself. Regular files, devices and any
other --out-file still fail the run.

Document the output-write exit status in README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
finish_output_file() returns a synthetic EIO when stdio dropped the
data of a failed write. Its real cause is not only EPIPE: it can be
EAGAIN on a full non-blocking pipe (stdout shared with a parent that
set O_NONBLOCK) or a socket error, all with a live reader. Exempting
every EIO on a stdout pipe or socket hid that data loss: under
stdbuf -oL such a run exited 0 with nothing delivered.

For EIO, ask the kernel instead: poll(POLLOUT) reports POLLERR on a
pipe with no reader and POLLHUP on a closed socket, and neither on a
full pipe whose reader is alive. EPIPE and ECONNRESET stay exempt. The
check now runs before fclose(), while the fd is open.

Tests: a full non-blocking stdout pipe with a live reader must exit 1;
the closed-pipe test reuses _closed_pipe_run(); the `| head` test is
marked as a best-effort smoke test. README notes the live-reader case
and that a connection-stage abort still exits 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
New tests: an --out-file FIFO whose reader exits fails the run; a full
non-blocking stdout pipe with a live reader fails with default
buffering too (EAGAIN), not only under stdbuf; a stdout socket whose
peer closed (socketpair) or reset (TCP RST) is not an error; and a
connection-stage abort still exits 2 when the output write fails.

Move the test helpers above their callers and fcntl to the module
imports. Note that musl's stdio also drops the data of a failed write.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
test_out_file_fifo_reader_exits_is_an_error closed the FIFO's read end
after a fixed 0.7 s. On a slow start (e.g. a sanitizer build) that can
come before memtier's open(), which then waits for a reader forever.
Wait for the "Writing results to FIFO..." line, printed after the open,
instead. memtier's stderr now goes to a file so it can be polled, and
the helpers clean up their temporary directories.

Explain in stdout_reader_gone() why a full non-blocking stdout pipe
happens in practice.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/719-output-write-errors branch from e5f3683 to ef2dcd2 Compare October 16, 2026 20:03
The background on lost errnos lives in the commit messages; keep the
comment to the rule and the O_NONBLOCK case. README now says that the
HDR histogram, --client-stats and --failed-keys-file writes are not
checked yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Addressed in ef2dcd2 and 33f437e (head 33f437e, rebased on cde91ea). Point by point:

  • FIFO test race: fixed. test_out_file_fifo_reader_exits_is_an_error no longer closes the reader after a fixed 0.7 s. memtier's stderr now goes to a file, and the test waits (with a 30 s timeout) for Writing results to FIFO..., which memtier prints after its open() succeeds. Only then does it close the read end.
    • With a simulated 1.5 s startup delay (a local check, not in the PR), the test still passes; the old fixed sleep would have closed the reader before memtier's open().
    • All 11 tests also pass 3/3 on an ASan build (--enable-sanitizers, detect_leaks=1, no sanitizer reports).
    • _start_memtier() and the FIFO test now use TemporaryDirectory, so they leave nothing behind.
  • Writes to the output before the final report: none to outfile during --test-time. Progress lines go to stderr. The first fprintf(outfile, ...) is the summary after the run loop, and the data-verification lines are the last write. The one early stdout write is the Prometheus exporter listening on ... line with --prometheus-port. It goes to stdout, not to outfile. When the results also go to stdout, a failure of that write sets ferror(stdout) and is caught by the same final check, with the same gone-reader rule.
  • Scope (HDR, --client-stats, --failed-keys-file): yes, these are left for a follow-up. Their fclose() calls are still unchecked (run_stats.cpp save_hdr_percentiles_print_format() / save_hdr_log_format() / save_csv(), and retry_policy.cpp). The README "Output write errors" section now says so explicitly.
  • Exit code 1: deliberate. rc 1 already means "the run did not produce the results you asked for" (g_worker_failed), and a new code is extra surface for scripts to learn. stderr names the cause. rc 2 still wins for a connection-stage abort.
  • poll() returning -1: agreed. It falls through to "not gone", so the run fails, which is the safe direction. No change.
  • Long comment: shortened to six lines (the rule plus the O_NONBLOCK case: a parent such as Node or a CI runner sharing the pipe description). The glibc/musl background is in the commit messages.
  • Description: the test count is now 11 test functions, the table has a "this PR" column, and the commit references point to the rebased SHAs.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Answering the 20:08 version of the review:

  • Release note for the non-blocking stdout pipe: agreed. The release notes now have their own line for it: a full O_NONBLOCK stdout pipe with a live reader now exits 1 with Resource temporarily unavailable, where it used to exit 0 with output silently missing. The line also suggests --out-file for CI jobs that hit this.
  • Retrying on EAGAIN: considered, and left out on purpose. stdio has already dropped the data by the time fflush() reports the error, so a retry at the end can't recover it. A real fix would mean switching stdout to blocking mode, or buffering every result write ourselves. Both are larger changes than this PR, and both would change behaviour for parents that rely on the non-blocking mode. Reporting the loss honestly is the safe minimal step.
  • HDR, --client-stats and --failed-keys-file writes: yes, that's planned as a follow-up that reuses finish_output_file(). It's being filed as its own issue.
  • Moving stdout_reader_gone() next to finish_output_file(): fair. I'll leave it in memtier_benchmark.cpp for now, because it depends on main()'s knowledge of which stream is stdout. The follow-up can move it if it gains another caller.

@fcostaoliveira
fcostaoliveira merged commit 2285b02 into master Oct 16, 2026
129 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
When memtier was started with stdout (or stderr) closed, the fopen() of
--json-out-file got fd 1 (or 2). The text results (or the stderr log
lines) were then written into the JSON file, which became invalid, and
the run still exited 0. The same applied to --out-file and any other
file opened while 0-2 were free.

main() now reopens any closed fd 0-2 on /dev/null before it does
anything else, the usual idiom for this. A closed stderr only loses the
log lines. A closed stdout loses the text results, so if no --out-file
was given the run now fails with exit 1 and an error on stderr instead
of reporting success for results that went nowhere. The JSON file stays
valid in every case.

An --out-file, --json-out-file or --failed-keys-file that names a
closed fd (/dev/stdout, /dev/fd/1, /proc/self/fd/1, or a symlink to one
of them; likewise for stdin and stderr) would now resolve to the
reopened /dev/null and lose its output with exit 0, so it is rejected at
startup with exit 1, before any of those files is opened. The path is
followed one symlink at a time, because an inode comparison cannot tell
it from /dev/null itself, which is accepted however it is spelled.
Another process's fd (/proc/1/fd/1) is not matched. The closed-stdout
error is printed before the other exit-status checks, so it also shows
when a connection-stage abort exits 2.

After #729 a write to the reopened /dev/null succeeds, so its write-error
check no longer sees a closed stdout; the closed-stdout rule here covers
that case with the same exit code.

The new tests also run in the asan and tsan "misc" cells.

Fixes #733

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
When memtier was started with stdout (or stderr) closed, the fopen() of
--json-out-file got fd 1 (or 2). The text results (or the stderr log
lines) were then written into the JSON file, which became invalid, and
the run still exited 0. The same applied to --out-file and any other
file opened while 0-2 were free.

main() now reopens any closed fd 0-2 on /dev/null before it does
anything else, the usual idiom for this. A closed stderr only loses the
log lines. A closed stdout loses the text results, so if no --out-file
was given the run now fails with exit 1 and an error on stderr instead
of reporting success for results that went nowhere. The JSON file stays
valid in every case.

An --out-file, --json-out-file or --failed-keys-file that names a
closed fd (/dev/stdout, /dev/fd/1, /proc/self/fd/1, or a symlink to one
of them; likewise for stdin and stderr) would now resolve to the
reopened /dev/null and lose its output with exit 0, so it is rejected at
startup with exit 1, before any of those files is opened. The path is
followed one symlink at a time, because an inode comparison cannot tell
it from /dev/null itself, which is accepted however it is spelled.
Another process's fd (/proc/1/fd/1) is not matched. The closed-stdout
error is printed before the other exit-status checks, so it also shows
when a connection-stage abort exits 2.

After #729 a write to the reopened /dev/null succeeds, so its write-error
check no longer sees a closed stdout; the closed-stdout rule here covers
that case with the same exit code.

The new tests also run in the asan and tsan "misc" cells.

Fixes #733

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
When memtier was started with stdout (or stderr) closed, the fopen() of
--json-out-file got fd 1 (or 2). The text results (or the stderr log
lines) were then written into the JSON file, which became invalid, and
the run still exited 0. The same applied to --out-file and any other
file opened while 0-2 were free.

main() now reopens any closed fd 0-2 on /dev/null before it does
anything else, the usual idiom for this. A closed stdin or stderr does
not fail the run by itself.

With stdout closed, the text results would now go to /dev/null. So if
no --out-file is given, memtier fails at startup with exit 1 and an
error, before connecting or opening any output file, like an --out-file
that cannot be opened. After #729 a write to the reopened /dev/null
succeeds, so its write-error check would no longer catch this.

An --out-file, --json-out-file or --failed-keys-file that names a
closed fd (/dev/stdout, /dev/fd/1, /proc/self/fd/1, or a symlink to one
of them; likewise for stdin and stderr) would now resolve to the
reopened /dev/null and lose its output with exit 0, where master failed
with "cannot open". It is rejected at startup with exit 1 too. The path
is followed one symlink at a time, because an inode comparison cannot
tell it from /dev/null itself, which is accepted however it is spelled.
Another process's fd (/proc/1/fd/1) is not matched.

The new tests also run in the asan and tsan "misc" cells.

Fixes #733

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
When memtier was started with stdout (or stderr) closed, the fopen() of
--json-out-file got fd 1 (or 2). The text results (or the stderr log
lines) were then written into the JSON file, which became invalid, and
the run still exited 0. The same applied to --out-file and any other
file opened while 0-2 were free.

main() now reopens any closed fd 0-2 on /dev/null before it does
anything else, the usual idiom for this. A closed stdin or stderr does
not fail the run by itself.

With stdout closed, the text results would now go to /dev/null. So if
no --out-file is given, memtier fails at startup with exit 1 and an
error, after the option checks and before connecting or opening any
output file, like an --out-file that cannot be opened. After #729 a write to the reopened /dev/null
succeeds, so its write-error check would no longer catch this.

An --out-file, --json-out-file or --failed-keys-file that names a
closed fd (/dev/stdout, /dev/fd/1, /proc/self/fd/1, or a symlink to one
of them; likewise for stdin and stderr) would now resolve to the
reopened /dev/null and lose its output with exit 0, where master failed
with "cannot open". It is rejected at startup with exit 1 too. The path
is followed one symlink at a time, because an inode comparison cannot
tell it from /dev/null itself, which is accepted however it is spelled.
Another process's fd (/proc/1/fd/1) is not matched.

The new tests also run in the asan and tsan "misc" cells.

Fixes #733

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
When memtier was started with stdout (or stderr) closed, the fopen() of
--json-out-file got fd 1 (or 2). The text results (or the stderr log
lines) were then written into the JSON file, which became invalid, and
the run still exited 0. The same applied to --out-file and any other
file opened while 0-2 were free.

main() now reopens any closed fd 0-2 on /dev/null before it does
anything else, the usual idiom for this. A closed stdin or stderr does
not fail the run by itself.

With stdout closed, the text results would now go to /dev/null. So if
no --out-file is given, memtier fails at startup with exit 1 and an
error, after the option checks and before connecting or opening any
output file, like an --out-file that cannot be opened. After #729 a write to the reopened /dev/null
succeeds, so its write-error check would no longer catch this.

An --out-file, --json-out-file or --failed-keys-file that names a
closed fd (/dev/stdout, /dev/fd/1, /proc/self/fd/1, or a symlink to one
of them; likewise for stdin and stderr) would now resolve to the
reopened /dev/null and lose its output with exit 0, where master failed
with "cannot open". It is rejected at startup with exit 1 too. The path
is followed one symlink at a time, because an inode comparison cannot
tell it from /dev/null itself, which is accepted however it is spelled.
Another process's fd (/proc/1/fd/1) is not matched. The same check
covers the input paths --data-import, --monitor-input, --cert, --key and
--cacert: with stdin closed, /dev/stdin would now read an empty
/dev/null. All path checks run right after option parsing, before any
of these files is opened or read.

The new tests also run in the asan and tsan "misc" cells.

Fixes #733

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 17, 2026
…-file

The --hdr-file-prefix files, the --client-stats CSVs and
--failed-keys-file were written without checking for errors: the HDR
writers and save_csv() ignored fclose(), and the failed-keys logger
checked fprintf() (which only fills the stdio buffer) but not the
fflush() that does the write. On a full disk, quota or I/O error the
files ended up empty or truncated, nothing was printed and memtier
exited 0. A file that could not be opened was reported but also exited 0.

These files now follow the rule #729 set for --out-file and
--json-out-file. They are flushed and checked with ferror() through
finish_output_file(), and a failure prints "error: failed writing
--hdr-file-prefix file PATH: REASON" (or --client-stats file, or
--failed-keys-file PATH) or "error: cannot open ...". The other files
are still written, and memtier exits 1 (a connection-stage abort keeps
exit code 2). g_output_failed replaces main()'s local output_failed so
run_benchmark() can set it for the CSVs.

--failed-keys-file is fatal too, open failure included: it is an
output that was asked for, and a lost log with exit 0 is what #719
fixed for the other outputs. The run is not cut short: as before,
logging stops and the benchmark goes on; the exit code reports it.
stdout_reader_gone() moves to file_io.cpp so a --failed-keys-file on
stdout whose reader went away (/dev/stdout and `| head`) gets the same
exemption as the results on stdout.

Release notes: such runs now exit 1 instead of 0, including an HDR or
--client-stats path in a missing directory and a --failed-keys-file
that cannot be opened. That message is now "error: cannot open
--failed-keys-file PATH: REASON; logging disabled." instead of a
warning.

Tests: /dev/full symlinks for the full-run and per-command .txt and
.hgrm files and for a --client-stats CSV, --failed-keys-file /dev/full
with SETs that fail with -OOM, and a --failed-keys-file on a closed
stdout pipe.

Fixes #744

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 17, 2026
…-file

The --hdr-file-prefix files, the --client-stats CSVs and
--failed-keys-file were written without checking for errors: the HDR
writers and save_csv() ignored fclose(), and the failed-keys logger
checked fprintf() (which only fills the stdio buffer) but not the
fflush() that does the write. On a full disk, quota or I/O error the
files ended up empty or truncated, nothing was printed and memtier
exited 0. A file that could not be opened was reported but also exited 0.

These files now follow the rule #729 set for --out-file and
--json-out-file. They are flushed and checked with ferror() through
finish_output_file(), and a failure prints "error: failed writing
--hdr-file-prefix file PATH: REASON" (or --client-stats file, or
--failed-keys-file PATH) or "error: cannot open ...". The other files
are still written, and memtier exits 1 (a connection-stage abort keeps
exit code 2). g_output_failed replaces main()'s local output_failed so
run_benchmark() can set it for the CSVs.

--failed-keys-file is fatal too, open failure included: it is an
output that was asked for, and a lost log with exit 0 is what #719
fixed for the other outputs. The run is not cut short: as before,
logging stops and the benchmark goes on; the exit code reports it.
stdout_reader_gone() moves to file_io.cpp so a --failed-keys-file on
stdout whose reader went away (/dev/stdout and `| head`) gets the same
exemption as the results on stdout.

Release notes: such runs now exit 1 instead of 0, including an HDR or
--client-stats path in a missing directory and a --failed-keys-file
that cannot be opened. That message is now "error: cannot open
--failed-keys-file PATH: REASON; logging disabled." instead of a
warning.

Tests: /dev/full symlinks for the full-run and per-command .txt and
.hgrm files and for a --client-stats CSV, --failed-keys-file /dev/full
with SETs that fail with -OOM, and a --failed-keys-file on a closed
stdout pipe.

Fixes #744

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 17, 2026
…-file

The --hdr-file-prefix files, the --client-stats CSVs and
--failed-keys-file were written without checking for errors: the HDR
writers and save_csv() ignored fclose(), and the failed-keys logger
checked fprintf() (which only fills the stdio buffer) but not the
fflush() that does the write. On a full disk, quota or I/O error the
files ended up empty or truncated, nothing was printed and memtier
exited 0. A file that could not be opened was reported but also exited 0.

These files now follow the rule #729 set for --out-file and
--json-out-file. They are flushed and checked with ferror() through
finish_output_file(), and a failure prints "error: failed writing
--hdr-file-prefix file PATH: REASON" (or --client-stats file, or
--failed-keys-file PATH) or "error: cannot open ...". The other files
are still written, and memtier exits 1 (a connection-stage abort keeps
exit code 2). g_output_failed replaces main()'s local output_failed so
run_benchmark() can set it for the CSVs.

--failed-keys-file is fatal too, open failure included: it is an
output that was asked for, and a lost log with exit 0 is what #719
fixed for the other outputs. The run is not cut short: as before,
logging stops and the benchmark goes on; the exit code reports it.
stdout_reader_gone() moves to file_io.cpp so a --failed-keys-file on
stdout whose reader went away (/dev/stdout and `| head`) gets the same
exemption as the results on stdout.

Release notes: such runs now exit 1 instead of 0, including an HDR or
--client-stats path in a missing directory and a --failed-keys-file
that cannot be opened. That message is now "error: cannot open
--failed-keys-file PATH: REASON; logging disabled." instead of a
warning.

Tests: /dev/full symlinks for the full-run and per-command .txt and
.hgrm files and for a --client-stats CSV, --failed-keys-file /dev/full
with SETs that fail with -OOM, and a --failed-keys-file on a closed
stdout pipe.

Fixes #744

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 17, 2026
…-file

The --hdr-file-prefix files, the --client-stats CSVs and
--failed-keys-file were written without checking for errors: the HDR
writers and save_csv() ignored fclose(), and the failed-keys logger
checked fprintf() (which only fills the stdio buffer) but not the
fflush() that does the write. On a full disk, quota or I/O error the
files ended up empty or truncated, nothing was printed and memtier
exited 0. A file that could not be opened was reported but also exited 0.

These files now follow the rule #729 set for --out-file and
--json-out-file. They are flushed and checked with ferror() through
finish_output_file(), and a failure prints "error: failed writing
--hdr-file-prefix file PATH: REASON" (or --client-stats file, or
--failed-keys-file PATH) or "error: cannot open ...". The other files
are still written, and memtier exits 1 (a connection-stage abort keeps
exit code 2). g_output_failed replaces main()'s local output_failed so
run_benchmark() can set it for the CSVs.

--failed-keys-file is fatal too, open failure included: it is an
output that was asked for, and a lost log with exit 0 is what #719
fixed for the other outputs. The run is not cut short: as before,
logging stops and the benchmark goes on; the exit code reports it.
stdout_reader_gone() moves to file_io.cpp so a --failed-keys-file on
stdout whose reader went away (/dev/stdout and `| head`) gets the same
exemption as the results on stdout.

Release notes: such runs now exit 1 instead of 0, including an HDR or
--client-stats path in a missing directory and a --failed-keys-file
that cannot be opened. That message is now "error: cannot open
--failed-keys-file PATH: REASON; logging disabled." instead of a
warning.

Tests: /dev/full symlinks for the full-run and per-command .txt and
.hgrm files and for a --client-stats CSV, --failed-keys-file /dev/full
with SETs that fail with -OOM, and a --failed-keys-file on a closed
stdout pipe.

Fixes #744

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

Write errors on --out-file and --json-out-file are ignored: a run whose results could not be written exits 0

1 participant