Skip to content

Commit

Permalink
Fix azblob/download data race (#22334)
Browse files Browse the repository at this point in the history
* data race while downloadFile

* changelog update

* cast variable type
  • Loading branch information
tanyasethi-msft authored Feb 6, 2024
1 parent 0970bdc commit d4384c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions sdk/storage/azblob/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes

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

### Other Changes

Expand Down
22 changes: 9 additions & 13 deletions sdk/storage/azblob/blob/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package blob
import (
"context"
"io"
"math"
"os"
"sync"
"time"
Expand Down Expand Up @@ -469,23 +470,18 @@ func (b *Client) downloadFile(ctx context.Context, writer io.Writer, o downloadO

buffers := shared.NewMMBPool(int(o.Concurrency), o.BlockSize)
defer buffers.Free()
acquireBuffer := func() ([]byte, error) {
select {
case b := <-buffers.Acquire():
// got a buffer
return b, nil
default:
// no buffer available; allocate a new buffer if possible
if _, err := buffers.Grow(); err != nil {
return nil, err
}

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

numChunks := uint16((count-1)/o.BlockSize) + 1
acquireBuffer := func() ([]byte, error) {
return <-buffers.Acquire(), nil
}

blocks := make([]chan []byte, numChunks)
for b := range blocks {
blocks[b] = make(chan []byte)
Expand Down

0 comments on commit d4384c8

Please sign in to comment.