Skip to content

The display-awake assertion can be released immediately after a tool call refreshes its idle timer #8564

Open

Description

@soarkit

Summary

release_when_idle() reads remaining() under GUARD, drops the lock, and then calls release_now(), which re-acquires GUARD and takes the guard without rechecking last_activity. note_tool_activity() runs immediately before every tool dispatch; if it acquires GUARD between those two critical sections, it refreshes last_activity and returns because the guard is still Some, and release_now() still releases the PreventUserIdleDisplaySleep assertion. No release thread remains for that activity, so the tool call that was just recorded runs without the assertion until a later tool call re-arms it. This contradicts the documented contract "Hold the assertion now and keep it until idle passes with no activity", and can let the display sleep during a tool call that starts just as the idle window expires.

Environment

  • Repository: trycua/cua
  • Commit: b1633ef91cb003ce1551c3f81accab173e7b603e (fix(cua-driver): report a locked screen and keep the display awake while an agent works (#5483))
  • File: libs/cua-driver/rust/crates/platform-macos/src/display_awake.rs:122-155 (note_activity, release_when_idle, release_now)
  • Caller: libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs, where crate::tool_activity::note_tool_activity() runs before tool.invoke(args)
  • macOS 14 or later with CUA_DRIVER_KEEP_DISPLAY_AWAKE set (default 600 seconds); Rust 1.97.1

Expected behavior

The assertion stays held while any tool activity is within the idle window. If a tool call refreshes last_activity after the release thread has observed an expired window, the release must not happen: the thread should recompute the remaining time and sleep again.

Actual behavior

With no activity during the decision, the guard is released only after the window has passed:

control: no activity during the release decision
  release_now: assertion=1 time_since_last_activity=237.267126ms idle_window_elapsed=true activity_refreshed_after_decision=false
  created=1 released=1

When activity is recorded between the two lock acquisitions, the guard is released although the refreshed window had not passed:

race schedule: a tool call records activity between the two locks
  release thread reached remaining()=None and is paused before release_now
  note_activity: last_activity = now, guard was Some, returns
  release_now: assertion=2 time_since_last_activity=16.340542ms idle_window_elapsed=false activity_refreshed_after_decision=true
  RACE: the guard was released although last_activity was refreshed after the expired decision and the idle window had NOT passed (remaining=Some(183.659458ms))
  created=2 released=2
  guard held after release: false

The assertion identifier is a counter in this trace; the lock/state code is the production code.

Root cause

The decision and the release are two separate GUARD critical sections:

let wait = {
    let guard = GUARD.lock()...;
    let Some(held) = guard.as_ref() else { return; };
    remaining(held.last_activity.elapsed(), idle)
};
match wait {
    Some(wait) => std::thread::sleep(wait),
    None => {
        release_now();
        return;
    }
}
fn release_now() {
    let held = GUARD.lock()...take();
    if let Some(held) = held {
        unsafe { IOPMAssertionRelease(held.assertion) };
    }
}

release_now() takes whatever is in GUARD at the second lock; it does not check whether last_activity moved after the first lock. note_activity() cannot prevent this: when it finds the guard Some it only updates last_activity and returns, precisely because it assumes the guard is still held and the release thread is still watching. The state machine is therefore check-then-act across two lock acquisitions.

Suggested fix

Revalidate the idle window in the same critical section that takes the guard. Replace the loop's release_now() with a conditional release that locks GUARD, checks remaining(held.last_activity.elapsed(), idle), and returns without taking the guard when it is Some; the loop can then sleep again. Keep the unconditional release_now() only for the thread-spawn failure path. A test that refreshes last_activity between the decision and the release, then asserts the assertion is still held, would cover the schedule.

Activity

  1. f-trycua commented on Nov 23, 2026

    @f-trycua
    Collaborator

    Thanks for the report and the schedule trace. I read current main: in libs/cua-driver/rust/crates/platform-macos/src/display_awake.rs, release_when_idle reads remaining() under the lock and drops it, then release_now() takes the guard without checking last_activity again. So a note_activity call that lands between the two lock sections is not seen by the release, as you describe.

    I haven't reproduced the interleaving. The team is looking into it and I've assigned it to the Cua Driver owner. Re-checking the idle window inside release_now (or looping back when it isn't expired) looks like the natural fix.

  2. added
    bug
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions