Skip to content

Commit d4384c8

Browse files
Fix azblob/download data race (#22334)
* data race while downloadFile * changelog update * cast variable type
1 parent 0970bdc commit d4384c8

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

sdk/storage/azblob/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

99
### Bugs Fixed
10+
* Fix concurrency issue while Downloading File. Fixes [#22156](https://github.com/Azure/azure-sdk-for-go/issues/22156).
1011

1112
### Other Changes
1213

sdk/storage/azblob/blob/client.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package blob
99
import (
1010
"context"
1111
"io"
12+
"math"
1213
"os"
1314
"sync"
1415
"time"
@@ -469,23 +470,18 @@ func (b *Client) downloadFile(ctx context.Context, writer io.Writer, o downloadO
469470

470471
buffers := shared.NewMMBPool(int(o.Concurrency), o.BlockSize)
471472
defer buffers.Free()
472-
acquireBuffer := func() ([]byte, error) {
473-
select {
474-
case b := <-buffers.Acquire():
475-
// got a buffer
476-
return b, nil
477-
default:
478-
// no buffer available; allocate a new buffer if possible
479-
if _, err := buffers.Grow(); err != nil {
480-
return nil, err
481-
}
482473

483-
// either grab the newly allocated buffer or wait for one to become available
484-
return <-buffers.Acquire(), nil
474+
numChunks := uint16((count-1)/o.BlockSize + 1)
475+
for bufferCounter := float64(0); bufferCounter < math.Min(float64(numChunks), float64(o.Concurrency)); bufferCounter++ {
476+
if _, err := buffers.Grow(); err != nil {
477+
return 0, err
485478
}
486479
}
487480

488-
numChunks := uint16((count-1)/o.BlockSize) + 1
481+
acquireBuffer := func() ([]byte, error) {
482+
return <-buffers.Acquire(), nil
483+
}
484+
489485
blocks := make([]chan []byte, numChunks)
490486
for b := range blocks {
491487
blocks[b] = make(chan []byte)

0 commit comments

Comments
 (0)