Skip to content

fix: replace time.Sleep with context-aware sleeps in doPressKey and doDragMouse#146

Merged
rgarcia merged 1 commit intomainfrom
raf/fix-context-aware-sleeps
Feb 5, 2026
Merged

fix: replace time.Sleep with context-aware sleeps in doPressKey and doDragMouse#146
rgarcia merged 1 commit intomainfrom
raf/fix-context-aware-sleeps

Conversation

@rgarcia
Copy link
Contributor

@rgarcia rgarcia commented Feb 5, 2026

Summary

  • doPressKey used time.Sleep for the key hold duration, blocking without respecting context cancellation while holding inputMu
  • doDragMouse used time.Sleep for the optional pre-drag delay, same problem
  • Both are especially problematic in the batch flow where the mutex is held for the entire batch

Changes

  • Extract a sleepWithContext helper that uses select with time.NewTimer and ctx.Done()
  • Refactor doSleep to use the shared helper
  • Replace time.Sleep in doPressKey with sleepWithContext; on cancellation, best-effort release held keys using a background context
  • Replace time.Sleep in doDragMouse with sleepWithContext; on cancellation, best-effort release mouse button and modifier keys
  • No remaining time.Sleep calls in computer.go

Test plan

  • go build ./... and go vet ./... pass
  • go test ./cmd/api/api/... passes
  • Verify key hold and drag delay are interruptible by cancelling a request mid-sleep

Made with Cursor


Note

Medium Risk
Touches low-level input simulation and cleanup behavior; cancellation paths now run xdotool releases via context.Background(), which could change how interrupted requests leave system input state.

Overview
Makes input-related delays context-aware to avoid blocking while inputMu is held (notably during BatchComputerAction).

Replaces time.Sleep in doPressKey key-hold and doDragMouse pre-drag delay with a shared sleepWithContext helper, and refactors doSleep to use it as well. On cancellation, both key-hold and drag delay now perform best-effort cleanup (releasing keys and/or mouse button) via context.Background() before returning an error.

Written by Cursor Bugbot for commit 90ff857. This will update automatically on new commits. Configure here.

…oDragMouse

time.Sleep in doPressKey (key hold duration) and doDragMouse (pre-drag
delay) blocks without respecting context cancellation. While holding
the inputMu mutex, this prevents timely cleanup on request
cancellation.

Extract a sleepWithContext helper and use it in all three sleep sites
(doPressKey, doDragMouse, doSleep). On cancellation, best-effort
cleanup releases held keys/mouse buttons before returning.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copy link

@az-rye az-rye left a comment

Choose a reason for hiding this comment

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

🔥

@rgarcia rgarcia merged commit 94e69e7 into main Feb 5, 2026
4 of 5 checks passed
@rgarcia rgarcia deleted the raf/fix-context-aware-sleeps branch February 5, 2026 21:20
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.

2 participants