Skip to content

The nights: a sleep on battery ends in a power-off after N minutes - #108

Merged
CoreyH merged 2 commits into
mainfrom
nights/off-after
Nov 16, 2026
Merged

CoreyH merged 2 commits into
mainfrom
nights/off-after

Conversation

@CoreyH

@CoreyH CoreyH commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Corey OK'd this on 2026-11-16: a lid closed on battery may mean "sleep, then power off after 20 to 30 minutes" (the session is lost). s2idle draws about 2 W here, and on 2026-11-14 a lid closed at 50% ran the Mac flat overnight.

  • sleep-drain-off-after, a systemd-sleep hook. Before a suspend on battery it arms the PMU's RTC alarm N minutes out (in the RTC's own seconds) and writes a state file. When the alarm ends the sleep and the lid is still closed, it powers off, through a detached finish step under a lid-switch inhibitor: logind refuses a power-off while its suspend job is open, and that job ends only when the hook returns. An alarm someone else armed (a timed test) is left alone, and so is that sleep. A spurious wake keeps the deadline. Charger in, lid opened, or lid state unknown: no power-off. Never in a safe boot.
  • sleep-drain-alarm.service loads the alarm drivers of rtc-alarm-test/ at boot (until t600x: PMU RTC wake alarm (SPMI notify at 192, Maverick event clear, DT) omacom/linux-aurora#351 merges), only when the feature is on and not in a safe boot. install.sh builds them for the running kernel; after a kernel update load refuses them before touching a driver, and sleeps go on as before.
  • sleep-drain-nights on [MINUTES] | off switches it. Off by default.
  • tests/off_after_test.py: 154 hardware-free checks.

Not yet run on the Mac: the boot-time load and a real lid-close are the first live trial, with a short limit.

🤖 Generated with Claude Code

s2idle draws about 2 W here, so a lid closed overnight on battery runs the
Mac flat (2026-11-14). Corey OK'd on 2026-11-16 that a lid close on battery
may mean: sleep, then power off after 20 to 30 minutes.

sleep-drain-off-after is a second systemd-sleep hook. With
/etc/sleep-drain/off-after there (its content is the minutes, default 25) it
arms the PMU's RTC alarm before a suspend on battery and powers off when the
alarm ends the sleep with the lid still closed, or with the lid open before
and after within 20 s of the alarm (an idle suspend). It leaves an alarm that
isn't its own (rtcwake -m no) and that sleep alone, clears its alarm on every
other way out, and does nothing in a safe boot. An early wake with the lid
closed keeps its deadline for the sleep that follows within 120 s.

Three things differ from the first design, each from reading the code:

- The power-off runs in a "finish" step that post starts through systemd-run
  under a handle-lid-switch block inhibitor. logind (262) refuses PowerOff
  with "already in progress" until its suspend job is gone, and that job ends
  only after the post hooks return; systemctl doesn't fall back on that
  error. Once the job is gone, logind's lid re-check may suspend again before
  the power-off is asked for, which the inhibitor prevents. finish makes the
  5 s lid and charger re-check, then asks each second for up to 30 s.
- Every time is the alarm RTC's since_epoch, not the system clock. The
  kernel compares wakealarm with the RTC's time, this kernel has no
  RTC_SYSTOHC, and the RTC reads 1970 after the PMU has lost power.
- A sleep that isn't armed drops any older state of ours, so its post can't
  act on a deadline from an earlier sleep.

sleep-drain-alarm.service loads the alarm drivers at boot from copies that
install.sh builds into /usr/local/lib/sleep-drain/rtc-alarm/. The safe boot's
marker is written in the initramfs, so the unit's condition sees it.
sleep-drain-nights on [MINUTES] | off | status switches all of it.

tests/off_after_test.py: 154 checks, hardware-free.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

A pending power-off can still execute after the feature is switched off. Kernel-update recovery also leaves the alarm inactive after rebuilding.

Reviewed changes — Reviewed the battery-sleep deadline, detached power-off workflow, driver loading, controls, and installation changes.

  • Timed power-off: Adds RTC arming, deadline preservation across early wakes, AC/lid checks, and an inhibited shutdown worker.
  • Driver loading and controls: Adds the conditional boot service and sleep-drain-nights CLI, with safe-boot exclusions.
  • Installation and documentation: Builds and installs the alarm modules and new tools, updates removal, and documents their behavior.
  • Validation: All 154 new checks, existing RTC loader/check and drain-validation suites, Bash syntax checks, and ShellCheck passed; live hardware behavior remains untested.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using gpt-6.1-sol | 𝕏

Comment thread sleep-drain-off-after Resolved
Comment thread install.sh Resolved
… the alarm unit after a rebuild (review of #108)

sleep-drain-nights off during the hook's finish step didn't stop the
power-off: finish went by the charger and the lid only, and its refusal path
could write the removed state again. still_wanted() now also needs the flag
and the state file (why=switched-off), it runs right before every power-off
request, and once more before the state is saved after 30 refusals, so a
state that off removed isn't written again. off also stops the finish unit.

After a kernel update the enabled sleep-drain-alarm.service has failed on
the old modules. install.sh, once the alarm modules are rebuilt and after
daemon-reload, now starts it if it is enabled and the alarm RTC isn't there.
Not fatal, and never over loaded drivers, where load stops at "described
already".

sleep-drain-nights says in its header that on and off run through pkexec,
and puts the system's directories on PATH itself.

tests/off_after_test.py: 178 checks. The real sleep-drain-nights off runs in
the middle of the re-check and of the refused requests; install.sh's two
functions run as they stand against stub systemctl; nights runs as uid 0
with only the stubs on PATH.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes — Reviewed 8ca4531, the changes since the previous Pullfrog review.

  • Canceled pending shutdowns when the feature is disabled, including during shutdown retries, and stopped the detached worker.
  • Recovered alarm loading after rebuilds by starting the already-enabled unit only when matching modules were built and the alarm RTC is absent.
  • Supported restricted command paths for pkexec invocation and documented the root commands.
  • Expanded regression coverage for cancellation, installer recovery, and root invocation. All 178 checks, Bash syntax checks, and ShellCheck passed; live hardware validation remains outstanding.

Pullfrog  | View workflow run | Using gpt-6.1-sol | 𝕏

@CoreyH
CoreyH merged commit ef6c118 into main Nov 16, 2026
1 check passed
CoreyH added a commit that referenced this pull request Nov 16, 2026
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