Repository navigation
(2/3) Restore sleep if Sleepless quits, crashes or hangs in lid-closed mode - #16
Open
heartwerktech wants to merge 4 commits into
Open
heartwerktech wants to merge 4 commits into
heartwerktech wants to merge 4 commits into
Conversation
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>
This was referenced Nov 21, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this change?
If Sleepless crashed or hung while lid-closed mode was on,
pmset disablesleepstayed 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/shwatchdog loop, a script compiled into the binary. The loop restoressudo -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.shcompiles clean (zero warnings) and the app launchesToggled on/off and verified
pmset -g | grep SleepDisabledfollows the switch(If touching the grant/scripts)
./uninstall.shstill 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):SleepDisabled1 → 0, ownership flag followskill -9the appSleepDisabled=0, no watchdog process leftToggle and
kill -9ran again on headcd78b53. 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 (bundledgrant.shrun as root through anosascriptadministrator 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
🤖 Generated with Claude Code