Skip to content

fix: flush stdin on POSIX exit to prevent stale bytes leaking to shell#17083

Open
altendky wants to merge 49 commits intoanomalyco:devfrom
altendky:fix/flush-stdin-on-exit
Open

fix: flush stdin on POSIX exit to prevent stale bytes leaking to shell#17083
altendky wants to merge 49 commits intoanomalyco:devfrom
altendky:fix/flush-stdin-on-exit

Conversation

@altendky
Copy link
Copy Markdown
Contributor

@altendky altendky commented Mar 11, 2026

Issue for this PR

Closes #17081

Type of change

  • 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.tsconsole.ts and consolidates platform-specific FFI (Windows kernel32 + POSIX libc) under a unified load() function
  • Drops the win32 prefix from flushInputBuffer since 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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. contributor and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@altendky altendky marked this pull request as draft March 21, 2026 02:16
@altendky altendky marked this pull request as ready for review April 7, 2026 14:35
# Conflicts:
#	packages/app/src/context/layout.tsx
#	packages/app/src/pages/layout/sidebar-items.tsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stale stdin bytes leak to shell after exit on POSIX (space on prompt, blocks ctrl+d)

5 participants