Skip to content

Commit

Permalink
fix(dsync): Don't call the finalize hook more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Mar 17, 2020
1 parent 254aa17 commit e0b01c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dsync/dsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ func (ds *Dsync) ReceiveBlock(sid, hash string, data []byte) ReceiveResponse {
res := sess.ReceiveBlock(hash, bytes.NewReader(data))
log.Debugf("received block: %s", res.Hash)

// if we're done transferring, finalize!
if res.Status == StatusOk && sess.Complete() {
// check if transfer has completed, if so finalize it, but only once
if res.Status == StatusOk && sess.IsFinalizedOnce() {
if err := ds.finalizeReceive(sess); err != nil {
return ReceiveResponse{
Hash: sess.info.RootCID().String(),
Expand Down
19 changes: 19 additions & 0 deletions dsync/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"math/rand"
"sync"

ipld "github.com/ipfs/go-ipld-format"
coreiface "github.com/ipfs/interface-go-ipfs-core"
Expand All @@ -23,6 +24,8 @@ type session struct {
diff *dag.Manifest
prog dag.Completion
progCh chan dag.Completion
lock sync.Mutex
fin bool
}

// newSession creates a receive state machine
Expand Down Expand Up @@ -99,6 +102,22 @@ func (s *session) completionChanged() {
s.progCh <- s.prog
}

// IsFinalizedOnce will return true if the session is complete, but only the first time it is
// called, even if multiple threads call this function at the same time
func (s *session) IsFinalizedOnce() bool {
if !s.Complete() {
return false
}
ret := false
s.lock.Lock()
if !s.fin {
ret = true
s.fin = true
}
defer s.lock.Unlock()
return ret
}

// the best stack overflow answer evaarrr: https://stackoverflow.com/a/22892986/9416066
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
Expand Down

0 comments on commit e0b01c8

Please sign in to comment.