Repository navigation
Reopen closed stdin/stdout/stderr on /dev/null at startup - #736
Conversation
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this. I went through the C++ and the tests, and only have a couple of small comments.
The fix itself looks solid to me. I'd mainly want the description brought in line with what the code actually does before this merges. (would file this as a comment, not a formal block) |
|
Thanks for the review. On the exit-code question: a closed stdout with
Closed stdin now has a test too ( The latest push also rejects an |
f11a150 to
12856ff
Compare
|
Thanks. Both are addressed in 12856ff (rebased on master 0fc29f4):
On the exit-code question, the PR body now has a short "why exit 1 rather than a warning" paragraph. The reason holds without #729: losing the default results sink silently is #719's failure mode, and |
12856ff to
69f128c
Compare
|
Points 2 and 3a are addressed in 69f128c:
New tests pin both: |
34f9c03 to
bce73c5
Compare
|
Thanks. I've answered each point; the changes are in bce73c5, rebased on master 8ef3971. Is
I kept it and did what you suggested: the
What runs between
Everything else comes after the check:
Since fds 0-2 are open by the time any of these runs, none of them can get fd 0-2. The "before any of those files is opened" claim depends only on the three checked output paths, and all three are opened after the check. Nit: |
bce73c5 to
ec232eb
Compare
|
Thanks. The changes are in ec232eb, rebased on master 8ef3971. Point by point:
|
ec232eb to
e08ac06
Compare
|
Thanks for the update. Both points are addressed in e08ac06, rebased on master a7881e0:
|
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]>
e08ac06 to
43eea81
Compare
|
Thanks for the re-edit. Point by point (changes in 43eea81, rebased on master 38dfbaa, which includes #751):
|
|
Thanks. On the two open points:
|
Problem
If memtier is started with stdout closed (
memtier_benchmark ... >&-), thefopen()of--json-out-filegets fd 1. The text results that go tostdoutare written into the JSON file, mixed with the JSON. The file is invalid and the run exits 0. With stderr closed, the JSON file gets fd 2, so it starts withJson file ... created...and is invalid, also with exit 0. Even when the JSON happened to stay valid (--hide-histogram), the text results were lost (write(1) = EBADF) and the run still exited 0.Root cause
Nothing made sure fds 0-2 were open before files were opened.
main()usesoutfile = stdout, thennew json_handler()callsfopen(), which takes the lowest free fd. After that,stdout(orstderr) and the JSONFILE*write to the same file. The same applies to--out-file,--failed-keys-fileand any other file opened while 0-2 are free.Fix
reopen_closed_std_fds()runs first inmain(). For fds 0, 1 and 2, iffcntl(fd, F_GETFD)fails withEBADF, it callsopen("/dev/null", O_RDWR)(retrying onEINTR), which gets that fd. It returns a mask of the reopened fds. If the open fails or returns another fd, memtier prints an error (when stderr is open) and exits 1. This is the usual daemon/setuid idiom.--out-file,--json-out-file,--failed-keys-file.--data-import,--monitor-input,--cert,--key,--cacert. With stdin closed,/dev/stdinused to fail with "cannot open"; after the reopen it would read an empty/dev/null./dev/stdout,/dev/fd/1,/proc/self/fd/1,/proc/thread-self/fd/1and symlinks to them, and likewise for stdin and stderr.error: --out-file /dev/stdout resolves to stdout, which was closed at startup. With stderr closed it is not visible, only rc 1.--failed-keys-filethat names a closed fd is fatal here, while a failed-keys path that cannot be opened only warns, as before. The difference is deliberate: an unopenable path prints an error, but a closed-fd path opens fine on/dev/nulland the log would be lost without any message.--out-file): a closed stdout with no--out-fileis rejected with exit 1 anderror: stdout was closed at startup, so the results would not be written; use --out-file. Because it is late, a bad option (for example a jitter error) still reports its own error. To keep only the JSON, pass--out-file /dev/null.--json-out-fileis valid. The exit-code tail ofmain()is unchanged from master.path_is_closed_fd(path, fd)follows the path one symlink at a time. It resolves the directory part withrealpath()and checks the name against/dev/<name>,/dev/fd/N,/proc/<getpid()>/fd/Nand/proc/<getpid()>/task/<tid>/fd/N(is_task_fd_path()). Any spelling of/dev/null(/dev/./null, a symlink to it) is accepted. So is another process's fd, such as the Docker idiom--out-file /proc/1/fd/1.--hdr-file-prefixand--client-statsare prefixes. memtier always appends a suffix (_FULL_RUN_1.txt,-1-0-0.csv, ...), so they never name a descriptor themselves and are not checked. With stdout closed,--hdr-file-prefix /dev/stdout --client-stats /dev/fd/1opens/dev/stdout_FULL_RUN_1.txt(Permission denied) and/dev/fd/1-1-0-0.csv(No such file), as on master; reporting those errors in the exit code is the known gap Write errors on --hdr-file-prefix files, --client-stats CSVs and --failed-keys-file are ignored: a full disk leaves empty/truncated files and the run exits 0 #744, linked from the README.Why fail at startup: with stdout closed and no
--out-file, it is known as soon as the fds are reopened that the text results would go to/dev/null. Running the whole benchmark (possibly minutes with--test-time) just to exit 1 at the end gains nothing; the user has to rerun with--out-file(or--out-file /dev/null) anyway. This matches #717, and it removes the ordering logic an end-of-run check needed next to the connection-stage (2) and worker-failure (1) exits.Why the path check: reopening fd 1 on
/dev/nullwould itself cause a silent-loss regression. On master,--out-file /dev/stdoutwith stdout closed failed loudly (rc 1, "cannot open"). After the reopen the same path opens/dev/null, so the results would be lost with exit 0. The path check keeps those cases failing, now with a clearer message, while any spelling of/dev/nullstill works. An inode comparison cannot do this, since the reopened fd 1 is/dev/null.Why exit 1 rather than a warning: stdout is the default results sink, and losing it silently with exit 0 is the failure mode of #719. #729 treats a failed results write as exit 1. This PR keeps that rule after the reopen hides the
EBADF.Interplay with #729 (merged as 2285b02)
Before this PR, #729 reported a closed stdout as a failed write (
EBADF), but only after the whole run. Now fd 1 is/dev/null, so those writes would succeed. The startup check above covers that case with the same exit code (1). #729'sstdout_reader_gone()exemption cannot apply to/dev/null.Tests
New
tests/test_closed_std_fds.py(23 tests), also added to the asan and tsan "misc" cells (single endpoint and TLS v1.3). Each test runs memtier throughsh -c 'exec "$@" >&-'(or<&-,2>&-), notpreexec_fn, which is unsafe in a threaded parent. Each test uses its ownTemporaryDirectory. The results go to stdout (Benchmark's default--out-fileis removed), with--json-out-file. On master the closed-stdout case corrupts the JSON only intermittently, so the tests catch the bug through the exit code and the error message. The startup-rejection tests check that memtier stopped before opening files or connecting: nomb.json, noJson file ... createdline and noPreparing benchmark clientline.test_stdout_closed_json_out_file,..._hide_histogram,..._run_count_2test_stdout_closed_out_file_dev_stdout,..._json_out_file_dev_stdout,..._out_file_dev_fd_1,..._out_file_proc_thread_self_fd_1,..._out_file_symlink_to_dev_stdouttest_stdin_closed_out_file_dev_stdintest_stdin_closed_data_import_dev_stdin,test_stdin_closed_monitor_input_dev_stdin,test_stdin_closed_cacert_dev_stdin/dev/null)test_stderr_closed_failed_keys_file_dev_stderrtest_stderr_closed_json_out_file_dev_stderr,test_stderr_closed_out_file_dev_fd_2test_stderr_closed_json_out_fileJson file ... created...)test_stdout_closed_failed_keys_file_does_not_take_fd_1test_stdout_closed_with_out_file_succeeds(control)test_stdout_closed_out_file_dev_null_is_allowed,..._dev_dot_null_is_allowed,..._symlink_to_dev_null_is_allowed(controls, rc 0)test_stdin_closedtest_stdout_closed_bad_option_reports_its_own_error(min jitter > max jitter, stdout closed: the jitter error, not the stdout one)The rc-2 precedence test from earlier revisions is gone: a closed stdout without
--out-filenow exits before connecting, so the run can no longer reach a connection-stage abort.Measured rates (
--env oss), on 43eea81 (rebased on master 38dfbaa, which includes #751):test_closed_std_fds.py: 23/23 passed in 5/5 runs. The previous head e08ac06 fails the 3 new input-path tests.test_json_output_integrity.pyandtest_json_percentile_key_collision.py: 36/36 passed.test_stdin_closed, the two stderr-path tests that master already rejects with "cannot open", andtest_stdout_closed_bad_option_reports_its_own_error) pass.test_stdout_closed_failed_keys_file_does_not_take_fd_1and 12 others.Fixes #733
Affected versions
1.2.9 through 2.5.2 (latest) and master.
fopen()while the text results go tostdout, with no fd 0-2 guard.git tag --contains fb6888dgives 1.2.9.🤖 Generated with Claude Code