Repository navigation
fix: block SIGPIPE on kernel runtime threads - #509
vuanhphung wants to merge 3 commits into
Conversation
std's TcpStream::write passes MSG_NOSIGNAL, but write_vectored is a plain writev, and rustls/hyper write TLS records through it. After a peer reset, the close_notify that hyper writes on connection shutdown raises SIGPIPE on a kernel runtime thread. Rust executables ignore SIGPIPE at startup, but a Go host sends a SIGPIPE raised on a non-Go thread to the default handler, so the process dies. Block SIGPIPE with on_thread_start on the C-ABI runtime and the shared telemetry runtime. The hook covers their workers and blocking pools. The write then fails with EPIPE, and the process-wide disposition is unchanged. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
A re-exec child sets SIGPIPE to its default action, reads ECONNRESET from a peer that closed with a zero linger, then writes with writev on a C-ABI worker. It asserts EPIPE, and the parent asserts the child was not killed by SIGPIPE. The log callback contract (Rust doc and C header) now says kernel threads run with SIGPIPE blocked and callbacks must not unblock it. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
The parent now also checks the child's libtest summary for "1 passed", so a filter that matches nothing can't pass silently. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).
Verdict: 1 Nit
Looks good, with one low concern. Blocking SIGPIPE per thread with on_thread_start on the C-ABI and telemetry runtimes is sound. The subprocess test runs with SIGPIPE at its default action and checks for "1 passed", so it would catch a regression. The unhooked pyo3 and ADBC runtimes are documented as follow-ups.
Other findings
- ⚪ Nit — The return values of sigemptyset, sigaddset and pthread_sigmask are ignored. With these fixed, valid arguments they can't realistically fail, so the risk is very small. Adding a debug_assert_eq!(rc, 0) on the pthread_sigmask result would still make a failure visible in tests. (Found by Claude Opus 5.5 · confirmed by the judge)
|
Thanks. On the nit about ignored return values: I'm leaving them as is. |
Description
On Linux, a Go program that links the kernel's C ABI dies with SIGPIPE when a peer resets a TLS connection. Rust std's
TcpStream::writepassesMSG_NOSIGNAL, butwrite_vectoredis a plainwritev, and ontls-rustlsbuilds (the Go archive, pyo3, napi, ODBC on Linux aarch64) rustls/hyper write TLS records through it. AfterrecvfromreturnsECONNRESET, hyper shuts the connection down, and thewritevof the close_notify fails withEPIPEand raises SIGPIPE on akernel-c-abithread. Rust executables ignore SIGPIPE beforemain, but a library's host may not. Go finds no non-Go handler for a SIGPIPE raised on a non-Go thread, so it re-raises the signal with the default action and the process exits with status 141.This PR blocks SIGPIPE through tokio's
on_thread_start, usingpthread_sigmask, on the two runtimes the kernel owns: the C-ABI runtime and the shared telemetry runtime. The hook also runs for their blocking-pool threads. The failed write still returnsEPIPE, and the request reports the connection error.The fix is thread-scoped instead of a process-wide
SIG_IGNso that the host keeps its own disposition. Go's handler, stdout broken-pipe behavior, a user'ssignal.Notify, and what child processes inherit all stay as they are. Go'sos/signaldocs allow threads started by non-Go code to set their own masks. Children spawned from kernel threads still start with a clean mask, because std'sCommandresets it.Measured on the Go driver with bindings v1.5.0 (kernel 5c3a909):
TestKernelCloudFetchDisabledPreservesUnsupportedServerErrorcrashes in about 40% of runs.A Go-side shim that leaves the kernel threads in the same mask state as this PR brought those numbers to 0/100, 0/3 and 0/400.
Kernel threads and runtimes
kernel-c-abi(src/c_abi/mod.rs): workers and blocking pooldbx-sql-telemetry(src/telemetry/client.rs), shared by every bindingspawn_blocking(src/auth/oauth/token_store.rs)dbx-sql-kernel(pyo3/src/session.rs)kernel-log-drainthreadadbc/src/runtime.rs)All other
thread::spawnand runtimeBuilderuses are in#[cfg(test)]code.Cross-driver state
kernel_mtls_test.go:180@319b145 already avoids a peer-closed TLS write for this reasontls-rustlsbuilds unless the host ignores SIGPIPE → Fixed (this PR)tls-nativebuilds send TLS through OpenSSL's plainwrite(tokio-native-tls has no vectored write), which usesMSG_NOSIGNALSIG_IGN(Modules/signalmodule.c:1913@v3.13.0). An app that restoresSIG_DFLis exposed on the session threads; this PR covers only the telemetry threadsSIG_IGN(src/node.cc:544@v22.11.0)Testing
c_abi_runtime_threads_block_sigpipeandtelemetry_runtime_threads_block_sigpipe, run a task on a worker and on aspawn_blockingthread of each runtime. Each task checks that bit 13 is set inSigBlkin/proc/thread-self/status. The tests first assert that the calling test thread doesn't block SIGPIPE, so an inherited mask can't make them pass.c_abi_write_after_peer_reset_returns_epipere-executes the test binary with SIGPIPE at its default action. In the child, a C-ABI worker readsECONNRESETfrom a loopback peer that closed with a zero linger, then callswrite_vectoredand assertsEPIPE. Without the hook the child dies from SIGPIPE (exit 141) and the parent test fails.macOS dSYM, whoseVerify dSYMstep has also timed out on everymainpush since 2026-11-24. As a result, no CI job compiles the newcfg(unix)path on macOS; I checked that the libc 0.2.186 symbols it uses exist on Apple targets.libc0.2 is already in all four lock files (0.2.186, and 0.2.191 for adbc). Each lock file only gains a"libc"entry in the kernel's dependency list, so--lockedbuilds (napi) need no re-resolution.Additional notes
EPIPEinstead of SIGPIPE from a write to a closed pipe. Go callbacks keep SIGPIPE blocked (Go treats it as blockable on non-Go threads), and Go's own stdout check still kills the process as before. TheKernelLogCallbackcontract, in both the Rust docs and the C header, now says callbacks must not unblock SIGPIPE, because a SIGPIPE already pending on that thread would then be delivered.pthread_sigmaskcall per runtime thread start, and nothing changes on the request path.This pull request and its description were written by Isaac.