In BlazorServer app, while uploading a bunch of small files all at once (100 files; 30 - 80k each), I encounter a race condition with the following code:
public async Task UploadAsync() {
this._fileContent = new MemoryStream();
using var readStream = OpenReadStream(AppConfiguration.MaxFileSize, cancellationToken);
byte[] buffer = new byte[30 * 1024]; // Buffer 30k, let's call it "30kb-chunk"
// ...
while ((bytesRead = await readStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0) {
await _fileContent.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
// ...
}
// ...
// this._fileContent {MemoryStream} gets saved to .zip (using System.IO.Compression.ZipArchive) along with some other stuff; Not relevant here
}
In one example, where files were uploaded "locally" (logged-in to a server & drag&drop locally; upload was super-fast), about 4% of the files were erroneously saved as:
[2nd 30kb-chunk]
[1st 30kb-chunk]
[2nd 30kb-chunk]
... (I guess mix-up could happen with other chunks as well)