Internal call to vfork() is observable by downstream users
Internal call to vfork() is observable by downstream users
The test suite for Swish, which has a runtime model similar to Node.js, started observing an unexpected SIGCHLD during execution.
I reduced the behavior to the following C reproducer. On GitHub Actions ubuntu-24.04 runners, I see different behavior between libuv 1.52.1 and 1.53.0.
With libuv 1.52.1:
./repro
libuv version: 1.52.1
calling uv_spawn()
uv_spawn returned; requested child pid=4341
timeout: no unexpected SIGCHLD observed
requested child exit_cb: pid=4341 status=0 signal=15
RESULT: unexpected SIGCHLD not observedWith libuv 1.53.0:
./repro
libuv version: 1.53.0
calling uv_spawn()
uv_spawn returned; requested child pid=4296
UNEXPECTED: SIGCHLD arrived while requested child should still be sleeping
requested child exit_cb: pid=4296 status=0 signal=15
RESULT: unexpected SIGCHLD observedI believe the SIGCHLD observed in 1.53.0 may come from the internal vfork() introduced in #3520.
If so, this appears to expose an implementation detail of uv_spawn() to application-level signal handlers.
My expectation is that a SIGCHLD caused solely by libuv's internal process-creation machinery should not be observable by libuv user code. In practice, this also means the signal can propagate to higher-level runtimes such as Swish or Node.js, where application code may observe a child-process event for a process it did not create.
Reproducer
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include "uv.h"
static uv_process_t child;
static uv_signal_t sigchld_watcher;
static uv_timer_t timer;
static int unexpected_sigchld = 0;
static void on_child_exit(uv_process_t *process,
int64_t exit_status,
int term_signal)
{
fprintf(stderr,
"requested child exit_cb: pid=%d status=%lld signal=%d\n",
process->pid,
(long long) exit_status,
term_signal);
uv_close((uv_handle_t *) process, NULL);
}
static void cleanup(void)
{
int rc;
rc = uv_process_kill(&child, SIGTERM);
if (rc != 0)
fprintf(stderr, "uv_process_kill: %s\n", uv_strerror(rc));
}
static void on_sigchld(uv_signal_t *handle, int signum)
{
(void) signum;
fprintf(stderr,
"UNEXPECTED: SIGCHLD arrived while requested child "
"should still be sleeping\n");
unexpected_sigchld = 1;
/* Test is over. Cancel the watchdog and stop observing SIGCHLD. */
uv_timer_stop(&timer);
uv_close((uv_handle_t *) &timer, NULL);
uv_signal_stop(handle);
uv_close((uv_handle_t *) handle, NULL);
cleanup();
}
static void on_timer(uv_timer_t *handle)
{
fprintf(stderr, "timeout: no unexpected SIGCHLD observed\n");
/*
* Stop observing SIGCHLD first, so the deliberate termination below
* cannot be mistaken for the condition we're testing.
*/
uv_signal_stop(&sigchld_watcher);
uv_close((uv_handle_t *) &sigchld_watcher, NULL);
uv_timer_stop(handle);
uv_close((uv_handle_t *) handle, NULL);
cleanup();
}
int main(void)
{
uv_loop_t *loop;
uv_process_options_t options;
char *args[] = { "/bin/sleep", "30", NULL };
int rc;
fprintf(stderr, "libuv version: %s\n", uv_version_string());
loop = uv_default_loop();
rc = uv_signal_init(loop, &sigchld_watcher);
if (rc != 0) {
fprintf(stderr, "uv_signal_init: %s\n", uv_strerror(rc));
return 1;
}
rc = uv_signal_start(&sigchld_watcher, on_sigchld, SIGCHLD);
if (rc != 0) {
fprintf(stderr, "uv_signal_start: %s\n", uv_strerror(rc));
return 1;
}
memset(&child, 0, sizeof(child));
memset(&options, 0, sizeof(options));
options.file = args[0];
options.args = args;
options.exit_cb = on_child_exit;
fprintf(stderr, "calling uv_spawn()\n");
rc = uv_spawn(loop, &child, &options);
if (rc != 0) {
fprintf(stderr, "uv_spawn: %s\n", uv_strerror(rc));
return 1;
}
fprintf(stderr,
"uv_spawn returned; requested child pid=%d\n",
child.pid);
/*
* This timer is only a watchdog. If no unexpected SIGCHLD appears
* promptly, terminate the sleeping child so the test can finish.
*/
rc = uv_timer_init(loop, &timer);
if (rc != 0) {
fprintf(stderr, "uv_timer_init: %s\n", uv_strerror(rc));
return 1;
}
rc = uv_timer_start(&timer, on_timer, 1000, 0);
if (rc != 0) {
fprintf(stderr, "uv_timer_start: %s\n", uv_strerror(rc));
return 1;
}
uv_run(loop, UV_RUN_DEFAULT);
fprintf(stderr,
"RESULT: %s\n",
unexpected_sigchld
? "unexpected SIGCHLD observed"
: "unexpected SIGCHLD not observed");
return unexpected_sigchld ? 0 : 2;
}Expected behavior
Calling uv_spawn() should not cause application-installed SIGCHLD watchers to observe signals generated only by libuv's internal process-creation implementation.
Actual behavior
Beginning with libuv 1.53.0, the reproducer receives a SIGCHLD while the process explicitly requested through uv_spawn() is still running.
Source: libuv/libuv