Merge pull request #4019 from MichaelEischer/fix-file-saver-race
archiver: Fix race condition resulting in files containing null IDs
This commit is contained in:
commit
32c9667990
2 changed files with 8 additions and 0 deletions
|
@ -48,14 +48,19 @@ func TestBlobSaver(t *testing.T) {
|
||||||
|
|
||||||
var wait sync.WaitGroup
|
var wait sync.WaitGroup
|
||||||
var results []SaveBlobResponse
|
var results []SaveBlobResponse
|
||||||
|
var lock sync.Mutex
|
||||||
|
|
||||||
wait.Add(20)
|
wait.Add(20)
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
buf := &Buffer{Data: []byte(fmt.Sprintf("foo%d", i))}
|
buf := &Buffer{Data: []byte(fmt.Sprintf("foo%d", i))}
|
||||||
idx := i
|
idx := i
|
||||||
|
lock.Lock()
|
||||||
results = append(results, SaveBlobResponse{})
|
results = append(results, SaveBlobResponse{})
|
||||||
|
lock.Unlock()
|
||||||
b.Save(ctx, restic.DataBlob, buf, func(res SaveBlobResponse) {
|
b.Save(ctx, restic.DataBlob, buf, func(res SaveBlobResponse) {
|
||||||
|
lock.Lock()
|
||||||
results[idx] = res
|
results[idx] = res
|
||||||
|
lock.Unlock()
|
||||||
wait.Done()
|
wait.Done()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,7 +199,10 @@ func (s *FileSaver) saveFile(ctx context.Context, chnker *chunker.Chunker, snPat
|
||||||
|
|
||||||
// add a place to store the saveBlob result
|
// add a place to store the saveBlob result
|
||||||
pos := idx
|
pos := idx
|
||||||
|
|
||||||
|
lock.Lock()
|
||||||
node.Content = append(node.Content, restic.ID{})
|
node.Content = append(node.Content, restic.ID{})
|
||||||
|
lock.Unlock()
|
||||||
|
|
||||||
s.saveBlob(ctx, restic.DataBlob, buf, func(sbr SaveBlobResponse) {
|
s.saveBlob(ctx, restic.DataBlob, buf, func(sbr SaveBlobResponse) {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
|
|
Loading…
Reference in a new issue