Skip to content

Commit f26f4f7

Browse files
authored
sync: fix deadlock for large files (#6372)
1 parent 9ebd837 commit f26f4f7

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

pkg/sync/sync.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,7 @@ func CopyData(src, dst object.ObjectStorage, key string, size int64, calChksum b
840840
}
841841

842842
type holder struct {
843-
done bool
844-
sync.Mutex
845-
*sync.Cond
843+
done chan struct{}
846844
}
847845

848846
var muHolder sync.Mutex
@@ -863,18 +861,20 @@ func fetchTask(tasks chan object.Object) (t object.Object, done func()) {
863861
}
864862
}()
865863
AGAIN:
866-
t = <-tasks
864+
if t == nil {
865+
t = <-tasks
866+
}
867867
muHolder.Lock()
868868
if len(holders) > 0 {
869869
h := holders[len(holders)-1]
870870
holders = holders[:len(holders)-1]
871871
muHolder.Unlock()
872-
tasks <- t // put back
873-
h.Lock()
874-
for !h.done {
875-
h.Wait()
872+
select {
873+
case tasks <- t: // put back
874+
t = nil
875+
<-h.done
876+
case <-h.done:
876877
}
877-
h.Unlock()
878878
goto AGAIN
879879
}
880880
defer muHolder.Unlock()
@@ -883,18 +883,13 @@ AGAIN:
883883
size = withoutSize(t).Size()
884884
}
885885
if size >= maxBlock*2 {
886-
h := &holder{}
887-
h.Cond = sync.NewCond(&h.Mutex)
886+
done := make(chan struct{})
887+
h := &holder{done: done}
888888
n := min(int(size)/maxBlock, 20)
889889
for i := 1; i < n; i++ {
890890
holders = append(holders, h)
891891
}
892-
return t, func() {
893-
h.Lock()
894-
defer h.Unlock()
895-
h.done = true
896-
h.Broadcast()
897-
}
892+
return t, func() { close(done) }
898893
} else {
899894
return t, func() {}
900895
}

0 commit comments

Comments
 (0)