backup: prevent hang using --stdin-from-command if upload fails (#21829)

This commit is contained in:
Michael Eischer
2026-05-31 15:27:05 +02:00
committed by GitHub
parent 451cc6c048
commit ce24640d75
3 changed files with 24 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
Bugfix: Prevent `backup --stdin-from-command` from hanging
When using `--stdin-from-command`, the `backup` command could hang until
manually cancelled if the backup stopped before all subprocess output was
consumed, for example after a failed upload to the repository. This has been
fixed.
https://github.com/restic/restic/issues/5683
https://github.com/restic/restic/pull/21829
+3
View File
@@ -97,5 +97,8 @@ func (fp *CommandReader) Close() error {
return nil
}
// This line can only be reached if the command was not read until the end. This can, for example,
// happen if a backup run is interrupted. Kill the command to avoid hanging.
_ = fp.cmd.Cancel()
return fp.wait()
}
+12
View File
@@ -6,6 +6,7 @@ import (
"io"
"strings"
"testing"
"time"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/test"
@@ -51,3 +52,14 @@ func TestCommandReaderOutput(t *testing.T) {
test.Equals(t, "hello world", strings.TrimSpace(buf.String()))
}
func TestCommandReaderQuickClose(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
reader, err := fs.NewCommandReader(ctx, []string{"sleep", "3600"}, func(msg string, args ...interface{}) {})
test.OK(t, err)
// test that close returns before the context expires
_ = reader.Close()
test.OK(t, ctx.Err())
}