Skip to content

Suspend automatically after idle timeout - #7210

Closed
vander00 wants to merge 3 commits into
omacom:quattrofrom
vander00:idle-suspend
Closed

vander00 wants to merge 3 commits into
omacom:quattrofrom
vander00:idle-suspend

Conversation

@vander00

Copy link
Copy Markdown
Contributor

Add a configurable idle suspend deadline, default is 900 seconds.
I have made it myself, the docs are written by AI. I've read them all, they must be correct.

Refers to #6931.

Copilot AI balanced review requested due to automatic review settings August 17, 2026 01:48

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@mabster

mabster commented Aug 17, 2026 •

Copy link
Copy Markdown

(Full disclosure - OpenCode wrote this comment!)

Hi vander00 — nice work on this. The docs, migration, and migration test are thorough. I've been working on the same feature locally and wanted to flag two edge cases I hit during testing.

1. No lock guard — suspendSystem can sleep an unlocked session

Their suspendSystem() runs bare systemctl suspend:

runProcess(suspendProcess, "suspend", "systemctl suspend")

When the suspend timer fires mid-lock (the normal case), the screen is already locked, so this works. But if something goes wrong — the lock process is slow, the user is between screens, etc. — there is nothing preventing a suspend on an unlocked session.

The fix is a guard in the bash command:

[[ $(omarchy-shell lock isLocked 2>/dev/null) == "true" ]] && systemctl suspend

The lock-before-suspend path (omarchy-sleep-lock.service) secures the screen on PrepareForSleep, but the guard removes the race window entirely.

2. suspend <= lock suspends before lock fires

When suspend is less than or equal to lock (e.g. "suspend": 10, "lock": 300), firstIdleTimeoutSeconds becomes the suspend value, and suspendDelaySeconds becomes 0. This triggers suspendSystem() immediately at cycle start — before the lock fires, on an unlocked session.

Tested live (suspend=10, lock=20): the cycle started, screensaver launched, and systemctl suspend ran immediately at cycle start — 20 seconds before the lock would have fired.

The fix: only arm the suspendTimer when suspend > lock (it fires mid-lock at the correct idle+suspend time, the screen is already locked). For suspend <= lock, add a backstop on lockProcess.onExited that suspends at lock completion:

function suspendAtLockIfDue() {
  if (root.suspendTimeoutSeconds < 0) return
  if (root.suspendTimeoutSeconds > root.lockTimeoutSeconds) return
  if (!root.idleEnabled) return
  root.suspendSystem("suspend-at-lock")
}

Then in startIdleCycle:

if (root.suspendTimeoutSeconds > root.lockTimeoutSeconds) {
  suspendTimer.restart()
}

And in lockProcess.onExited:

onExited: function(exitCode, exitStatus) {
  root.logEvent("process-exit", "lock exitCode=" + exitCode + " status=" + exitStatus)
  root.suspendAtLockIfDue()
}

This also means lockSystem should keep clearing idledThisCycle as the original code does — the backstop gates on idleEnabled only (the isLocked guard in suspendSystem protects an unlocked session; lockSystem is only reachable from an active cycle anyway).

3. Minor: null trap in config

"suspend": null in shell.json → Number(null) = 0 → immediate suspend. The existing secondsFromConfig function treats this as a valid zero. A type check at the top of secondsFromConfig rejects non-number/non-numeric-string values and falls back to the default, which also hardens the screensaver/lock paths:

function secondsFromConfig(value, fallback) {
  var n
  if (typeof value === "number") n = value
  else if (typeof value === "string" && value.trim() !== "") n = Number(value)
  else return fallback
  if (!isFinite(n) || n < 0) return fallback
  return Math.floor(n)
}

Happy to contribute these fixes to your branch if useful. I've been running the hardened version locally for a few days and it's solid — the suspend <= lock case was the one that took the most testing to get right.

@vander00

Copy link
Copy Markdown
Contributor Author

(Full disclosure - OpenCode wrote this comment!)

Hi vander00 — nice work on this. The docs, migration, and migration test are thorough. I've been working on the same feature locally and wanted to flag two edge cases I hit during testing.

1. No lock guard — suspendSystem can sleep an unlocked session

Their suspendSystem() runs bare systemctl suspend:

runProcess(suspendProcess, "suspend", "systemctl suspend")

When the suspend timer fires mid-lock (the normal case), the screen is already locked, so this works. But if something goes wrong — the lock process is slow, the user is between screens, etc. — there is nothing preventing a suspend on an unlocked session.

The fix is a guard in the bash command:

[[ $(omarchy-shell lock isLocked 2>/dev/null) == "true" ]] && systemctl suspend

The lock-before-suspend path (omarchy-sleep-lock.service) secures the screen on PrepareForSleep, but the guard removes the race window entirely.

2. suspend <= lock suspends before lock fires

When suspend is less than or equal to lock (e.g. "suspend": 10, "lock": 300), firstIdleTimeoutSeconds becomes the suspend value, and suspendDelaySeconds becomes 0. This triggers suspendSystem() immediately at cycle start — before the lock fires, on an unlocked session.

Tested live (suspend=10, lock=20): the cycle started, screensaver launched, and systemctl suspend ran immediately at cycle start — 20 seconds before the lock would have fired.

The fix: only arm the suspendTimer when suspend > lock (it fires mid-lock at the correct idle+suspend time, the screen is already locked). For suspend <= lock, add a backstop on lockProcess.onExited that suspends at lock completion:

function suspendAtLockIfDue() {
  if (root.suspendTimeoutSeconds < 0) return
  if (root.suspendTimeoutSeconds > root.lockTimeoutSeconds) return
  if (!root.idleEnabled) return
  root.suspendSystem("suspend-at-lock")
}

Then in startIdleCycle:

if (root.suspendTimeoutSeconds > root.lockTimeoutSeconds) {
  suspendTimer.restart()
}

And in lockProcess.onExited:

onExited: function(exitCode, exitStatus) {
  root.logEvent("process-exit", "lock exitCode=" + exitCode + " status=" + exitStatus)
  root.suspendAtLockIfDue()
}

This also means lockSystem should keep clearing idledThisCycle as the original code does — the backstop gates on idleEnabled only (the isLocked guard in suspendSystem protects an unlocked session; lockSystem is only reachable from an active cycle anyway).

3. Minor: null trap in config

"suspend": null in shell.json → Number(null) = 0 → immediate suspend. The existing secondsFromConfig function treats this as a valid zero. A type check at the top of secondsFromConfig rejects non-number/non-numeric-string values and falls back to the default, which also hardens the screensaver/lock paths:

function secondsFromConfig(value, fallback) {
  var n
  if (typeof value === "number") n = value
  else if (typeof value === "string" && value.trim() !== "") n = Number(value)
  else return fallback
  if (!isFinite(n) || n < 0) return fallback
  return Math.floor(n)
}

Happy to contribute these fixes to your branch if useful. I've been running the hardened version locally for a few days and it's solid — the suspend <= lock case was the one that took the most testing to get right.

Hello.
I am very pleased for the review. I am also very sorry that I couldn't reply earlier because of my business.
I have checked the lock thing. It must lock every time the 'systemctl suspends' is executed. I do not remember how it works exactly but it looks like some omarchy thing. The default shell's system menu uses the 'systemctl suspend' as well.

@bjarneo

bjarneo commented Sep 21, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #11686, which covers the same idle suspend timeout. I keep that one open and close this one to consolidate review.

If you feel this is the wrong decision, please open the PR again with a note on the difference.

@bjarneo bjarneo closed this Sep 21, 2026
@vander00
vander00 deleted the idle-suspend branch September 25, 2026 19:54
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