mirror of
https://github.com/restic/restic.git
synced 2026-06-02 15:19:43 +00:00
backup: prevent hang using --stdin-from-command if upload fails (#21829)
This commit is contained in:
@@ -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
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user