Skip to content

Commit c5428ce

Browse files
committed
core: fix race condition during startup of a service with ExitType=cgroup
This commit allows service_sigchld_event() is executed before service_dispatch_exec_io(), which might happen when a main process exits very quickly. Also do not check PID for service goodness because the main process have already been exited in this case. Fix: #27919
1 parent 6209e06 commit c5428ce

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/core/service.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,11 +1984,13 @@ static bool service_good(Service *s) {
19841984
if (s->type == SERVICE_DBUS && !s->bus_name_good)
19851985
return false;
19861986

1987-
main_pid_ok = main_pid_good(s);
1988-
if (main_pid_ok > 0) /* It's alive */
1989-
return true;
1990-
if (main_pid_ok == 0) /* It's dead */
1991-
return false;
1987+
if (s->exit_type == SERVICE_EXIT_MAIN) {
1988+
main_pid_ok = main_pid_good(s);
1989+
if (main_pid_ok > 0) /* It's alive */
1990+
return true;
1991+
if (main_pid_ok == 0) /* It's dead */
1992+
return false;
1993+
}
19921994

19931995
/* OK, we don't know anything about the main PID, maybe
19941996
* because there is none. Let's check the control group
@@ -3565,6 +3567,14 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
35653567
default:
35663568
assert_not_reached("Uh, main process died at wrong time.");
35673569
}
3570+
} else if (s->exit_type == SERVICE_EXIT_CGROUP) {
3571+
if (s->state == SERVICE_START) {
3572+
/* If a main process exits very quickly, this function might be executed
3573+
* before service_dispatch_exec_io(). Since this function disabled IO events
3574+
* to monitor the main process above, we need to update the state here too.
3575+
* Let's consider the process is successfully launched and exited. */
3576+
service_enter_start_post(s);
3577+
}
35683578
}
35693579
}
35703580

0 commit comments

Comments
 (0)