Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dtivel authored and jainaashish committed Oct 8, 2018
1 parent 741a137 commit e0340cc
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/NuGet.Core/NuGet.Protocol/HttpSource/HttpCacheUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,43 @@ public static async Task CreateCacheFileAsync(
// The update of a cached file is divided into two steps:
// 1) Delete the old file.
// 2) Create a new file with the same name.

// Some FileStream operations on Windows are synchronous even though it may not seem so.
// The HTTP stack rewrite in .NET Core 2.1 introduced circumstances whereby these
// synchronous FileStream calls will keep an IO completion thread busy and block other
// HTTP requests from completing. The immediate solution is to perform write and read
// operations on separate streams, but only on .NET Core where the problem exists.
// See https://github.com/dotnet/corefx/issues/31914 for details.
const int writeBufferSize =
#if IS_CORECLR
1; // This disables write buffering.
#else
BufferSize;
#endif

using (var fileStream = new FileStream(
result.NewFile,
FileMode.Create,
FileAccess.ReadWrite,
FileAccess.Write,
FileShare.None,
BufferSize,
writeBufferSize,
useAsync: true))
{
using (var networkStream = await response.Content.ReadAsStreamAsync())
{
await networkStream.CopyToAsync(fileStream, BufferSize, cancellationToken);
}
}

using (var fileStream = new FileStream(
result.NewFile,
FileMode.Open,
FileAccess.Read,
FileShare.None,
BufferSize,
useAsync: true))
{
// Validate the content before putting it into the cache.
fileStream.Seek(0, SeekOrigin.Begin);
ensureValidContents?.Invoke(fileStream);
}

Expand Down

0 comments on commit e0340cc

Please sign in to comment.