Skip to content

Commit

Permalink
secure_bucket_client: do not send to a closed chan
Browse files Browse the repository at this point in the history
  • Loading branch information
maurycy authored Dec 3, 2020
1 parent 51f4c46 commit 1021006
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions core/textile/secure_bucket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"regexp"
"strings"
"sync"
"sync/atomic"

"github.com/FleekHQ/space-daemon/config"
"github.com/FleekHQ/space-daemon/core/ipfs"
Expand Down Expand Up @@ -291,19 +293,29 @@ func getTempFileName(encPath string) string {
}

func (s *SecureBucketClient) racePullFile(ctx context.Context, key, encPath string, w io.Writer, opts ...bc.Option) error {
pullers := []pathPullingFn{s.pullFileFromDHT, s.pullFileFromLocal, s.pullFileFromClient}
pullers := []pathPullingFn{s.pullFileFromLocal, s.pullFileFromDHT, s.pullFileFromClient}

var pullSuccessClosed uint32
var pullSuccessMutex sync.Mutex

pullSuccess := make(chan *pullSuccessResponse)

defer func() {
pullSuccessMutex.Lock()
defer pullSuccessMutex.Unlock()

atomic.StoreUint32(&pullSuccessClosed, 1)
close(pullSuccess)
}()

errc := make(chan error)
defer close(pullSuccess)

ctxWithCancel, cancelPulls := context.WithCancel(ctx)
pendingFns := len(pullers)
erroredFns := 0

for _, fn := range pullers {
f, err := ioutil.TempFile("", "*-"+getTempFileName(encPath))

if err != nil {
cancelPulls()
return err
Expand All @@ -323,7 +335,18 @@ func (s *SecureBucketClient) racePullFile(ctx context.Context, key, encPath stri
shouldCache: shouldCache,
}

pullSuccess <- chanRes
if ctxWithCancel.Err() != nil {
errc <- ctxWithCancel.Err()
return
}

pullSuccessMutex.Lock()
defer pullSuccessMutex.Unlock()

if atomic.LoadUint32(&pullSuccessClosed) == 0 {
pullSuccess <- chanRes
}

errc <- nil
}(fn, f)
}
Expand Down

0 comments on commit 1021006

Please sign in to comment.