#17083 · @altendky · opened Mar 11, 2026 at 7:16 PM UTC · last updated Mar 21, 2026 at 8:48 PM UTC
fix: flush stdin on POSIX exit to prevent stale bytes leaking to shell
Score breakdown
Impact
Clarity
Urgency
Ease Of Review
Guidelines
Readiness
Size
Trust
Traction
Summary
This PR addresses a bug where stale stdin bytes leak to the parent shell on POSIX systems after opencode exits, causing issues like extra spaces or blocking ctrl+d. It implements tcflush via FFI for POSIX and unifies platform-specific console functions.
Description
Issue for this PR
Closes #17081
Type of change
- [x] Bug fix
- [ ] New feature
- [ ] Refactor / code improvement
- [ ] Documentation
What does this PR do?
When opencode exits on POSIX systems (Linux/macOS), stale bytes left in the terminal's stdin buffer leak to the parent shell, typically manifesting as an extra space character on the prompt or blocking ctrl+d. This happens because the existing FlushConsoleInputBuffer call only works on Windows.
This PR:
- Adds a POSIX
tcflush(STDIN_FILENO, TCIFLUSH)call via Bun's libc FFI to discard pending tty input bytes on exit - Renames
win32.ts→console.tsand consolidates platform-specific FFI (Windows kernel32 + POSIX libc) under a unifiedload()function - Drops the
win32prefix fromflushInputBuffersince it is now cross-platform; Windows-only functions (win32DisableProcessedInput,win32InstallCtrlCGuard) retain their prefix
How did you verify your code works?
Tested on Linux by running opencode, pressing space several times before exiting, and confirming no stale bytes appear on the shell prompt after exit. Also verified Windows codepath is unchanged by reviewing the conditional logic.
Screenshots / recordings
N/A — terminal behavior fix, no UI change.
Checklist
- [x] I have tested my changes locally
- [x] I have not included unrelated changes in this PR
Linked Issues
#17081 Stale stdin bytes leak to shell after exit on POSIX (space on prompt, blocks ctrl+d)
View issueComments
No comments.
Changed Files
packages/opencode/src/cli/cmd/tui/app.tsx
+2−2packages/opencode/src/cli/cmd/tui/attach.ts
+1−1packages/opencode/src/cli/cmd/tui/console.ts
+31−7packages/opencode/src/cli/cmd/tui/context/exit.tsx
+2−2packages/opencode/src/cli/cmd/tui/thread.ts
+1−1