#35 1.10 regression: sshpass hangs forever if the child exits as a SIGTERM arrives
Note: Found with the help of Claude Opus 5.5, and the below text is also generated by them. Read through and approved by me.
sshpass 1.10 can stay in pselect() forever, with its child a zombie, when SIGCHLD and a SIGTERM (or SIGHUP, SIGINT, SIGTSTP) are both pending as pselect() unblocks them. Further SIGTERMs do not end it; SIGKILL does. 1.09 does not hang in the reproducer below.
I ran into it stopping a systemd service, which sends SIGTERM to sshpass and ssh together: now and then
sshpass outlived its ssh and held the stop up until TimeoutStopSec (90 s by default). I have not traced
one of those, but they look the same. That was on Ubuntu 26.04 (1.10-0.1build1); the reproducer hangs
there, on Arch Linux (1.10-2), and with 1.10 built from the release tarball.
Cause, in runprogram(): both handlers run in the one pselect() call. As termsig is set, the loop
forwards the signal and does continue, which skips the waitpid(). The SIGCHLD has been used up, so the
next pselect() waits for something that has already happened. The pty does not wake it either, since
the parent keeps the slave open. Each later SIGTERM takes the same continue.
This needs wait_id to be 0, so an earlier pass through the loop, as the password prompt makes. Without
one, continue evaluates the loop condition with wait_id uninitialized.
This is not patch #11 (SIGCHLD lost before the mask is set), whose fix is in 1.10. 1.10 moved the signal
handling into the loop, seemingly for bug #22 (signal safety). main has the same loop as 1.10.
Reproducer (the SIGSTOP makes both signals pending together; it should also happen without it, only
rarely):
import os, signal, subprocess, tempfile, time
with tempfile.NamedTemporaryFile("w") as f:
f.write("x\n")
f.flush()
# A prompt, so that the loop has gone round once.
child = ["sh", "-c", "echo assword: >/dev/tty; exec sleep 300"]
p = subprocess.Popen(["sshpass", "-f", f.name, *child])
time.sleep(0.5)
kid = int(subprocess.check_output(["pgrep", "-P", str(p.pid)]))
p.send_signal(signal.SIGSTOP)
time.sleep(0.1)
os.kill(kid, signal.SIGTERM)
time.sleep(0.1)
p.send_signal(signal.SIGTERM)
p.send_signal(signal.SIGCONT)
try:
p.wait(3)
print("exited")
except subprocess.TimeoutExpired:
print("hung")
p.kill()
Suggested fix: fall through to the waitpid() after forwarding. With essentially this reproducer, 1.10 hung 5 times out of 5, and 0 out of 5 with the fix. A single SIGTERM, SIGHUP or SIGINT, the child's exit status and a wrong password come out as before, tested with stand-in commands rather than a real ssh.
--- a/main.c
+++ b/main.c
@@ -390,11 +390,7 @@
termsig = 0;
term_child(signum);
-
- continue;
- }
-
- if( selret>0 ) {
+ } else if( selret>0 ) {
if( FD_ISSET( masterpt, &readfd ) ) {
int ret;
if( (ret=handleoutput( masterpt )) ) {