Skip to content

fix: block SIGPIPE on kernel runtime threads - #509

Open
vuanhphung wants to merge 3 commits into
mainfrom
fix/block-sigpipe-runtime-threads
Open

vuanhphung wants to merge 3 commits into
mainfrom
fix/block-sigpipe-runtime-threads

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

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::write passes MSG_NOSIGNAL, but write_vectored is a plain writev, and on tls-rustls builds (the Go archive, pyo3, napi, ODBC on Linux aarch64) rustls/hyper write TLS records through it. After recvfrom returns ECONNRESET, hyper shuts the connection down, and the writev of the close_notify fails with EPIPE and raises SIGPIPE on a kernel-c-abi thread. Rust executables ignore SIGPIPE before main, 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, using pthread_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 returns EPIPE, and the request reports the connection error.

The fix is thread-scoped instead of a process-wide SIG_IGN so that the host keeps its own disposition. Go's handler, stdout broken-pipe behavior, a user's signal.Notify, and what child processes inherit all stay as they are. Go's os/signal docs 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's Command resets it.

Measured on the Go driver with bindings v1.5.0 (kernel 5c3a909):

  • An RST while a request waits for its response kills the process in 100/100 runs.
  • A live warehouse behind a proxy that resets connections: 4/4 processes died. The Thrift backend survived 4/4.
  • The Go CI test TestKernelCloudFetchDisabledPreservesUnsupportedServerError crashes 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

Runtime / thread Socket I/O This PR
kernel-c-abi (src/c_abi/mod.rs): workers and blocking pool Yes: SEA, CloudFetch, OAuth Hooked
dbx-sql-telemetry (src/telemetry/client.rs), shared by every binding Yes: telemetry export Hooked
OAuth refresh spawn_blocking (src/auth/oauth/token_store.rs) Yes Runs on the caller's runtime, so it's covered on the C ABI
pyo3 per-session dbx-sql-kernel (pyo3/src/session.rs) Yes Not hooked: it's a separate crate, so it would need a public helper or a copy. CPython ignores SIGPIPE
napi-rs global runtime; napi kernel-log-drain thread Yes; No Not hooked: Node ignores SIGPIPE
ADBC driver-owned runtime (adbc/src/runtime.rs) Yes Not hooked: separate crate. Follow-up for ADBC C ABI hosts such as Go
Caller-owned runtimes (direct Rust, injected into ADBC) Yes Unchanged

All other thread::spawn and runtime Builder uses are in #[cfg(test)] code.

Cross-driver state

Host State Notes
Go (C ABI) Dies → Fixed (this PR) The Thrift backend uses Go sockets and isn't exposed. kernel_mtls_test.go:180 @319b145 already avoids a peer-closed TLS write for this reason
ODBC and other C-ABI hosts Exposed on tls-rustls builds unless the host ignores SIGPIPE → Fixed (this PR) databricks-odbc @542da01 has no SIGPIPE handling. tls-native builds send TLS through OpenSSL's plain write (tokio-native-tls has no vectored write), which uses MSG_NOSIGNAL
Python (pyo3) Not exposed by default CPython sets SIG_IGN (Modules/signalmodule.c:1913 @v3.13.0). An app that restores SIG_DFL is exposed on the session threads; this PR covers only the telemetry threads
Node.js (napi) Not exposed Node sets SIG_IGN (src/node.cc:544 @v22.11.0)
JDBC Not applicable Doesn't use the kernel

Testing

  • New Linux-only tests, c_abi_runtime_threads_block_sigpipe and telemetry_runtime_threads_block_sigpipe, run a task on a worker and on a spawn_blocking thread of each runtime. Each task checks that bit 13 is set in SigBlk in /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.
  • End to end, c_abi_write_after_peer_reset_returns_epipe re-executes the test binary with SIGPIPE at its default action. In the child, a C-ABI worker reads ECONNRESET from a loopback peer that closed with a zero linger, then calls write_vectored and asserts EPIPE. Without the hook the child dies from SIGPIPE (exit 141) and the parent test fails.
  • No Rust toolchain was available locally, so this PR's CI is the build, clippy, fmt and test gate. Every check passes except macOS dSYM, whose Verify dSYM step has also timed out on every main push since 2026-11-24. As a result, no CI job compiles the new cfg(unix) path on macOS; I checked that the libc 0.2.186 symbols it uses exist on Apple targets.
  • libc 0.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 --locked builds (napi) need no re-resolution.
  • This PR's build hasn't been run under the Go repro, because PR CI doesn't upload the C-ABI static library.

Additional notes

  • In practice this affects Linux only. On macOS the Go runtime ignores SIGPIPE when it forwards signals, and Windows has no SIGPIPE, so the helper is a no-op there.
  • Host callbacks that run on kernel threads, such as the C-ABI log callback, also get EPIPE instead 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. The KernelLogCallback contract, 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.
  • Adversarial review: converged in round 1 with no high findings. Two lows are fixed (the callback contract and the end-to-end test); the third, hooking the ADBC driver-owned runtime, is a follow-up because it needs a public kernel helper.
  • Performance is unaffected: the hook costs one pthread_sigmask call per runtime thread start, and nothing changes on the request path.

This pull request and its description were written by Isaac.

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>
@vuanhphung
vuanhphung marked this pull request as ready for review November 26, 2026 23:51

@oss-reviewer-bot oss-reviewer-bot 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.

🔀 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)

@vuanhphung

Copy link
Copy Markdown
Collaborator Author

Thanks. On the nit about ignored return values: I'm leaving them as is. pthread_sigmask(SIG_BLOCK, ...) fails only with EINVAL for an invalid how, and sigemptyset/sigaddset fail only for an invalid signal number. All the arguments here are constants, so there's no real trigger. The Linux tests also check the effect itself rather than a return code: they read SigBlk from /proc/thread-self/status on a worker and a blocking-pool thread of both runtimes, and c_abi_write_after_peer_reset_returns_epipe checks that the write returns EPIPE while SIGPIPE is at its default action. A debug_assert on the return code would catch strictly less than those tests do.

This branch was successfully deployed

1 active deployment
azure-prod — 2fd0635f Deployed Nov 26, 2026 by vuanhphung via review #1566
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.

1 participant