Skip to content

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

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

rgarcia merged 1 commit into
mainfrom
raf/fix-context-aware-sleeps

Conversation

@rgarcia

@rgarcia rgarcia commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor

Date of sync: 2026-04-15

Summary

  • Merged new private commits from origin/main into sync/upstream-release before syncing upstream.
  • Merged upstream/main into sync/upstream-release.
  • kernel-browser was already up to date at v145.0.7632.75-r14; no Dockerfile changes were needed.
  • Current image versions on the sync branch remain headless v34 and headful v38.

Private sync

  • Merged commits from origin/main, including the headful WebAuthn Conditional UI disablement and related GPU launch flag alignment.

Upstream merge

kernel-browser

New image versions

  • Headless: v34
  • Headful: v38

Merge conflicts resolved

  • No conflicts. The private merge and upstream merge were both clean.

…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>

@az-rye az-rye 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.

🔥

@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
AbdulRashidReshamwala pushed a commit to reclaimprotocol/popcorn-images that referenced this pull request Mar 26, 2026
…oDragMouse (kernel#146)

## 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

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

Made with [Cursor](https://cursor.com)

<!-- CURSOR_SUMMARY -->
---

> [!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.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
90ff857. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: Cursor <cursoragent@cursor.com>
@kernel-internal kernel-internal Bot changed the title fix: replace time.Sleep with context-aware sleeps in doPressKey and doDragMouse Sync upstream - v34/v38 (20260415) Apr 15, 2026
@kernel-internal kernel-internal Bot changed the title Sync upstream - v34/v38 (20260415) fix: replace time.Sleep with context-aware sleeps in doPressKey and doDragMouse Apr 15, 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.

2 participants