From 105702ebe0e1cf7b36ddb075d4cde22b4b3f1bbe Mon Sep 17 00:00:00 2001 From: Konstantin Lebedev <9497591+kmlebedev@users.noreply.github.com> Date: Tue, 30 Aug 2022 12:03:01 +0500 Subject: [PATCH] avoid data race access to uploadReaderToChunks.uploadErr (#3550) avoid data race access to uploadErr https://github.com/seaweedfs/seaweedfs/issues/3549 --- weed/server/filer_server_handlers_write_upload.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/weed/server/filer_server_handlers_write_upload.go b/weed/server/filer_server_handlers_write_upload.go index 7756b5d59..1a7449670 100644 --- a/weed/server/filer_server_handlers_write_upload.go +++ b/weed/server/filer_server_handlers_write_upload.go @@ -52,6 +52,7 @@ func (fs *FilerServer) uploadReaderToChunks(w http.ResponseWriter, r *http.Reque var bytesBufferCounter int64 bytesBufferLimitCond := sync.NewCond(new(sync.Mutex)) var fileChunksLock sync.Mutex + var uploadErrLock sync.Mutex for { // need to throttle used byte buffer @@ -77,7 +78,9 @@ func (fs *FilerServer) uploadReaderToChunks(w http.ResponseWriter, r *http.Reque bufPool.Put(bytesBuffer) atomic.AddInt64(&bytesBufferCounter, -1) bytesBufferLimitCond.Signal() + uploadErrLock.Lock() uploadErr = err + uploadErrLock.Unlock() break } if chunkOffset == 0 && !isAppend { @@ -105,8 +108,12 @@ func (fs *FilerServer) uploadReaderToChunks(w http.ResponseWriter, r *http.Reque }() chunk, toChunkErr := fs.dataToChunk(fileName, contentType, bytesBuffer.Bytes(), offset, so) - if uploadErr == nil && toChunkErr != nil { - uploadErr = toChunkErr + if toChunkErr != nil { + uploadErrLock.Lock() + if uploadErr == nil { + uploadErr = toChunkErr + } + uploadErrLock.Unlock() } if chunk != nil { fileChunksLock.Lock()