From 7436d05e36d0ae474f5c402cb6bad22368846bdd Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 16 Oct 2026 17:07:57 +0100 Subject: [PATCH] list: fix ext sort error sometimes being ignored at the end of a listing Sorter.Send selects on both the output and error channels of the ext sort. If the ext sort failed it closes both, so the select could pick the closed output channel and return without the error. This was seen as a flaky TestSorterExtTempFileError under the race detector. --- fs/list/sorter.go | 5 +++++ fs/list/sorter_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) 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{}