Commit 94e69e7
fix: replace time.Sleep with context-aware sleeps in doPressKey and doDragMouse (#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>1 parent a90f3b3 commit 94e69e7
1 file changed
+52
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
557 | 557 | | |
558 | 558 | | |
559 | 559 | | |
560 | | - | |
561 | 560 | | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
568 | 565 | | |
569 | 566 | | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
570 | 578 | | |
571 | 579 | | |
572 | | - | |
573 | | - | |
574 | | - | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
575 | 584 | | |
576 | 585 | | |
577 | | - | |
| 586 | + | |
578 | 587 | | |
579 | 588 | | |
580 | 589 | | |
| |||
770 | 779 | | |
771 | 780 | | |
772 | 781 | | |
773 | | - | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
774 | 793 | | |
775 | 794 | | |
776 | 795 | | |
| |||
869 | 888 | | |
870 | 889 | | |
871 | 890 | | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
872 | 909 | | |
873 | 910 | | |
874 | 911 | | |
| |||
877 | 914 | | |
878 | 915 | | |
879 | 916 | | |
880 | | - | |
881 | | - | |
882 | | - | |
883 | | - | |
884 | | - | |
885 | | - | |
886 | | - | |
887 | | - | |
| 917 | + | |
| 918 | + | |
888 | 919 | | |
| 920 | + | |
889 | 921 | | |
890 | 922 | | |
891 | 923 | | |
| |||
0 commit comments