Skip to content

Commit

Permalink
Merge pull request #4106 from microsoft/bugfix/dead-lock-update
Browse files Browse the repository at this point in the history
bugfix/dead lock update
  • Loading branch information
andrueastman authored Feb 2, 2024
2 parents a85fd90 + 1c2b716 commit 928d61f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed a deadlock on update for multiple clients targeting the same local file.

## [1.11.0] - 2024-02-01

### Added
Expand Down
15 changes: 14 additions & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using AsyncKeyedLock;
using DotNet.Globbing;
using Kiota.Builder.Caching;
using Kiota.Builder.CodeDOM;
Expand Down Expand Up @@ -396,6 +397,11 @@ private void StopLogAndReset(Stopwatch sw, string prefix)
sw.Reset();
}

private static readonly AsyncKeyedLocker<string> localFilesLock = new(o =>
{
o.PoolSize = 20;
o.PoolInitialFill = 1;
});

private async Task<Stream> LoadStream(string inputPath, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -424,7 +430,14 @@ private async Task<Stream> LoadStream(string inputPath, CancellationToken cancel
try
{
#pragma warning disable CA2000 // disposed by caller
input = new FileStream(inputPath, FileMode.Open);
var inMemoryStream = new MemoryStream();
using (await localFilesLock.LockAsync(inputPath, cancellationToken).ConfigureAwait(false))
{// To avoid deadlocking on update with multiple clients for the same local description
using var fileStream = new FileStream(inputPath, FileMode.Open);
await fileStream.CopyToAsync(inMemoryStream, cancellationToken).ConfigureAwait(false);
}
inMemoryStream.Position = 0;
input = inMemoryStream;
#pragma warning restore CA2000
}
catch (Exception ex) when (ex is FileNotFoundException ||
Expand Down

0 comments on commit 928d61f

Please sign in to comment.