Repository navigation
fix: stdin handling on suspend/resume - #331
Merged
Merged
Conversation
veracioux
marked this pull request as draft
November 21, 2025 15:57
veracioux
marked this pull request as ready for review
November 21, 2025 16:40
kommander
approved these changes
Nov 22, 2025
msmps
pushed a commit
that referenced
this pull request
Jan 8, 2026
simonklee
pushed a commit
that referenced
this pull request
Mar 23, 2026
…ation (#858) ## Problem `resume()` calls `stdin.resume()` then defers listener registration into a `setImmediate`: ```ts this.stdin.resume() this.addExitListeners() setImmediate(() => { while (this.stdin.read() !== null) {} this.stdin.on("data", this.stdinListener) }) ``` The stream starts flowing immediately, but `stdinListener` isn't attached until the next macrotask. Any keystrokes arriving in that gap are silently lost — which is exactly the window where a user types after `fg`. `setupInput()` has a milder variant of the same bug: `resume()` before `on("data")`. The drain loop was introduced in #331 to discard stale input from child process suspension. The placement made it ineffective: `read()` returns null in flowing mode because data goes through events, not pull. Stale bytes were only "discarded" by accident — emitted as `data` events with no listener present. **Repro:** in the test suite, `mockInput.pressKey()` right after `renderer.resume()` is silently dropped. The existing tests masked this by yielding a macrotask (`await new Promise(r => setImmediate(r))`) before asserting. ## Fix Reorder `resume()` to: drain → register listener → resume stream: ```ts while (this.stdin.read() !== null) {} this.stdin.on("data", this.stdinListener) this.stdin.resume() ``` The ordering is load-bearing: 1. **Drain first** — `read()` pulls from the internal buffer in paused mode, discarding stale input (#331's scenario). 2. **Register listener second** — must come after the drain. Adding a `data` listener can auto-resume a paused Readable, flushing stale bytes through the listener before the drain runs. 3. **Resume last** — starts flowing mode. New data hits the registered listener with zero gap. `setupInput()` just swaps the two lines (no drain needed at init). ## Tests **Modified:** removed three `setImmediate` yield workarounds (2 in `renderer.control`, 1 in `renderer.input`). The tests now assert synchronous delivery — they'd fail if the fix were wrong. **New (4):** - "keystrokes received immediately after resume() without yielding" — core invariant. - "keystrokes survive multiple rapid suspend/resume cycles" — 5 cycles, no accumulation. - "input buffered during suspension is drained on resume" — `push()` bytes into stdin during suspension, verifies they're discarded. Directly tests the #331 scenario. - "suspend/resume does not leak stdin listeners" — 10 cycles, `listenerCount("data")` stays constant. ## Background I hit this in 0.1.88 while building a TUI on OpenTUI and patched the bundled JS. When 0.1.90 shipped, `setupInput()` had its `setImmediate` removed but the ordering was still wrong, and `resume()` still defers via `setImmediate`. I've been running a patched build since. The `setImmediate` + drain pattern was introduced in #770 (building on #331). The intent was sound — this fix keeps the drain but moves it to where `read()` actually works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes anomalyco/opencode#4586.
Fixes anomalyco/opencode#4209.
Cause
When the editor is running in opencode, both it and opencode are reading from stdin, because stdin.pause() was removed from renderer.suspend. This means that the editor and opencode compete to receive keystrokes.
Fix
Do stdin.pause() like before, but to prevent buffered input on resume, consume everything synchronously before reattaching renderer's stdin listener. I believe this is a universal and foolproof fix.
Testing
I tested this change in opencode, using this development version of opentui.
Scenario 1
Open the editor using
/editoror keybind and it should work as expected, receiving all inputScenario 2
Replace the logic in opencode that opens the editor with an `await new Promise(r => setTimeout(r, 2000)). While the renderer is suspended, type characters into the terminal - they should be printed. When the renderer resumes, those characters should not be added to the prompt.