Skip to content

Fix stuck systemctl poweroff could block terminate#40866

Open
chemwolf6922 wants to merge 5 commits into
masterfrom
user/chemwolf6922/fix-stuck-systemctl-poweroff-blocking-terminate
Open

Fix stuck systemctl poweroff could block terminate#40866
chemwolf6922 wants to merge 5 commits into
masterfrom
user/chemwolf6922/fix-stuck-systemctl-poweroff-blocking-terminate

Conversation

@chemwolf6922

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

The systemctl poweroff call is async after the operation is enqueued per https://man7.org/linux/man-pages/man1/systemctl.1.html. However, in some rare cases it could block. And that will block the wsl --terminate call. For example: #40433 (comment)
This PR moves the calling logic to a separate thread, so the fallback timeout is always started in the systemd path.

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

No new tests are added as the original failure could not be easily reproduced.

Copilot AI review requested due to automatic review settings June 22, 2026 08:17
@chemwolf6922 chemwolf6922 requested a review from a team as a code owner June 22, 2026 08:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the Linux-side instance termination path in systemd mode so that a potentially-blocking systemctl poweroff invocation cannot prevent the termination timeout/fallback (reboot(RB_POWER_OFF)) from being reached.

Changes:

  • Run systemctl poweroff on a detached thread to avoid blocking wsl --terminate when systemctl hangs.
  • Always start the existing BootInitTimeout-based fallback timer on the main path and force reboot(RB_POWER_OFF) on timeout.

Comment thread src/linux/init/init.cpp Outdated
Comment thread src/linux/init/init.cpp Outdated
{
std::this_thread::sleep_for(std::chrono::milliseconds(Config.BootInitTimeout));
LOG_ERROR("systemctl poweroff did not terminate the instance in {} ms, calling reboot(RB_POWER_OFF)", Config.BootInitTimeout);
std::thread([]() {

@benhillis benhillis Jun 22, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a local std::thread(...).detach(), use the existing UtilCreateChildProcess helper. This will simplify the code.

    if (Config.BootInit && !Config.BootStartWriteSocket)
    {
        THROW_LAST_ERROR_IF(UtilSetSignalHandlers(g_SavedSignalActions, false) < 0);

        //
        // systemctl poweroff is normally async but can block in rare cases. Run it in a
        // child process so a stuck invocation can't prevent the fallback timeout below.
        //
        UtilCreateChildProcess("poweroff", []() {
            if (UtilExecCommandLine("systemctl poweroff", nullptr) < 0)
            {
                LOG_ERROR("systemctl poweroff failed, calling reboot(RB_POWER_OFF)");
                reboot(RB_POWER_OFF);
            }
        });

        std::this_thread::sleep_for(std::chrono::milliseconds(Config.BootInitTimeout));
        LOG_ERROR("systemctl poweroff did not terminate the instance in {} ms, calling reboot(RB_POWER_OFF)", Config.BootInitTimeout);
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a new process for this?

Comment thread src/linux/init/init.cpp Outdated
Comment thread src/linux/init/init.cpp Outdated
Copilot AI review requested due to automatic review settings June 23, 2026 03:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/linux/init/init.cpp
Comment thread src/linux/init/init.cpp Outdated
Copilot AI review requested due to automatic review settings June 24, 2026 02:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/linux/init/init.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants