sftp: ssh's error line is sometimes not printed when the session fails to start on a loaded host
Disclaimer: AI-drafted, verified by me before filing. I answer follow-ups.
When the sftp backend cannot start its ssh session, restic sometimes exits without printing ssh's own error line (subprocess ssh: ssh: connect to host ... Connection refused). restic's own message is the same whatever ssh said, so the lost line is the only one that says why the connection failed. Our backup scripts tell an unreachable target (a quiet skip) from a real failure (an alert) by that line, so a busy host reported an unreachable target as a failed backup. On a host under CPU load it went missing in about a quarter of runs; without extra load it never did.
Output of restic version
restic 0.19.1 compiled with go1.26.4 on linux/amd64This is the official release binary. The nixpkgs build (restic 0.19.1 compiled with go1.26.7 on linux/amd64) shows the same behavior. startClient in internal/backend/sftp/sftp.go is the same on master.
OpenSSH_10.5p1, Linux 6.18 x86_64 (NixOS), 12 hardware threads.
What backend/service did you use to store the repository?
sftp. The reproduction needs no server; it points restic at a closed port on 127.0.0.1.
Problem description / Steps to reproduce
repro.sh runs restic cat config against a closed local port 100 times and counts the runs whose output contains ssh's line. run.sh runs it once without extra load, then twice while stress-ng --cpu 0 keeps every core busy.
| Condition | Runs | ssh's line printed |
|---|---|---|
| no extra load | 100 | 100 |
stress-ng --cpu 0 |
200 | 152 |
repro.sh:
#!/bin/sh
# Count how many restic runs against a closed port print ssh's own error line.
# usage: ./repro.sh [restic binary] [runs]
restic=${1:-restic}
runs=${2:-40}
export RESTIC_PASSWORD=x
seen=0
for i in $(seq "$runs"); do
"$restic" -r sftp://[email protected]:2222//tmp/x cat config >out.txt 2>&1
grep -q '^subprocess ssh:' out.txt && seen=$((seen + 1))
done
echo "runs=$runs printed_ssh_line=$seen load=$(cut -d' ' -f1 /proc/loadavg)"run.sh:
#!/bin/sh
restic version
ssh -V
./repro.sh restic 100
stress-ng --cpu 0 --quiet &
sleep 5
./repro.sh restic 100
./repro.sh restic 100
kill $!$ ./run.sh
restic 0.19.1 compiled with go1.26.4 on linux/amd64
OpenSSH_10.5p1, OpenSSL 3.6.4 25 Aug 2026
runs=100 printed_ssh_line=100 load=8.10
runs=100 printed_ssh_line=75 load=9.08
runs=100 printed_ssh_line=77 load=9.47Other processes kept the load average near 8 during the first line, with the CPU about 80% idle.
Expected behavior
Every run prints ssh's line before restic's error:
subprocess ssh: ssh: connect to host 127.0.0.1 port 2222: Connection refused
Fatal: unable to open repository at sftp://[email protected]:2222//tmp/x: unable to start the sftp session, error: error receiving version packet from server: server unexpectedly closed connection: unexpected EOFActual behavior
Under load, some runs exit with status 1 and print only restic's line:
Fatal: unable to open repository at sftp://[email protected]:2222//tmp/x: unable to start the sftp session, error: error receiving version packet from server: server unexpectedly closed connection: unexpected EOFDo you have any idea what may have caused this?
startClient copies ssh's stderr to restic's stderr from a goroutine that nothing waits for. When sftp.NewClientPipe fails, startClient returns the "unable to start the sftp session" error and restic exits, whether or not that goroutine has printed ssh's line yet.
A diagnostic build agrees. With the goroutine signalling when it reaches EOF, and startClient waiting for that signal before returning the error, 300 of 300 runs under the same load printed ssh's line. Waiting for the signal only before cmd.Wait() did not stop the loss.
stress-ng --cpu 0All three builds are v0.19.1 source, built with go1.26.7 and CGO_ENABLED=0, run alternately with repro.sh (2 × 150 runs each), alongside the release binary.
| Build | ssh's line printed |
|---|---|
| release binary | 219 / 300 |
| unmodified source | 218 / 300 |
A: cmd.Wait() waits for the stderr goroutine to finish |
213 / 300 |
B: A, plus startClient waits for it before returning the error |
300 / 300 |
Build A:
--- a/internal/backend/sftp/sftp.go
+++ b/internal/backend/sftp/sftp.go
@@ -71,7 +71,9 @@
return nil, errors.Wrap(err, "cmd.StderrPipe")
}
+ stderrDone := make(chan struct{})
go func() {
+ defer close(stderrDone)
sc := bufio.NewScanner(stderr)
for sc.Scan() {
errorLog("subprocess %v: %v\n", program, sc.Text())
@@ -99,6 +101,7 @@
// wait in a different goroutine
ch := make(chan error, 1)
go func() {
+ <-stderrDone
err := cmd.Wait()
debug.Log("ssh command exited, err %v", err)
for {Build B adds, on top of A:
--- a/internal/backend/sftp/sftp.go
+++ b/internal/backend/sftp/sftp.go
@@ -115,6 +115,7 @@
// increase send buffer per file to 4MB
sftp.MaxConcurrentRequestsPerFile(128))
if err != nil {
+ <-stderrDone
return nil, errors.Errorf("unable to start the sftp session, error: %v", err)
}
These builds only locate the loss. Build B would block if NewClientPipe failed while ssh kept running.
Source: restic/restic