Skip to content

Keep an idle cycle from outliving the screensaver it launched - #15931

Closed
anandude wants to merge 4 commits into
omacom:quattrofrom
anandude:fix/idle-cycle-stale-screensaver-windows
Closed

anandude wants to merge 4 commits into
omacom:quattrofrom
anandude:fix/idle-cycle-stale-screensaver-windows

Conversation

@anandude

@anandude anandude commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15066.

Problem

An idle cycle can stay armed after the screensaver it launched is gone. omarchy-shell idle status then keeps reporting inIdleCycle: true while the user works, and anything that reads that flag as "the user is idle" acts on a user who is not. In #15066 a plugin suspended the machine every few minutes during normal use.

The reporter's corrected journal shows a cycle that never cancels:

04:37:52.856Z idle-monitor: idle
04:37:52.856Z idle-cycle-start: screensaver=3600 lock=7200
04:37:52.856Z process-start: screensaver ...
04:37:53.775Z idle-monitor: active
04:37:53.775Z idle-monitor-active: screensaver cycle remains armed
04:37:54.599Z process-exit: screensaver exitCode=0 status=0
(nothing else from the idle service for the next hour)
05:37:52.858Z lock-system: lock-timeout

No idle-cycle-cancel line of any kind appears for that boot, and the grace-timer fallback (screensaver-not-running) never fires even though it is started with the cycle.

Cause

Both decisions that keep the cycle armed read screensaverWindowCount, which is accumulated from the Hyprland openwindow/closewindow raw events:

  • handleActiveSignal() ignores activity while screensaverWindowCount > 0, so every later active signal is swallowed while the count is wrong.
  • The launch grace timer cancels only when screensaverWindowCount === 0, and handleScreensaverWindowOpened() stops that timer as soon as one window opens, so the fallback is disabled for the rest of the cycle.

A tracked window whose closewindow event never arrives, or which closes while the count is already out of step, therefore keeps the cycle armed for as long as the session lasts. Nothing else can end it: the main idle monitor reported active once, from the launch itself, and stays active while the user works, so no further isIdleChanged transition arrives to re-evaluate.

Change

Ask Hyprland what the screensaver is doing now, at the moment the decision is made, instead of trusting the windows the events have accumulated.

  • IdleModel.screensaverWindowsFrom(entries, className) builds the set from the live toplevels: the windows whose app id is the screensaver's, counted by address, once each. Pure and unit tested.
  • Service.qml gains liveScreensaverWindows() (reads Hyprland.toplevels.values, answers null if the list cannot be read so a caller keeps what the events tracked) and syncScreensaverWindows(), which replaces the tracked set and logs screensaver-windows live=N tracked=M when the two disagree, so the next journal shows this case immediately.
  • handleActiveSignal() syncs before it decides, so a stale count can no longer hold activity off.
  • The launch grace timer syncs before it decides, and handleScreensaverWindowOpened() no longer stops it. By the time it fires, the screensaver has either stayed up, which keeps the cycle armed, or gone with the user's return, which ends it.
  • A Connections on Hyprland.toplevels watches the live window list itself, so the cycle also ends when the screensaver window goes away with no closewindow event at all: handleToplevelsChanged() syncs and cancels with screensaver-closed. It leaves the launch window to the grace timer and keeps the cycle armed while any screensaver window is still live.
  • The cycle therefore has two independent end-of-screensaver signals: the event, and the window leaving the toplevel list. The process exit is not one of them, since bin/omarchy-launch-screensaver exits as soon as the windows have opened.
  • When activity is held off with a screensaver window still tracked, the grace re-check is re-armed, so that cycle is re-evaluated three seconds later instead of waiting for an idle transition that may never come.

The guard itself is unchanged: the compositor still reports activity when the screensaver launches, and a cycle whose screensaver is genuinely up still stays armed for the lock deadline. The difference is that "the screensaver is up" is now read from Hyprland rather than from event bookkeeping that can drift.

Neighbours

Tests

bash test/shell.d/idle-test.sh passes (6 new model checks and 8 new wiring checks). ./test/shell fails the same 19 files as unmodified quattro on this machine, apart from the known flaky shell-idle-spawn-test.sh, which is not among them here.

@greptile-apps

greptile-apps Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issue was found.

Summary

The PR checks Hyprland's live windows before keeping an idle cycle armed.

  • The idle service checks live screensaver windows before keeping a cycle armed.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Change[Live window list changes] --> Grace{Launch grace running?}
  Grace -->|Yes| Timer[Grace timer checks live windows]
  Grace -->|No| Sync[Sync tracked windows]
  Sync --> Gone[Pass removed addresses to close handler]
  Gone --> Remaining{Any screensaver windows remain?}
  Remaining -->|Yes| Keep[Keep cycle armed]
  Remaining -->|No| Cancel[Cancel cycle and pending lock]
Loading

Reviews (3) · Last reviewed commit: "Let the close handler decide when the li..." · Reviewed by Greptile

Comment thread shell/plugins/services/idle/Service.qml Resolved
@omarchybot omarchybot added the bug Something isn't working label Oct 21, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 5d45d17 against quattro, by Claude Opus 5.5 with Codex Medium as the second opinion. This is not ready yet, for two reasons: the failure it fixes hasn't been seen, and the new reconciliation can drop a screensaver window that is really up.

The bug isn't reproduced. On a worker running this branch's shell (Hyprland, foot, one monitor), every real dismissal I could produce closed the screensaver's windows with matching closewindow events, and base cancels the cycle on those. The corrected journal in #15066 also fits correct behaviour. A cycle with screensaver=3600 lock=7200, the launch-time active held, the launcher exiting once its window mapped, and lock-timeout exactly an hour later is what a screensaver left up while the user is away looks like. And lockSystem() sets idledThisCycle = false before it runs the lock (Service.qml:81), so a missed closewindow can keep a cycle armed only until the lock deadline. It can't explain inIdleCycle still being true after the reporter unlocked, or suspends that went on until a reboot. Codex reached the same reading. I had already written mine into its brief, so take that as agreement rather than an independent result. Neither of us found a flow on base where the tracked set ends up with more windows than are open.

The live list can miss a screensaver window that is up. I measured this on the worker with a probe shell:

  • Hyprland.toplevels emits valuesChanged before the matching raw openwindow/closewindow reaches onRawEvent.
  • A newly mapped toplevel's wayland is still null at that point, and at the raw openwindow. It was filled in 5–49 ms later across five launches.
  • liveScreensaverWindows() reads a null wayland as an empty app id, so it doesn't count that window. syncScreensaverWindows() then replaces the tracked set, which drops a window the raw openwindow had already recorded.

Codex worked out where this costs the lock. Say a screensaver window maps in the last few tens of milliseconds before the 3 s grace ends. The grace timer's new sync erases it, and screensaver-not-running cancels the cycle, because the launch has already made the idle monitor report active. The same erase can happen in handleActiveSignal(), or on a multi-monitor launch where one screensaver window goes while another is still unassociated. In each case the screensaver stays on screen with no lock behind it. Base keeps the cycle armed in all three, because it trusts the raw openwindow and stops the grace timer on it. The window is narrow, but on a lock path it fails open. The fix is to treat a toplevel without a wayland handle as unknown rather than absent: keep it if the events already track it. Also, the toplevels change arrives first, so every dismissal on this branch now cancels as screensaver-closed and handleScreensaverWindowClosed() no longer decides it. That matters for #15244, whose output-loss guard lives in that function and would be bypassed if both land.

Tests. On the worker, test/shell.d/idle-test.sh (39 passed), shell-idle-spawn-test.sh, screensaver-workspace-test.sh, system-lock-test.sh and sleep-lock-test.sh all pass. I also drove real idle cycles at screensaver=4 lock=90: the launch-time active was held, the cycle stayed armed while an unrelated window opened and closed, and closing the screensaver ended it. The new Service.qml assertions in idle-test.sh match source text, so they would pass with the race above in place. The screensaverWindowsFrom checks do test behaviour.

What it waits on. It waits on the author: either evidence that the tracked set drifts on a real machine, or a smaller change. For example, the screensaver-windows live=N tracked=M log line on its own would catch the next occurrence on the reporter's four-monitor setup. Whatever reconciliation stays needs to keep a tracked window that is still unassociated. Nothing was pushed to the branch.

The live toplevel read treats a window without a wayland handle as having an
empty app id, so a screensaver window that maps inside the launch grace is
read as absent and the reconciliation drops it. The launch grace then finds
no screensaver window and cancels the cycle, and the screensaver stays on
screen with no lock behind it. Base keeps the cycle armed, so this fails open
on a lock path.

The handle is attached 5-49ms after the window maps, so the app id is unknown
at that point, not absent. Report it as null and keep such a window when the
openwindow events already track its address. A toplevel the events never saw
is still ignored, so an unidentified window cannot become a screensaver window
on its own.
The toplevels watcher cancelled the cycle itself, so a dismissal it noticed
first passed through a second cancel path. The window is gone either way, but
what a dismissal means then lives in two places, and the reason it records
depends on which path was quicker.

Hand each vanished window to handleScreensaverWindowClosed() instead, which is
what the closewindow event already uses, so the decision and its reason stay in
one place whichever path notices first.
@anandude

Copy link
Copy Markdown
Contributor Author

Both things you could act on are in.

The live list can miss a window that is really up — fc872d37. This was the most useful thing in your review, and it is a real fail-open. liveScreensaverWindows() passed "" for a toplevel whose wayland handle had not attached yet, "" reads as "not the screensaver", and the reconciliation then dropped a window the raw openwindow had already recorded. It now passes null, and screensaverWindowsFrom() takes the tracked set as a third argument: null means unknown, so the window is kept when the events already track its address and ignored when they do not. A toplevel the events never saw still cannot become a screensaver window on its own. All three paths you named — the grace timer's sync, handleActiveSignal(), and the multi-monitor launch — read through that one function, so all three keep the still-unassociated window now.

The toplevels path no longer decides on its own — 5d018954. You were right that a dismissal it notices first cancels as screensaver-closed, and that handleScreensaverWindowClosed() then returns early because cancelIdleCycle() has already reset the tracked set. That is worse than a reason string: whatever decides output loss in #15244 would have been skipped entirely. The watcher now hands each vanished window to handleScreensaverWindowClosed() instead of cancelling, so a dismissal has one decision point and one reason, and #15244's guard stays where it lives if both land. screensaver-closed is no longer used anywhere.

Tests. idle-test.sh is 47 assertions, all passing. That includes five new screensaverWindowsFrom cases for the unknown app id — tracked, empty tracked set, no tracked set, beside a non-screensaver window — plus the sentinel the QML emits and the handoff to the close handler. The kept-when-tracked case fails against the old code, which returned { windows: {}, count: 0 } for a window the events had already recorded. shell-idle-spawn-test.sh, screensaver-workspace-test.sh and system-lock-test.sh pass. sleep-lock-test.sh fails "warns that the session was left unlocked" both with and without this branch; I stashed and re-ran it to confirm that one is not mine.

What I still cannot answer. The first half of your "not ready" — that the drift has not been seen on a real machine — I cannot settle from here. I do not have the reporter's four monitors, and I agree with your reading that base is correct in every flow I can construct. That is the whole reason I added the log line: it was meant to catch the next occurrence, not to justify the reconciliation on its own. So the choice between your two routes is not mine to make:

Say which you want and I will do it. I would rather not sit in the queue classified as verified while it is the second option you asked for.

@greptile-apps

greptile-apps Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@anandude

Copy link
Copy Markdown
Contributor Author

Decision on the scope question above: keeping the reconciliation and the screensaver-windows live=N tracked=M log line in this pull request, rather than cutting back to the log line alone.

The reason is the fail-open in fc872d37. The reconciliation is what drops a window that is really up when its app id is not readable yet, and that is a lock path, so shipping the tracker without the reconcile-and-keep logic would leave a way to keep a cycle armed over a gone window while adding a way to end one early. With the unknown-app-id case handled, the mechanism keeps a still-unassociated window and only ends a cycle on a window the live list no longer has at all.

So what this waits on now is the same thing as before, and it is not something more review will settle: an occurrence of the drift on a real machine. The log line is there to catch it on #15066's setup.

@omarchybot

Copy link
Copy Markdown
Collaborator

Re-reviewed at 5d01895 against quattro, by Claude Opus 5.5, with Codex Medium and muse-spark-1.3-contributor (pi, thinking high) as second opinions. Both new commits do what they say. Two of the findings below come from those reviewers, and I checked both myself, one on a worker and one in Quickshell's source.

What's fixed. fc872d37 keeps a tracked window whose wayland handle hasn't attached yet, which closes the fail-open at the grace timer, in handleActiveSignal() and on a multi-monitor launch. 5d018954 sends a vanished window through handleScreensaverWindowClosed(), so a dismissal has one decision point again. On a worker running this branch's shell, closing the screensaver after the 3 s grace now cancels as screensaver-dismissed, and the later raw closewindow is ignored rather than handled twice. Swapping in the old IdleModel.js makes the new "keeps a live window with no app id yet" test fail, so that test checks real behaviour.

The log line can't tell drift from a normal close. In that same run the journal showed screensaver-windows: live=0 tracked=1 and then idle-cycle-cancel: screensaver-dismissed, on an ordinary close where no event was missing. Hyprland.toplevels changes before the raw closewindow arrives, so every dismissal after grace logs a mismatch. If the reporter's journal shows that line, it says nothing about whether an event was lost.

The live list doesn't back up the event stream. In Quickshell 0.3.2's source (src/wayland/hyprland/ipc/connection.cpp), a window leaves Hyprland.toplevels only when the closewindow socket event comes in, and that same event is then passed on as rawEvent in the same call. refreshToplevels() adds windows but never removes them. So if Quickshell never sees a closewindow, the toplevel list keeps the window too. The reconciliation would only help if the event reached onRawEvent and the handler then ignored it, and nobody has found a way for that to happen on base. The worker runs a Quickshell build from just after 0.3.2, so I checked the tagged source, not the build itself.

One interaction to know about. Opening a window no longer stops the launch grace timer. With #15244 applied, losing every output in the first 3 s after launch would be held by its guard in the close handler, but then the grace timer would cancel the cycle as screensaver-not-running anyway. This needs both patches, so it isn't a defect on its own.

Tests. On the worker: test/shell.d/idle-test.sh (47 passed), shell-idle-spawn-test.sh, screensaver-workspace-test.sh, system-lock-test.sh and sleep-lock-test.sh all pass. sleep-lock-test.sh passed there, so the failure you saw locally looks specific to your machine. The new Service.qml assertions match source text, so they don't exercise event ordering. The screensaverWindowsFrom cases do.

What it waits on. You've put the choice of route to the maintainer, and it stays there. Both reviewers and I still read #15066 as unexplained: base clears the cycle before it locks, and the reported cycle surviving an unlock doesn't fit a missed closewindow. For the choice: neither the reconciliation nor the live=N tracked=M line, as written, can catch the event loss this was meant to cover. A diagnostic that could catch it would log each raw openwindow/closewindow with its address, then report a tracked window still missing its close some time later. Nothing was pushed to the branch, and it isn't labelled verified, since the bug hasn't been reproduced.

@anandude

anandude commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded — upstream replaced the mechanism this was built on.

#16800 ("Replace the terminal screensaver with the native Wayland one"), merged 2026-10-28, contains e3a5298c ("Stop tracking screensaver windows in the idle service") along with 5c3303b8, c1e9e5cc and a87ab405 — "Close the screensaver whenever the user is back", "Stop the screensaver when the launch grace ends with the user back", "Do not lock a user who came back during the launch grace".

shell/plugins/services/idle/Service.qml and IdleModel.js on quattro now contain no screensaverWindows*, handleScreensaverWindowClosed, liveScreensaverWindows or screensaverWindowsFrom. The whole surface this PR adds, and the live-versus-tracked reconciliation it argued for, has been deliberately removed, and the user-back case is handled by the close handler upstream instead.

So this cannot be rebased — the conflict is not textual, it is that the code being patched no longer exists. Re-proposing it would mean rewriting against the native Wayland screensaver rather than porting this diff.

Worth recording that the review's central objection was never answered: the live=N tracked=M line could not tell a lost closewindow from an ordinary close, because Hyprland.toplevels drops a window before the raw event arrives. Neither the reconciliation nor the log line would have caught #15066, and the bug itself was never reproduced on base. That is a good reason not to carry it forward as-is.

If the native screensaver turns out to have the same outliving-cycle problem, that wants a fresh issue against the new implementation and a diagnostic that logs each raw openwindow/closewindow with its address, then reports a tracked window still missing its close later.

Thanks to everyone who reviewed and tested this one while it was open.

@anandude anandude closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Idle service: inIdleCycle stays true after the user returns when the screensaver's launch-time "active" signal is swallowed

2 participants