Repository navigation
Fail at startup on an unwritable --out-file; open output files only after option validation - #717
Conversation
|
🤖 Automated first-pass review. A human maintainer still needs to review this before merge. Thanks, I read through the whole thing. Moving both opens below the validation looks right to me. Nothing reads A few small things, none blocking:
The core fix looks solid to me. (I'd file this as an approval-leaning comment, not a block.) |
|
Thanks for the review. Point by point:
|
main() only perror()'d a failed fopen() of --out-file and carried on with outfile == NULL. The whole benchmark ran (the full --test-time), and then the first fprintf(outfile, ...) of the results crashed with SIGSEGV (rc 139), losing the run and printing the crash-handler bug report. Treat it as a fatal configuration error, the same way #706 now handles an unopenable --json-out-file: print "error: cannot open --out-file PATH: REASON" and exit 1. The open still happens before any connection or thread is started, so no run is wasted. fopen() is retried on EINTR, as json_handler does. Regression test test_out_file_unwritable in tests_oss_simple_flow.py uses a path in a non-existent directory with --test-time 60 and expects exit 1, the message, no "Launching threads", and an exit within 20 s. Fixes #716 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ntact json_handler truncates --json-out-file and writes the config as soon as it is constructed, so failing on --out-file after that left a valid JSON file from an earlier run replaced by a partial one. Open --out-file just before the JSON handler instead. It is still before any connection or thread starts. A usage error found after this point now leaves an empty --out-file behind, which is empty rather than corrupt. The test now also checks that an existing --json-out-file is untouched, and asserts the full message including the strerror() reason. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Opening the output files early meant that any later startup error (usage(), a failed data-import, ...) had already truncated an existing --out-file and replaced an existing --json-out-file with an unterminated config block. A rerun with a typo destroyed the previous results. Move both opens to just before the run, after all option validation and the data-import loading. --out-file goes first, so its open error still leaves the JSON file untouched. No connection or worker thread has started at that point. The only visible change in the JSON "configuration" block: with a Zipf key pattern and no --key-zipf-exp, "key_zipf_exp" now shows the value actually used (1.0) instead of the unset 0.0. Tests cover the three #718 cases (--data-size with --data-size-list, -P memcache_text --select-db, a missing --data-import): each leaves an existing --out-file and --json-out-file byte-identical. Fixes #718 Co-Authored-By: Claude Opus 5.5 <[email protected]>
75b0c5e to
92ffb0b
Compare
|
Thanks. I went the way you suggested, in 92ffb0b. Rebased on bc7320a.
|
Opening --out-file with fopen("w") before the JSON handler truncated an
existing results file even when the --json-out-file open then failed and
memtier exited 1, so the previous results were lost.
Open --out-file with open(O_WRONLY | O_CREAT) and fdopen(), create the
json_handler, and only then ftruncate() the results file. ftruncate() is
done only for regular files, so /dev/stdout, /dev/null and FIFOs still
work. An error opening either file now leaves the other untouched, and
the stderr notes are back in their old order ("Json file ... created"
before "Writing results to ...").
New tests: an unwritable --json-out-file leaves an existing --out-file
byte-identical, and a successful run fully replaces a longer existing
--out-file and keeps the note order.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Point by point:
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ 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 c0f8842. Configure here.
If ftruncate() of --out-file failed (e.g. EIO), memtier exited after the json_handler had already written the config block, skipping its destructor and leaving an unterminated JSON file. Delete the handler before exiting so the nesting is closed and the JSON stays valid. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Point by point:
|
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]>
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]>
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]>

Decisions:
--out-filethat cannot be opened is now a fatal startup error (exit 1), matching what Handle SIGTERM like Ctrl+C so --json-out-file stays valid #706 did for--json-out-file. Previously memtier printed thefopenerror, ran the benchmark, and then crashed.--out-fileand--json-out-fileare now both opened only after all option validation, so a startup error leaves existing output files untouched.--out-fileis truncated only after the--json-out-filehas been opened, so an open error on either file leaves the other untouched.Problem
--out-file(for example, a missing directory or no permission), memtier printedPATH: No such file or directory, ran the whole benchmark (the full--test-time), and then crashed with SIGSEGV (rc 139) while writing the results.--json-out-filewas opened, truncated, and given the config block before most option validation. Anyusage()orexit(1)after that left a truncated, invalid JSON file in place of whatever was there before. A rerun with a typo destroyed the previous results.Root cause
fopen(cfg.out_file, "w")inmain()was onlyperror()'d.outfilestayed NULL until the firstfprintf(outfile, ...)after the run.new json_handler(cfg.json_out_file)andconfig_print_to_json()ran right after the TLS setup, before the getrlimit, protocol, data-size, key-range and data-import checks. Those exit directly, so~json_handler()never closed the object.Fix
Both opens now happen where
--out-fileused to be opened: after every usage check, theserver_addrDNS resolution and the data-import loading, and before any connection or worker thread starts.--out-fileis opened first, withopen(O_WRONLY | O_CREAT)+fdopen(), so it is not truncated yet.open()is retried on EINTR (as injson_handler), and on failure memtier printserror: cannot open --out-file PATH: REASONand callsexit(1). The JSON file has not been touched at that point.json_handleris created andconfig_print_to_json()runs. If the JSON open fails, memtier exits 1 and the existing--out-fileis left as it was.ftruncate()d. This is done only for regular files, so/dev/stdout,/dev/nulland FIFOs work as before.--out-filedid not exist before and the JSON open then fails, an empty--out-fileis left behind. An existing file is never modified.JSON
configurationblock. I compared the parsed block before and after this change for several option sets:Z:ZZ:Z --key-zipf-exp 0.5G:G --key-stddev 10--data-size-range--command-key-pattern=Z-t 2 -c 3 --ratio 1:3 --pipeline 4--data-import--key-prefix/--randomizeThe only difference: with a Zipf key pattern and no
--key-zipf-exp,"key_zipf_exp"is now1.000000, the exponent actually used, instead of the unset0.000000. Every other field is identical.A shared open/retry/error helper for
JSON_handler.cppandmemtier_benchmark.cppwas considered and declined: they are separate translation units, and it would need a new header for four lines.Not covered (pre-existing): the
--statsdclient and the--prometheus-portexporter are set up earlier inmain(), before validation. That is unchanged by this PR.Tests
All tests are in
tests/tests_oss_simple_flow.py, run with--env existing-envagainst a local redis:test_out_file_unwritable: rc 1, full message with: No such file or directory, noLaunching threads, < 20 s, existing JSON untouchedtest_json_out_file_unwritable_keeps_out_file: unwritable JSON path, existing--out-filebyte-identical--out-fileemptied)test_out_file_replaces_existing: a longer existing--out-fileis fully replaced, and stderr order is "Json file" then "Writing results"test_startup_error_keeps_output_files_data_size(--data-size 10 --data-size-list 10:1)test_startup_error_keeps_output_files_select_db(-P memcache_text --select-db 1)test_startup_error_keeps_output_files_data_import(missing--data-importfile)At 75b0c5e, the three
test_startup_error_*tests also fail, because both files were clobbered.test_json_out_file_unwritablepassed 35/35 across 5 repeated runs with this PR.--env oss-cluster --shards-count 3. In cluster mode the select-db and data-import cases fail earlier, with the cluster-mode message, and the test expects that.--out-file /dev/nulland--out-file /dev/stdoutstill work (rc 0).test_json_output_integrity.pypasses (17/17).test_sigterm_closes_json,test_sigterm_stops_run_count,test_interrupt_*andtest_valid_json_using_debug_command.test_json_out_file_unwritable,test_default_setandtest_default_set_getpass.Manual repro,
-n 10 -t 1 -c 1 --out-file /nonexistent-memtier-dir/o.txt:crashed by signal: 11 ... SIGSEGV, rc 139error: cannot open --out-file /nonexistent-memtier-dir/o.txt: No such file or directory, rc 1Fixes #716
Fixes #718
Release notes
--out-fileor--json-out-filenow fails at startup with an error and exit code 1. It no longer crashes, and no longer leaves an empty or invalid JSON file behind.--out-file.configuration: with a Zipf key pattern (Z) and no--key-zipf-exp,"key_zipf_exp"now reports the exponent actually used (1.000000), not the unset0.000000.Affected versions
#716: 1.0.0 through 2.5.2 (latest) and master.
fopen/perror/ continue block has been there since then.#718: 1.2.9 through 2.5.2 (latest) and master.
json_handlerbefore the validation.🤖 Generated with Claude Code
Note
Medium Risk
Changes when and how benchmark results are written at startup; behavior is well-tested but affects all runs using
--out-fileor--json-out-file.Overview
Fixes startup handling for
--out-fileand--json-out-fileso bad paths fail immediately and do not destroy prior results.Output opens move to the end of startup validation (after option checks, DNS, and data import). JSON config is no longer written before validation, so early
exit/usageleaves existing JSON and text output files intact.--out-filebehavior changes: unwritable paths now exit with a clear error instead of running the benchmark and crashing on write. The file is created withopen(O_WRONLY|O_CREAT)first;ftruncateruns only after--json-out-fileopens successfully, and only for regular files (stdout/FIFOs unchanged). Failed JSON open no longer wipes an existing--out-file.Integration tests cover unwritable outputs, truncation on success, stderr message order, and that validation errors preserve both output files.
Reviewed by Cursor Bugbot for commit a673611. Bugbot is set up for automated code reviews on this repo. Configure here.