Skip to content

fix(desktop): retry text preparation when the grab bound expires - #248

Merged
ElbertePlinio merged 1 commit into
mainfrom
fix/222-desktop-type-grab-retry
Oct 5, 2026
Merged

ElbertePlinio merged 1 commit into
mainfrom
fix/222-desktop-type-grab-retry

Conversation

@ElbertePlinio

Copy link
Copy Markdown
Member

Under CPU contention, the grabbed phase of text preparation can outlive the 250 ms grab bound, so desktop_type fails closed with "Desktop text preparation failed; no text was sent". This change keeps the bound and retries the grabbed phase.

prepareText now runs the grabbed phase up to 3 times, each time on a fresh connection. The steps are hello, XKB setup, grab, keymap read, planning and verification. Each failed wire closes first, which drops the grab, so two connections or grabs never overlap. A 100 ms gap separates attempts, and the existing 3 s preparation budget and the caller's deadline bound every attempt. Only a grab-bound expiry retries. X11Wire.grabExpired is true only when the wire's first failure was its own grab deadline after boundGrab. Capacity exhaustion, malformed replies, socket loss, a dead target and verification failures still fail at once. A retry reads the live keymap again, so bindings left by a failed attempt count as available. X11_GRAB_MS, X11_SETUP_MS and the error text are unchanged.

This is a partial fix. The retry helps when contention sits near the bound but not under heavy sustained load. The main cost while the server is grabbed is the target liveness check: check() reads /proc/<pid>/stat about 1,500 times during planning. Throttling that check would change a safety check, so it is left for a separate decision on #222.

QA pass: the real desktop_type path was driven through desktop-input-captures-live.test.ts under load (table below). No visual surface changed.

Requirement Verified by Scenario Result Evidence
Decide the behavior under contention Owner decision Keep the 250 ms bound and retry within the 3 s budget Implemented This PR
Retry recovers from an expired grab Automated test First attempt expires during the keymap re-read, second succeeds 2 connections, one live wire at each setup, no new keycodes on retry, no grab or timers left x11-anchor.test.ts
Retries stop at the cap Automated test Every map read dropped Stops after 3 connections x11-anchor.test.ts
Retries respect the preparation budget Automated test Pre-grab work uses 2,700 ms 1 connection, no retry after the gap x11-anchor.test.ts
Deterministic refusals do not retry Automated test Capacity exhausted, malformed reply, dead target 1 connection each x11-anchor.test.ts
Tests catch a wrong retry rule Mutation check Retry every failure; remove the gap clamp 3 and 2 tests fail respectively Local run
Behavior under load Live test desktop-input-captures-live.test.ts, vitest and 10 busy loops on one CPU 5 of 6 passed with retry, 2 of 6 with a single attempt Local run
Heavy load Live test Same with 15 busy loops 0 of 8 with retry, 0 of 5 before; still fails closed Local run
Full suite and coverage Automated test bun run test:coverage 2,888 passed, 6 skipped, thresholds intact Local run, CI
Lint, types, function limits oxlint, tsc, PickCheck 0.4.0 --base origin/main All clean Local run

Refs #222

Under CPU contention the grabbed keymap phase can outlive the 250 ms
grab bound, so desktop_type failed closed. Keep the bound and retry the
grabbed phase up to three times on a fresh connection, with a short gap,
inside the existing preparation budget. Each failed wire closes first,
and each retry rereads the live keymap. Only a grab-bound expiry
retries; deterministic refusals, protocol faults and a dead target
still fail at once.

Refs #222
@ElbertePlinio
ElbertePlinio merged commit a829228 into main Oct 5, 2026
22 of 30 checks passed
@ElbertePlinio
ElbertePlinio deleted the fix/222-desktop-type-grab-retry branch October 5, 2026 22:06
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.

1 participant