Repository navigation
Keep the crash report header out of localtime() and stdio - #755
Conversation
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this, and for the very thorough write-up. I went through the handler change,
The core fix looks solid to me. The DST point is optional. I'd just like a quick answer on the re-raise question before this merges. |
2daf36a to
6315c51
Compare
|
Thanks for the review. Replies to your points, against 6315c51: a. The stale SIGALRM comment in b. Output format: yes, this is intended.
c. No Also changed in this push, from the other reviews:
🤖 Generated with Claude Code |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.
❌ 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 6315c51. Configure here.
crash_handler() called localtime() and strftime() before it printed anything. localtime() takes glibc's time zone lock, and the first call in the process (always the one in the handler, since nothing else in memtier calls it) runs tzset_internal(), which mallocs and opens /etc/localtime. A crash that interrupts a thread holding the malloc or time zone lock therefore deadlocked the handler before the "BUG REPORT START" line, and its own alarm(5) watchdog then killed the process with SIGALRM and no output. That is the flaky test_crash_handler_worker_thread failure seen on PR #636. The handler now: - takes the local UTC offset once in prewarm_crash_handler() and formats the timestamp with integer arithmetic, in the same "%d %b %Y %H:%M:%S" format; - writes the START header, the signal number and name, the signal code and the fault address with write(2) to stderr, before backtrace(), stdio or anything else that can lock; - arms a SIGALRM handler that writes a "BUG REPORT INCOMPLETE" line before the watchdog ends the process, so a later hang is visible. The rest of the report (backtrace, fprintf, client list) is unchanged and stays after the essential lines; a comment lists which of those calls are not async-signal-safe and why. The new test_crash_handler_time_zone_lock_held preloads a shim whose localtime/localtime_r/strftime/tzset block when called on a signal stack, which reproduces the deadlock on every run: it fails on master (exit -14, no report) and passes with this change. Fixes #681 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-up for the crash handler change. - If stderr is a pipe or tty that stopped draining, the crash handler blocks in write(2), and the SIGALRM handler then blocks writing its INCOMPLETE line. SIGALRM stayed masked while that handler ran, so the process hung for good, where master died at 5 s. The watchdog handler is now installed with SA_NODEFER | SA_RESETHAND and calls alarm(1) before it writes, so a second SIGALRM takes the default action. - ThreadSanitizer runs a handler for an asynchronous signal only when the thread enters or leaves an intercepted call, which a thread stuck on a futex or a blocked write never does. TSan builds keep SIG_DFL for SIGALRM, as before this change. - Move the line buffer, the UTC offset and the time formatting into crash_report.h, and check them against gmtime_r/localtime_r + strftime in a new crash_report_test unit test (1900-2400, real zone offsets, POSIX TZ strings, DST, year ends). - Tests: send the SEGV to a worker with tgkill(2) and skip threads that block SIGSEGV (TSan's background thread); check the header time under TZ=<+0530>-5:30; add tests where backtrace() hangs (INCOMPLETE, exit by SIGALRM), where a helper thread holds stderr's stdio lock (the write(2) lines and INCOMPLETE still appear), and where stderr is a full pipe (exit by SIGALRM). Compiling the shim and ldd get a 120 s / 60 s timeout and skip, with the compiler output, on failure. - Document the INCOMPLETE line in DEVELOPMENT.md and fix the stale SIGALRM comment in setup_crash_handlers(). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-up. - crash_watchdog_handler: drop the signal(sig, SIG_DFL) before raise(); SA_RESETHAND already restored the default action. Note that a crash by a different signal re-arms alarm(5), so death can take about 11 s. - The shim records in MEMTIER_TEST_SHIM_MARKER when the crash handler arms alarm() on a signal stack, when it blocks a function, and when the helper holds the stderr lock; the tests assert those lines, so a shim that never loaded or never saw the handler fails the test. The shim now arms the real alarm before waiting for the helper and waits at most 2 s, so a helper that never gets the lock cannot hang the process. - _crash_shim_env() also drops an inherited MEMTIER_TEST_LOCK_STDERR. - _tsan_build() looks for __tsan_init in the binary, so a clang build with the runtime linked in is detected too, not only libtsan in ldd. - On TSan builds, send the crash signal with kill(2) as before: TSan runs the handler for a signal sent to a busy worker later, off the signal stack, and that occasionally printed nothing. Skip test_crash_handler_header_survives_stderr_lock on TSan: in CI (job 117257659823) nothing after the SEGV was printed and the process never exited. Co-Authored-By: Claude Opus 5.5 <[email protected]>
6315c51 to
cca68ae
Compare
|
Replies to the rewritten review, against cca68ae:
On the SIGALRM/TSan interaction, two changes from CI:
A review of the TSan part by whoever owns the sanitizer CI cells would still be welcome. 🤖 Generated with Claude Code |
Review follow-up. - crash_report_test compared crash_format_time() with gmtime_r() for 1900-2400, but a 32-bit time_t (i386 and older armhf .deb builds, which run make check) only covers 1901-2038, so libc formatted wrapped values and the test failed with 151003 mismatches. Cases that do not fit time_t are now skipped and counted; the formatter itself computes in long long and is unaffected. Checked in an i386/ubuntu:bionic container: passes, 151003 cases skipped. - Comment at the watchdog install that SIGALRM may land on any thread, which is fine. - Keep getpid() in the best-effort part of the report, as on master, to keep the diff to what changed. - The time zone test parses the month name by hand instead of with strptime's locale-dependent %b; tgkill comes from libc when it has it; _tsan_build() is cached. - DEVELOPMENT.md: TSan builds never print the INCOMPLETE line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Replies to the current review, against c19de5a:
🤖 Generated with Claude Code |
|
Thanks. On the open points:
|

Problem
The crash handler can lose its whole bug report.
crash_handler()calledlocaltime()andstrftime()before printing anything. When the crash interrupts a thread that holds the malloc or time zone lock, the handler deadlocks before the=== MEMTIER_BENCHMARK BUG REPORT STARTline. Its ownalarm(5)watchdog then kills the process with SIGALRM and no output. This is the flakytest_crash_handler_worker_threadfailure on PR #636.Root cause
localtime()is not async-signal-safe: it takes glibc's time zone lock. Nothing else in memtier calls it, so the handler always makes the first call in the process. That first call runstzset_internal(), which doesstrdup,fopen("/etc/localtime")and a stdio buffermalloc(gdb trace in #681).prewarm_crash_handler()did not warm it.Fix
prewarm_crash_handler()takes the local UTC offset withcrash_utc_offset()(localtime_r/gmtime_r). The handler formatstime(NULL) + offsetwith integer arithmetic (Hinnant's civil_from_days) in the same%d %b %Y %H:%M:%Sformat. The code lives in the new headercrash_report.h.crashed by signal,Crashed running signal <...>,Signal codeandFault addresslines are formatted into a stack buffer and written withwrite(2)to fd 2. This happens beforebacktrace(), stdio, or anything else that can lock. The text is unchanged. The fault address is printed as0x<hex>, or(nil)for NULL, which matches glibc's%p. Other libcs differ for NULL (macOS prints0x0, musl prints0), so on those platforms the NULL case now looks like glibc.alarm(5), the handler installs a SIGALRM handler withSA_ONSTACK | SA_NODEFER | SA_RESETHAND. That handler first callsalarm(1)and then writes=== MEMTIER_BENCHMARK BUG REPORT INCOMPLETE: crash handler timed out ===. If that write blocks too (stderr is a full pipe or a stopped tty), the second SIGALRM takes the default action, so the process always dies: at about 5 s when the line can be written, about 6 s when it cannot.SIG_DFLfor SIGALRM, as on master. TSan defers a handler for an asynchronous signal until the thread enters an intercepted call, and a thread stuck on a futex or in a blocked write never does.backtrace()takes the loader lock while unwinding; its first-use cost is prewarmed.fprintf(stderr)takes the FILE lock. It is recursive, so it blocks only while another thread is inside a stderr call.print_client_list()reads worker vectors without synchronization.DEVELOPMENT.mddocuments the INCOMPLETE line.Known limitations:
alarm(5), so death can take about 11 s. It is still bounded.Tests
tests/crash_handler_lock_injector.cis an LD_PRELOAD shim. It follows theworker_exception_injector.cpppattern and keeps the sanitizer runtime first. Tests that use it skip when the shim cannot be compiled (120 s timeout) or whenlddfails (60 s timeout); the skip reason includes the compiler output. Every test sends SIGSEGV to a real worker withtgkill(2). It skips threads that block SIGSEGV, such as TSan's background thread. On TSan builds the tests usekill(2)to the process instead, as they did before this PR: TSan runs the handler for a signal sent to a busy worker later, off the signal stack, and in 1 of about 20 local runs that handler printed nothing. The shim writes a marker file (MEMTIER_TEST_SHIM_MARKER) recording what it saw: the handler armingalarm()on a signal stack, each function it blocked, and the helper taking the stderr lock. The tests assert those markers, so a shim that never loaded cannot pass silently. TSan builds are detected by the__tsan_initsymbol name in the binary, which covers both GCC's shared runtime and clang's static one.test_crash_handler_time_zone_lock_heldlocaltime/localtime_r/strftime/tzsetblock on the signal stack (#681), withTZ=<+0530>-5:30test_crash_handler_watchdog_marks_incomplete_reportbacktrace()blocks on the signal stacktest_crash_handler_header_survives_stderr_lockalarm()test_crash_handler_watchdog_blocked_stderrcrash_report_test(unit)crash_format_time()vsgmtime_r+strftimefrom 1900 to 2400 (only the part that fitstime_ton 32-bit) at 8 zone offsets, plus leap days and year ends;crash_utc_offset()vslocaltime_rfor POSIX TZ strings, including DST and local/UTC year boundariesResults (standalone OSS)
detect_leaks=1)setarch -R, as in CI)make checkpasses 12/12 on the default build of c19de5a (11/11 on ASan and UBSan before the rebase that added keylist_growth_test).crash_report_testalso passes in ani386/ubuntu:bioniccontainer (32-bittime_t), with the 151003 cases outside thetime_trange skipped.Timings with this head: blocked stderr exits about 6.0 s after the SEGV, and the hung-backtrace and stderr-lock tests exit at about 5.0 s.
Mutants:
fprintf, watchdog handler not installed, UTC offset never stored.raise()from the watchdog (the redundantsignal(SIG_DFL)before it was removed, sinceSA_RESETHANDalready restores the default). The process then dies at 6 s fromalarm(1)instead of at 5 s, so the only difference is 1 s of timing. I left it uncovered rather than add a sub-second timing assertion.Affected versions
2.3.0 through 2.5.2 (latest) and master.
localtime()in the handler: 4a8ac10 "Add crash handling/debugging support, and version metadata generation" (Add crash handling/debugging support, and version metadata generation #328). First tag: 2.3.0.alarm(5)and the prewarm that skippedlocaltime: 0b02655. First tag: 2.4.0.alarm(5)turns the deadlock into a silent exit; before 2.4.0 the process hung instead.Fixes #681
🤖 Generated with Claude Code
Note
Medium Risk
Changes signal-handler behavior and crash-report output paths (including watchdog termination via SIGALRM instead of the original fault signal when the handler hangs), which is security-adjacent infrastructure but well-tested.
Overview
Fixes #681: the crash handler could deadlock on
localtime()/stdioand print nothing beforealarm(5)killed the process.Crash path. UTC offset is captured at startup via
crash_utc_offset()in newcrash_report.h; the handler formats timestamps with integer-onlycrash_format_time()instead oflocaltime/strftime. The START line, signal name, code, and fault address go through acrash_linebuffer andwrite(2)before anybacktraceorfprintf. A SIGALRM watchdog printsBUG REPORT INCOMPLETEwhen the best-effort section hangs (with a secondalarm(1)if stderr blocks); ThreadSanitizer builds keep default SIGALRM behavior.Tests & docs. Unit
crash_report_testcompares formatting to libc; an LD_PRELOAD lock injector and expanded RLTest scenarios cover timezone-lock simulation, watchdog timeout, stderr lock, and blocked pipe;DEVELOPMENT.mddocuments incomplete reports.Reviewed by Cursor Bugbot for commit c19de5a. Bugbot is set up for automated code reviews on this repo. Configure here.