You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Drain in-flight requests when a PHP thread segfaults #2836
Follow-up to #1995, with a smaller goal. The process still dies, but only the faulting request is lost.
Today, a segfault in a PHP thread kills the process immediately. All in-flight requests fail, including the ones that Caddy serves without PHP.
I'd like to delay the death instead:
A C constructor installs a SIGSEGV/SIGBUS handler before the Go runtime starts. Go saves this handler and calls it when a fault happens outside Go code. Faults in Go code are still Go panics.
When the fault happens on a PHP thread, the handler notifies Go through a pipe, then parks the thread.
Go stops accepting requests and gives the in-flight ones a short deadline to finish.
Go then releases the parked thread. The thread faults again and the process dies by SIGSEGV as it does today. The core dump still shows the faulting stack.
staticvoidcrash_handler(intsig, siginfo_t*info, void*ctx) {
charc;
if (php_thread_index >= 0&&is_first_fault()) {
write(notify_pipe[1], &php_thread_index, sizeof(php_thread_index));
/* parked while Go drains */while (read(release_pipe[0], &c, 1) <0&&errno==EINTR);
}
/* then return: the instruction faults again */signal(sig, SIG_DFL);
}
This doesn't try to recover from the segfault. The faulting thread never runs PHP code again, and the supervisor still sees a crash.
The cost is the one @withinboredom described in #1995. The memory might be corrupted, and the other threads keep running until the deadline. They already do so today between the corruption and the fault. This makes the window longer, by a few seconds at most. A request could also wait for a mutex that the parked thread locked. That's why the deadline is needed. I'd make this opt-in, with the deadline as the setting.
I checked the mechanism in a standalone cgo program, with 4 C threads that call into Go. On a NULL dereference and on a stack overflow, the 3 other threads finish their request and the exit status is still 139. A GC cycle completes while the faulting thread is parked. A nil dereference in a Go callback is still a recoverable panic.
Activity