codex - ✅(Solved) Fix iTerm2: Ctrl+C exit can intermittently leave terminal stuck in CSI-u mode and print `9;5:3u` [1 pull requests, 2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#19553Fetched 2026-04-26 05:15:13
View on GitHub
Comments
2
Participants
3
Timeline
8
Reactions
0
Timeline (top)
labeled ×3commented ×2cross-referenced ×1renamed ×1

Fix Action

Fixed

PR fix notes

PR #19625: Reset TUI keyboard reporting on exit

Description (problem / solution / changelog)

Why

Codex enables enhanced keyboard reporting while the TUI owns the terminal. In iTerm2, exiting the TUI with Ctrl+C can intermittently leave the parent shell receiving raw CSI-u / modifyOtherKeys fragments instead of normal key input.

Final terminal cleanup should put the parent shell back into normal keyboard reporting even if the terminal misses the usual stack pop.

Fixes #19553.

What Changed

  • Move TUI keyboard enhancement setup and detection into tui/src/tui/keyboard_modes.rs.
  • Add an exit-only restore_after_exit() path that performs the normal keyboard enhancement pop plus unconditional keyboard enhancement and modifyOtherKeys resets.
  • Keep temporary restore paths, such as external-editor handoff, using the balanced stack pop behavior.

Confidence

Medium. This is a speculative fix: I was not able to reproduce the reported iTerm2 behavior manually, but the symptoms line up with terminal keyboard reporting state surviving Codex exit. The added reset sequences are scoped to final TUI shutdown and should be harmless when the terminal is already clean.

Changed files

  • codex-rs/tui/src/lib.rs (modified, +2/-2)
  • codex-rs/tui/src/tui.rs (modified, +46/-193)
  • codex-rs/tui/src/tui/keyboard_modes.rs (added, +280/-0)

Code Example

SHELL=/bin/zsh
TERM=xterm-256color
TERM_PROGRAM=iTerm.app
TERM_PROGRAM_VERSION=3.6.10

---

9;5:3u

---

9;5:3u

---

printf '\033[<u\033[>4;0m' && stty sane && reset
RAW_BUFFERClick to expand / collapse

What version of Codex CLI is running?

codex-cli 0.125.0

What subscription do you have?

No response

Which model were you using?

No response

What platform is your computer?

macOS 26.4.1 / Darwin 25.4.0 arm64

What terminal emulator and version are you using (if applicable)?

iTerm2 3.6.10

Relevant environment:

SHELL=/bin/zsh
TERM=xterm-256color
TERM_PROGRAM=iTerm.app
TERM_PROGRAM_VERSION=3.6.10

What issue are you seeing?

When running Codex CLI in iTerm2 on macOS, pressing Ctrl+C to exit Codex can intermittently leave the parent terminal input state corrupted. I have not found a stable reproduction pattern yet, and it does not happen on every Ctrl+C exit. However, the failure has occurred after exiting Codex with Ctrl+C.

After Codex exits, the shell can receive or display a raw CSI-u style keyboard sequence such as:

9;5:3u

This looks like Codex enables enhanced keyboard reporting / CSI-u / modifyOtherKeys style input, but does not always fully restore the terminal state on the Ctrl+C exit path. After Codex exits, the parent shell should be back in normal text input mode instead of still emitting raw enhanced-keyboard fragments.

This appears related in shape to #16517, but the environment and trigger are different:

  • terminal emulator: iTerm2, not Ghostty
  • trigger observed so far: local Ctrl+C exit from Codex, not an abnormal SSH disconnect
  • reproducibility: intermittent; I do not yet know the exact timing or state needed to trigger it
  • observed raw fragment: 9;5:3u

What steps can reproduce the bug?

I do not currently have a fully deterministic reproduction sequence. The observed trigger path is:

  1. Open iTerm2 on macOS.
  2. Start codex.
  3. While the Codex TUI is active, press Ctrl+C to exit.
  4. Return to the same iTerm2 tab/window's shell.
  5. Continue typing or pressing keys.

Observed intermittent result:

the shell can receive or display raw CSI-u style text such as:

9;5:3u

What is the expected behavior?

Codex should restore the terminal keyboard/input state on Ctrl+C / SIGINT exit paths, including disabling or popping any enhanced keyboard / CSI-u / modifyOtherKeys modes it enabled.

After Codex exits, the parent shell should immediately behave normally, without requiring the user to run reset, stty sane, or manual terminal control sequences.

Additional information

A possible manual recovery command is:

printf '\033[<u\033[>4;0m' && stty sane && reset

The fact that this kind of reset helps suggests the issue may be related to keyboard enhancement flags / CSI-u / modifyOtherKeys state not being reliably restored.

Suggested area to inspect: the Codex TUI cleanup path for SIGINT, early return, panic, task cancellation, and any other abnormal or semi-abnormal exits. In particular, please check whether keyboard enhancement flags are always restored symmetrically with the setup path.

extent analysis

TL;DR

The issue can be mitigated by running a manual recovery command after exiting Codex with Ctrl+C, which suggests a problem with terminal state restoration.

Guidance

  • Investigate the Codex TUI cleanup path for SIGINT to ensure keyboard enhancement flags are restored symmetrically.
  • Verify if the issue is related to the modifyOtherKeys mode not being properly disabled on exit.
  • Check the terminal emulator settings in iTerm2 to see if there are any configuration options that could affect the behavior.
  • Consider adding a workaround to run the manual recovery command automatically after Codex exits with Ctrl+C.

Example

printf '\033[<u\033[>4;0m' && stty sane && reset

This command can be used as a manual recovery to restore the terminal state after Codex exits.

Notes

The issue is intermittent and does not have a fully deterministic reproduction sequence, which makes it harder to debug. The fact that the manual recovery command helps suggests that the issue is related to keyboard enhancement flags not being restored.

Recommendation

Apply workaround: run the manual recovery command automatically after Codex exits with Ctrl+C, as this can mitigate the issue until the root cause is fixed.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

codex - ✅(Solved) Fix iTerm2: Ctrl+C exit can intermittently leave terminal stuck in CSI-u mode and print `9;5:3u` [1 pull requests, 2 comments, 3 participants]