agent: accept CR at the yes/no prompt - #649
Open
michaelpersonal wants to merge 1 commit into
Open
Conversation
The "Save current session? (y/n)" prompt can hang: typing y and pressing Enter echoes ^M and the prompt never returns, so each further Enter appends another ^M. agent_prompt_yes_no_ex() reads with fgets(), which waits for '\n'. The terminal may still be in linenoise raw mode at that point -- editor_stop() leaves cooked mode via linenoiseEditStop(), but linenoiseRestoreRawMode() (ds4_agent.c, raw_mode_needs_restore) can put it back -- and raw mode clears ICRNL in linenoiseMakeRawMode(), so Enter arrives as '\r' and never as '\n'. fgets then blocks forever. Read the line by hand and terminate on '\r' or '\n'. This is correct in both modes: when ICRNL is enabled the tty has already translated CR, so a lone '\r' cannot reach the application. Raw mode also clears ECHO, so in that case neither the answer nor a newline is echoed and the following output runs into the prompt line; emit a newline ourselves, but only when ECHO is off, so cooked mode does not get a blank line. tcgetattr() fails on a non-tty, which correctly suppresses it for pipes. Verified on macOS by driving ds4-agent through a pty and answering the save prompt with CR only (never LF): before, the prompt hung and echoed ^M; after, it is accepted and the process exits cleanly.
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.
Problem
The
Save current session? (y/n)prompt can hang. Typingyand pressing Enterechoes
^M, the prompt never returns, and each further Enter appends another:Cause
agent_prompt_yes_no_ex()reads withfgets(), which waits for'\n'.The terminal may still be in linenoise raw mode at that point.
editor_stop()does leave cooked mode via
linenoiseEditStop()→disableRawMode(), butlinenoiseRestoreRawMode()(driven byraw_mode_needs_restore) can put it back.Raw mode clears
ICRNLinlinenoiseMakeRawMode(), so Enter arrives as'\r'and never as
'\n'— andfgets()blocks forever.This is why it is intermittent rather than always reproducible.
Fix
Read the line by hand and terminate on
'\r'or'\n'. Correct in bothmodes: when
ICRNLis enabled the tty has already translated CR, so a lone'\r'cannot reach the application.Raw mode also clears
ECHO, so in that case neither the answer nor a newline isechoed and following output runs into the prompt line. The patch emits a newline
itself, but only when
ECHOis off, so cooked mode does not gain a blank line.tcgetattr()fails on a non-tty, which correctly suppresses it for pipes too.Testing
Driven through a pty on macOS (M5 Max, Metal), answering the save prompt with
CR only, never LF:
^Maccumulates, process must be killed0occurrences of^M, clean exitBuilds warning-free under the existing
-Wall -Wextra -std=c99. Adds#include <termios.h>.