diff --git a/fs/list/sorter.go b/fs/list/sorter.go index b9d759abe..e44bc7932 100644 --- a/fs/list/sorter.go +++ b/fs/list/sorter.go @@ -305,6 +305,11 @@ func (ls *Sorter) Send() (err error) { } } } + // The select may see outputChan closed before reading the + // error the sorter sent on errChan + if err := <-ls.errChan; err != nil { + return err + } err = list.Flush() if err != nil { return err diff --git a/fs/list/sorter_test.go b/fs/list/sorter_test.go index 2ba8d80bd..bdbe16c3b 100644 --- a/fs/list/sorter_test.go +++ b/fs/list/sorter_test.go @@ -3,6 +3,7 @@ package list import ( "cmp" "context" + "errors" "fmt" "os" "path/filepath" @@ -285,6 +286,31 @@ func TestSorterExtTempFileError(t *testing.T) { require.Error(t, err) } +// Test that Send returns the error from the ext sort when the ext +// sort closes its output and error channels before Send reads them. +func TestSorterExtSendError(t *testing.T) { + ctx := context.Background() + callback := func(entries fs.DirEntries) error { + return nil + } + // select picks randomly between ready channels so try many times + for range 100 { + ls, err := NewSorter(ctx, nil, callback, nil) + require.NoError(t, err) + outputChan := make(chan string) + close(outputChan) + errChan := make(chan error, 1) + errChan <- errors.New("sort failed") + close(errChan) + ls.extSort = true + ls.inputChan = make(chan string) + ls.outputChan = outputChan + ls.errChan = errChan + require.EqualError(t, ls.Send(), "sort failed") + ls.CleanUp() + } +} + // benchFs implements enough of the fs.Fs interface for Sorter type benchFs struct{}