Skip to content

Commit a9f1b33

Browse files
DaanDeMeyerbluca
authored andcommitted
shutdown: Send EXIT_STATUS before final sync
There's a race condition where the EXIT_STATUS= message we send just before shutting down the VM doesn't arrive on the host, presumably because the VM is shut down before the kernel has had a chance to forward the message to the host. Since there's no obvious way to wait until the message has been flushed to the host, let's send the message before we execute the final sync() instead of after executing the final sync(). In my testing, this seems to either guarantee the message is sent or introduces sufficient delay that the kernel always has time to flush its socket buffers to the host. (cherry picked from commit c88753d) (cherry picked from commit 82afcbd)
1 parent 0292727 commit a9f1b33

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/shutdown/shutdown.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ int main(int argc, char *argv[]) {
388388
goto error;
389389
}
390390

391+
/* This is primarily useful when running systemd in a VM, as it provides the user running the VM with
392+
* a mechanism to pick up systemd's exit status in the VM. Note that we execute this as early as
393+
* possible since otherwise we might shut down the VM before the AF_VSOCK buffers have been flushed.
394+
* While this doesn't guarantee the message will arrive, in practice we do enough work after this
395+
* that the message should always arrive on the host */
396+
(void) sd_notifyf(0, "EXIT_STATUS=%i", arg_exit_code);
397+
391398
(void) cg_get_root_path(&cgroup);
392399
bool in_container = detect_container() > 0;
393400

@@ -583,10 +590,6 @@ int main(int argc, char *argv[]) {
583590
if (!in_container)
584591
sync_with_progress();
585592

586-
/* This is primarily useful when running systemd in a VM, as it provides the user running the VM with
587-
* a mechanism to pick up systemd's exit status in the VM. */
588-
(void) sd_notifyf(0, "EXIT_STATUS=%i", arg_exit_code);
589-
590593
if (streq(arg_verb, "exit")) {
591594
if (in_container) {
592595
log_info("Exiting container.");

0 commit comments

Comments
 (0)