Skip to content

Commit

Permalink
Fix the data race under Linux
Browse files Browse the repository at this point in the history
Make sure the cancelreader Read & Close don't overlap.
  • Loading branch information
theclapp committed Mar 20, 2024
1 parent 24a510d commit 1235810
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions interp/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/muesli/cancelreader"
"mvdan.cc/sh/v3/expand"
Expand Down Expand Up @@ -935,16 +936,26 @@ func (r *Runner) readLine(ctx context.Context, raw bool) ([]byte, error) {
return nil, err
}
stdin = cr
done := make(chan bool)
done := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
go func() {
select {
case <-ctx.Done():
cr.Cancel()
case <-done:
}
wg.Done()
}()
defer func() {
close(done)
wg.Wait()
// Could put the Close in the above goroutine, but if "read" is
// immediately called again, the Close might overlap with creating a
// new cancelreader. Want this cancelreader to be completely closed
// by the time readLine returns.
cr.Close()
}()
defer close(done)
}

for {
Expand Down

0 comments on commit 1235810

Please sign in to comment.