Skip to content

Prevent lid closure from interrupting shutdown - #13076

Merged
dhh merged 3 commits into
omacom:quattrofrom
chemineer1:codex/shutdown-lid-inhibit
Oct 4, 2026
Merged

dhh merged 3 commits into
omacom:quattrofrom
chemineer1:codex/shutdown-lid-inhibit

Conversation

@chemineer1

@chemineer1 chemineer1 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Choosing Shut Down and closing the lid can suspend the laptop instead of shutting it down. Reopening the lid resumes the session. See #4273.

Fix

Add sleep:idle:handle-lid-switch inhibition through shutdown preparation, the two-second delay, and the power-off request. The lid-specific lock is needed because ordinary sleep inhibitors do not normally block lid-triggered suspend.

Start the user service immediately and move the delay inside it. Run preparation in the background and add a 30-second runtime limit to initiate cleanup if the worker stalls.

Show a critical notification if inhibitor acquisition or the power-off request fails, while preserving the failure status.

Testing

  • Eight focused tests pass, including blocked window closing and failure notifications. Real-systemd checks verify inhibitor lifetime and cleanup.
  • The original patch passed physical shutdown tests with the lid open and closed on a Dell XPS 13 DX13260 (Omarchy 4.0.4-1, September 19).

@chemineer1
chemineer1 marked this pull request as ready for review September 24, 2026 01:11
@llstrk

llstrk commented Sep 24, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Verified: the change addresses the reported problem. With the handle-lid-switch block lock held, logind refuses lid-triggered suspend during shutdown preparation, the 2-second delay and the power-off request. One narrow gap remains while the lock is being acquired. If a lid-triggered suspend wins that gap, the shutdown is dropped without any notice.

Lid closed during... Base (quattro) This PR
the 2 s delay Suspend starts. The queued power-off is refused as "already in progress" (the reported bug) Lid handling is refused. Power-off proceeds
the moment after systemctl poweroff is accepted Not applicable Safe. logind's pending power-off and 30 s lid holdoff refuse lid handling after the lock is released

Verified from systemd v261.3 source:

  • handle-lid-switch is the right lock. The default LidSwitchIgnoreInhibited=yes makes logind ignore a plain sleep inhibitor for lid events, as the PR comment says.
  • A lock held from a transient systemd-run --user service counts as active, and the default polkit policy allows it for local sessions.
  • The lock does not block the power-off request.
  • Type=exec still reports scheduling failures to the caller.

Shutdown is silently dropped if a lid suspend wins the lock-acquisition race

systemd-run returns once systemd-inhibit has been exec'd (Type=exec), not once the lock is held. If logind starts a lid-triggered suspend before it finishes processing the Inhibit call, the request is rejected because sleep is one of the requested operations:

Shut Down -> systemd-run returns 0 (systemd-inhibit exec'd)
          -> lid closed; logind records the pending suspend
          -> Inhibit(sleep:idle:handle-lid-switch)
             <- org.freedesktop.login1.OperationInProgress
                "The operation inhibition has been requested for is already running"
          -> systemd-inhibit exits 1; the --inhibited worker never runs; no retry
          -> suspend; after resume: windows still open, no OSD, no power-off queued

(Source trace through method_inhibit in src/login/logind-dbus.c and src/login/inhibit.c. It was not reproduced on hardware.)

Impact: low likelihood, but a confusing result when it occurs. The window covers process start, a bus connection and the polkit checks. That is an estimated milliseconds to tens of milliseconds on an idle system, usually shorter than the time needed to close a lid after choosing Shut Down, but it is not bounded under load. Inside that window, the base still queued a power-off attempt and closed windows. This PR instead leaves the session untouched with no user-visible sign that shutdown failed. The same silent path applies whenever Inhibit is refused for another reason, such as a custom polkit rule or a logind error.

Suggested change: do not let an inhibitor failure end the shutdown silently. Optional approaches:

  • when systemd-inhibit exits with an error, fall back to the previous unprotected path (OSD, window close and a timer-scheduled systemctl poweroff);
  • or at least show an OSD or notification that shutdown did not start.

Optional test improvement: test/shell.d/system-power-test.sh does not check the "cannot delay power-off" property the script comment describes. With the trailing & removed from the preparation subshell, all six checks still pass in the sandbox, because the sleep 2 stub only waits until window close has been logged. A window-close stub that blocks until the systemctl poweroff stub has run, with a bounded timeout that fails the test, would catch preparation moving back into the foreground.


Review information

Test scope: The review covered source for both changed files, their callers and helpers, and the relevant systemd v261.3 logind, systemd-inhibit and systemd-run code, cross-checked against v262 and polkit 127. The PR's test/shell.d/system-power-test.sh ran in an isolated sandbox with stubbed power commands. It passed on the head in 5 runs and failed on the base script at the expected shutdown assertion. No real systemctl, systemd-inhibit, lid events or hardware were exercised. The physical shutdown results in the PR description were not reproduced, and the lock-acquisition window is estimated from source, not measured.

AI process: Opus 5.5 Medium coordination and synthesis, independent Opus 5.5 Xhigh and GPT 6 Sol Xhigh technical assessments, with follow-up source checks on the lock-acquisition race, and an Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

@chemineer1

Copy link
Copy Markdown
Contributor Author

@llstrk Addressed in eaaca77: added failure notifications and a regression test for blocked window closing. Eight focused tests and real-systemd checks with dummy power commands pass.

@llstrk

llstrk commented Sep 27, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Follow-up to the earlier review and the author's reply, checked at eaaca77.

Verified: both earlier points are resolved. One new low-likelihood robustness point is noted below.

Earlier point Status at eaaca77
Shutdown silently dropped if Inhibit is refused (for example, a lid suspend winning the lock-acquisition race) Resolved. Any systemd-inhibit failure now sends a critical "Shutdown failed" notification and keeps the original exit status
Optional test: nothing checked that preparation cannot delay power-off Resolved. The new blocked-window-close test fails in 3 of 3 runs when the trailing & is removed

Verified for the notification path:

  • A refused Inhibit makes systemd-inhibit exit 1, which runs the new || { ... } block.
  • The saved status survives a failing notification, as the new test 8 checks. Diagnostic mutants that drop the notification, force exit 1, take the status from the notification or always notify were each caught by the head test.
  • In the Omarchy shell, critical notifications do not expire, and the default omarchy-action sender bypasses Do Not Disturb. The message is therefore still on screen after a lid-triggered suspend and resume. The race still cancels the shutdown, but the user is now told to retry.
  • The same branch also reports a rejected systemctl poweroff. At the earlier head, that case closed windows and then failed silently.
  • The four verified notes from the earlier review still hold with systemd-inhibit moved inside the service. These are the lid lock choice, the lock counting as active from a systemd-run --user service, the lock not blocking the power-off request, and Type=exec still returning scheduling failures to the caller.

Author claims:

  • The failure notifications and the blocked-window-close regression test are confirmed.
  • The test file emits exactly 8 ok lines. It passed in 10 of 10 sequential and 24 of 24 concurrent sandbox runs.
  • The "real-systemd checks" were not reproduced.

Worker path now comes from the service environment

At the earlier head, the caller's shell expanded "$OMARCHY_PATH/bin/omarchy-system-shutdown" --inhibited. It now expands inside the transient service, so it uses the user manager's OMARCHY_PATH:

caller:  systemd-run ... "$OMARCHY_PATH/bin/omarchy-system-shutdown" --inhibit      <- caller's OMARCHY_PATH
service: systemd-inhibit ... "$OMARCHY_PATH/bin/omarchy-system-shutdown" --inhibited  <- user manager's OMARCHY_PATH

bin/omarchy-restart-shell notes that these two can differ after omarchy-dev-link or omarchy-dev-unlink, because the user manager keeps the environment from session start.

Impact: low likelihood, from source and a stubbed emulation, not reproduced on a real system. In that window, Shut Down runs one copy's wrapper and another copy's worker. If the other copy has been removed, systemd-inhibit cannot start the worker. The user then sees "Shutdown failed" on every attempt until re-login or reboot.

Suggested change: use "$0" --inhibited. The service runs the script by the absolute path the caller expanded, so both levels stay on the same copy, as at the earlier head. This variant passed the PR test in a sandbox run.

Optional notes:


Review information

Test scope: Source review of both changed files at eaaca77, their callers, omarchy-notification-send and the shell notification plugin, and systemd v261.3 logind, systemd-inhibit and systemd-run code (key paths cross-checked against v262). The PR's test/shell.d/system-power-test.sh ran in an isolated sandbox with stubbed power commands, alongside diagnostic mutants of the shutdown script, the earlier-head and base scripts, and a stop-signal emulation. No real systemctl, systemd-inhibit, logind, polkit, notification daemon, lid events or hardware were exercised. The worker-path point rests on source and a stubbed emulation. The reboot note is from source only. Related open PRs were read at their current heads.

AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical follow-up review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

@chemineer1

Copy link
Copy Markdown
Contributor Author

@llstrk Fixed in bedc2a3: the inhibited worker now uses "$0" to stay on the same script path. Added a regression test for a stale service OMARCHY_PATH; it fails before the fix and passes afterward. All nine focused tests pass.

@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
@llstrk

llstrk commented Sep 28, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Follow-up to the earlier follow-up review and the author's reply, checked at bedc2a3.

Verified: the worker-path point is resolved. No regressions were found in the new commit.

Earlier point Status at bedc2a3
Worker path came from the user manager's OMARCHY_PATH Resolved. The worker is now "$0" --inhibited, the same absolute path the caller passed to systemd-run
Optional notes (external stop sends no notification, reboot keeps the timer design, related open PRs) Unchanged by this commit. The related-PR list gains one entry, below

Verified for the new commit:

  • systemd-run makes the executable path absolute against the caller's working directory without resolving symlinks, and the service manager execs that path directly (systemd v261.3 and v262 source). In --inhibit mode, $0 is therefore the path the caller expanded. The service's OMARCHY_PATH no longer selects the worker script.
  • In a sandbox emulation with the service OMARCHY_PATH pointing at a missing directory, the head script ran its own worker and reached systemctl poweroff. The eaaca77 line instead failed with "No such file or directory" and sent "Shutdown failed".
  • Launching through PATH, omarchy shutdown, omarchy system shutdown or a relative path all passed the same absolute path to systemd-run. A symlinked checkout path stays as spelled and still runs.

Author claims: all confirmed.

  • The new stale-OMARCHY_PATH test fails in 5 of 5 runs when only the eaaca77 shutdown script is restored, and passes on the head.
  • The test file emits exactly 9 ok lines. It passed in 10 of 10 sequential and 24 of 24 concurrent sandbox runs.

Optional notes:

  • Open PR Fall back when reboot has no session D-Bus #11151 ("Fall back when reboot has no session D-Bus") also edits bin/omarchy-system-shutdown and the power test. It keeps the base timer and adds a systemd-run --system fallback when the --user call fails, for example without a user bus. This PR schedules only through systemd-run --user, so combining the two needs a decision on whether that fallback carries over to the new service.
  • The PR description still says "Eight focused tests pass". There are now nine.

Review information

Test scope: Source review of the eaaca77..bedc2a3 change, the shutdown script's callers and the omarchy dispatcher, systemd v261.3 and v262 systemd-run, service-manager exec and systemd-inhibit code, and related open PRs at their current heads. The PR's test/shell.d/system-power-test.sh ran in an isolated sandbox with stubbed power commands, alongside a copy with only the earlier shutdown script restored and an emulation of the service's final exec. No real systemd-run, user manager, systemd-inhibit, logind, notification daemon or hardware was exercised. The external-stop note was not re-run, because nothing on that path changed.

AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical follow-up review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Oct 3, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at bedc2a39792d0af05f80446af4806f7482c38327 and reproduced on real systemd. The fix works, and nothing needs changing.

Reproduction. I used a disposable Omarchy worker VM running systemd 261, with a uinput lid switch, logind set to suspend on lid close, and systemd-suspend.service stubbed to an 8-second sleep so the VM survives the "suspend". On quattro (821ae589), choosing Shut Down and closing the lid half a second later made logind start a suspend, and the timer's power-off was refused with Action suspend already in progress, refusing requested poweroff operation. The machine stayed up. On this branch merged with quattro, the same sequence held the sleep:idle:handle-lid-switch block lock ("Shutdown in progress"), PreparingForSleep stayed false, logind started no suspend, and the machine powered off. The only difference between the two runs is bin/omarchy-system-shutdown, so reverting the fix brings the bug back.

Tests. I ran test/shell.d/system-power-test.sh (9 passed) and bin-style-test.sh on the worker, at the head and merged with quattro. The power test fails with only the shutdown script reverted. test/cli passes once its vscode theme checks are excluded, which fail the same way on quattro itself. The branch merges into quattro cleanly.

Second opinion. Codex at medium effort also reviewed this and found the change proportionate, with no privilege change and no broken caller. It raised the same point as the community review's optional note: if RuntimeMaxSec=30s expires, for example while an inhibitor or power-off request hangs waiting for authorization, systemd stops the shell before its || block runs, so no "Shutdown failed" notification appears. That is real but only happens when something is already hung. quattro has no failure notification at all, so this changes nothing for the worse. Its independence from this review is not guaranteed.

Worth knowing, not blocking:

This now waits on the maintainer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready Good to merge verified Omarchy Triage has verified that this issue is ready for final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants