Skip to content

(2/3) Restore sleep if Sleepless quits, crashes or hangs in lid-closed mode - #16

Open
heartwerktech wants to merge 4 commits into
Aboudjem:mainfrom
heartwerktech:split/2-crash-safety
Open

heartwerktech wants to merge 4 commits into
Aboudjem:mainfrom
heartwerktech:split/2-crash-safety

Conversation

@heartwerktech

@heartwerktech heartwerktech commented Nov 21, 2026 •

Copy link
Copy Markdown

Part 2 of 3. Merge order: 1/3 (#15) → 2/3 → 3/3 (#17). Builds on #15, so until that merges GitHub also shows its two commits. This PR's own commits are 0ebb9e6 and cd78b53.

What does this change?

If Sleepless crashed or hung while lid-closed mode was on, pmset disablesleep stayed at 1 until a reboot, and the Mac would never sleep on lid close.

This PR adds two protections. Signal handlers for SIGTERM, SIGINT and SIGHUP, plus quit, release the assertion and turn lid-closed mode off if this process turned it on. While lid-closed mode is on, the app also starts a small unprivileged /bin/sh watchdog loop, a script compiled into the binary. The loop restores sudo -n pmset -a disablesleep 0 (the off half of the existing grant) if the app process disappears, or if its lease file (~/Library/Caches/Sleepless/lid-lease, refreshed every 30 s) goes stale for 2 minutes. The loop exits when the app removes the lease. No UI change: the popover renders byte-identical to 1/3.

How was it tested?

  • ./build.sh compiles clean (zero warnings) and the app launches

  • Toggled on/off and verified pmset -g | grep SleepDisabled follows the switch

  • (If touching the grant/scripts) ./uninstall.sh still proves revocation (grant and scripts untouched)

  • Same gate as 1/3: CI compile, the 26.0-target compile, and the 13.0 and 26.0 bundles. All exit 0 with 0 warnings.

  • Crash and hang proofs. Each ran on the real grant, with the test build launched through LaunchServices (open -n):

Scenario Result
Lid on → off SleepDisabled 1 → 0, ownership flag follows
Lid on, then kill -9 the app restored to 0 within 6 s by the watchdog
Lid on, then SIGTERM restored to 0 immediately (signal handler)
Lid on, then main thread hangs still 1 during the hang, restored to 0 after 123 s (lease stale > 120 s)
After all runs SleepDisabled=0, no watchdog process left

Toggle and kill -9 ran again on head cd78b53. SIGTERM and the hang ran one commit earlier; App.swift differs from the head by one comment line.

macOS version tested: 26.3 (25D5101c) · Mac model: Mac16,8

Security model

The grant is unchanged and still matches exactly two literal commands. What is new is a second process that calls sudo, limited to the off command. SECURITY.md and docs/AUDIT.md now describe the loop, its lease file and its one privileged command, and no longer claim "no /bin/sh -c". They also name the existing one-time setup (bundled grant.sh run as root through an osascript administrator prompt) as the other shell.

Risk

Higher than 1/3: it adds a background process that runs a privileged command, and it changes the exit paths of the lid-closed mode.

How to revert

Revert the merge commit. A watchdog that is already running exits once its lease file is gone: rm ~/Library/Caches/Sleepless/lid-lease.

Checklist

  • No personal paths/usernames added to scripts, the sudoers template, or install commands
  • No new third-party dependencies or kexts. The watchdog is a child shell loop, not an installed daemon. It runs only while lid-closed mode is on.
  • Docs/translations updated (README + translations, SECURITY.md, docs/AUDIT.md, CHANGELOG)
  • I agree my contribution is licensed under the MIT License

🤖 Generated with Claude Code

heartwerktech and others added 4 commits November 21, 2026 22:07
Keep Mac awake holds an IOKit PreventUserIdleSystemSleep assertion (no root,
released when the app exits) and is on at launch. Allow lid closed is the
existing pmset disablesleep mode, now a separate opt-in switch. The auto-off
timer becomes a slider (Off/15m/30m/1h/2h/4h/8h) and the menu-bar glyph tells
awake (outline cup) from lid closed (solid cup). README, translations and
CHANGELOG updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signal handlers release the assertion and the lid-closed setting on
SIGTERM/SIGINT/SIGHUP and on quit. While lid-closed mode is on, a detached
unprivileged watchdog loop restores pmset disablesleep 0 with the existing
grant if the app dies or its lease goes stale for 2 minutes. SECURITY.md,
docs/AUDIT.md, README and translations describe the new shell loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…UDIT.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@heartwerktech

Copy link
Copy Markdown
Author

Notes from review: a hung app ignores a plain kill, because SIGTERM is handled on the main queue, so the idle-sleep assertion stays until SIGKILL or Force Quit; the watchdog still restores lid-closed mode after 2 minutes. Not exercised yet: SIGINT, SIGHUP, Quit from the popover, and a long lid-closed run with the popover shut (App Nap could stall the 30 s lease refresh). A clock jump or a cache cleaner deleting the lease file also turns lid-closed mode off early. All of these fail toward the Mac sleeping.

This branch has not been deployed

No deployments
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.

1 participant