Skip to content

cp: restore the terminal when a signal hits the PIN prompt - #50

Merged
richlegrand merged 1 commit into
mainfrom
pin-prompt-sigint
Nov 8, 2026
Merged

richlegrand merged 1 commit into
mainfrom
pin-prompt-sigint

Conversation

@richlegrand

Copy link
Copy Markdown
Owner

Fixes #48.

Ctrl-C at the PIN: prompt left the terminal with echo off, so the user
got back a shell that showed nothing they typed.

term.ReadPassword clears ECHO and puts the old termios back from a
deferred call:

newState.Lflag &^= unix.ECHO
newState.Lflag |= unix.ICANON | unix.ISIG
...
defer unix.IoctlSetTermios(fd, ioctlWriteTermios, termios)

A process killed by a signal never runs its defers. ISIG stays on
during the prompt, so Ctrl-C is a real SIGINT rather than an input byte,
and at that point in connect nothing has registered for it yet --
dialConnect is called at connect.go:265, the first signal.Notify is
at :303 and term.MakeRaw at :380. So SIGINT took its default
disposition and killed the process inside read(2), with ECHO left
cleared.

guardTerminal saves the state before the prompt and restores it from a
signal handler. It is the same sync.Once-plus-goroutine arrangement
setupInteractive already uses, and for the same reason: the signal path
has to call os.Exit, which skips defers.

connect and cp share makePINPrompt, so both were affected and both
are covered here.

Test

A pty test, since the fix exits via os.Exit and cannot be exercised
in-process. It re-execs the test binary as a child on a real pty, waits
for the prompt to take hold, signals it, and asserts the terminal state
is bit-for-bit what it was. With the guard removed it fails, and the
delta is Lflag 35387 -> 35379 -- the ECHO bit and nothing else.

creack/pty was already a direct dependency, so this adds none.

Not addressed

@lars18th also reported that reset does not recover the terminal, and
that part is still unexplained. Echo-off accounts for the typing and the
arrow keys, but busybox's reset applet calls TCSETSW itself with echo
on, so where it is compiled in it should have worked. Either the OpenWRT
build lacks the applet -- and reset: not found would have been
invisible with echo off -- or something beyond ECHO is also being lost.
stty -a from a wedged shell would settle it. Worth confirming before
the issue is closed, because if there is a second defect this does not
touch it.

Recovery in the meantime is to type stty sane blind and press Enter.

term.ReadPassword clears ECHO and puts the old termios back from a
deferred call, which a process killed by a signal never runs. At the PIN
prompt nothing has registered for SIGINT yet -- connect does that later,
around raw mode -- so Ctrl-C took the default disposition and died inside
read(2), leaving a shell that no longer echoed anything typed into it.
Reported on OpenWRT as #48.

guardTerminal saves the state before the prompt and restores it from a
signal handler: the same sync.Once-plus-goroutine arrangement
setupInteractive uses, and for the same reason, which is that the signal
path has to call os.Exit and os.Exit skips defers.

connect and cp share makePINPrompt, so both were affected and both are
covered. The test drives a real process on a real pty, because the fix
exits via os.Exit. It fails without the guard, with ECHO as the only
changed bit.
@lars18th

lars18th commented Oct 22, 2026 •

Copy link
Copy Markdown

Hi @richlegrand ,

Inside OpenWRT, the stty command is not available. But the applet reset exists.

The problem when closing (without this patch, that's using the stock v0.5.2) is that echo, newline and keys doesn't work; but all the window is cleared. Furthermore, if you launch tmux then inside it the console works. But if you repeat the experiment inside tmux the same problem appears.

After installing the coreutils-stty package in OpenWRT... typing stty -a after the error:

speed 38400 baud; rows 39; columns 153; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc

And if I execute stty sane then it restores the shell. 😃

So, from my point of view the problem will be fixed with this patch. 👍

@richlegrand
richlegrand merged commit 69b942e into main Nov 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: when <CTRL>+<C> when PIN: leaves the session misconfigured

2 participants