selftests/namespaces: Kill grandchild in nsid fixture teardown

The timens_separate and pidns_separate test cases fork a grandchild that
calls pause().  FIXTURE_TEARDOWN only kills the direct child, which is the
init process of the grandchild's namespace.  Once the child (init) exits,
the grandchild is reparented to the host init but remains alive and
continues to hold the inherited write end of the test runner's TAP pipe
open.  tap_prefix never receives EOF and blocks indefinitely, hanging the
entire test collection.

Record the grandchild PID in the fixture struct so that teardown can send
SIGKILL and reap it before dealing with the child.  The grandchild must be
reaped first because the child acts as its PID namespace init; killing the
child first would kill the grandchild without giving us a chance to
waitpid() it.

Signed-off-by: Ricardo B. Marlière <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Christian Brauner <[email protected]>
This commit is contained in:
Ricardo B. Marlière authored and Christian Brauner committed 2026-05-11 12:25:31 +02:00
1 parent 6f43b87876
commit fdb48976b6
1 file changed
+13 -1
+13 -1
View File
@@ -25,14 +25,24 @@
/* Fixture for tests that create child processes */
FIXTURE(nsid) {
pid_t child_pid;
pid_t grandchild_pid;
};
FIXTURE_SETUP(nsid) {
self->child_pid = 0;
self->grandchild_pid = 0;
}
FIXTURE_TEARDOWN(nsid) {
/* Clean up any child process that may still be running */
/*
* Kill grandchild first: timens_separate and pidns_separate fork a
* grandchild that calls pause(). It is reparented to init on child
* exit and keeps the test runner's tap pipe open, hanging the runner.
*/
if (self->grandchild_pid > 0) {
kill(self->grandchild_pid, SIGKILL);
waitpid(self->grandchild_pid, NULL, 0);
}
if (self->child_pid > 0) {
kill(self->child_pid, SIGKILL);
waitpid(self->child_pid, NULL, 0);
@@ -676,6 +686,7 @@ TEST_F(nsid, timens_separate)
pid_t grandchild_pid;
ASSERT_EQ(read(pipefd[0], &grandchild_pid, sizeof(grandchild_pid)), sizeof(grandchild_pid));
self->grandchild_pid = grandchild_pid;
close(pipefd[0]);
/* Open grandchild's time namespace */
@@ -797,6 +808,7 @@ TEST_F(nsid, pidns_separate)
pid_t grandchild_pid;
ASSERT_EQ(read(pipefd[0], &grandchild_pid, sizeof(grandchild_pid)), sizeof(grandchild_pid));
self->grandchild_pid = grandchild_pid;
close(pipefd[0]);
/* Open grandchild's PID namespace */