Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Retry creating file stream in LockFileFormat #2103

Merged
merged 1 commit into from
Jun 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Microsoft.Framework.PackageManager/Restore/LockFileFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading;
using Microsoft.Framework.Runtime.DependencyManagement;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -20,12 +21,40 @@ public class LockFileFormat

public LockFile Read(string filePath)
{
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var stream = OpenFileStream(filePath))
{
return Read(stream);
}
}

private static FileStream OpenFileStream(string filePath)
{
// Retry 3 times before re-throw the exception.
// It mitigates the race condition when DTH read lock file while VS is restoring projects.

int retry = 3;
while (true)
{
try
{
return new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (Exception)
{
if (retry > 0)
{
retry--;
Thread.Sleep(100);
}
else
{
throw;
}
}
}

}

public LockFile Read(Stream stream)
{
using (var textReader = new StreamReader(stream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading;
using Microsoft.Framework.Runtime.Json;
using NuGet;

namespace Microsoft.Framework.Runtime.DependencyManagement
{
{
internal class LockFileReader
{
public const int Version = -9996;
public const string LockFileName = "project.lock.json";

public LockFile Read(string filePath)
{
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var stream = OpenFileStream(filePath))
{
try
{
Expand All @@ -34,6 +35,34 @@ public LockFile Read(string filePath)
}
}
}

private static FileStream OpenFileStream(string filePath)
{
// Retry 3 times before re-throw the exception.
// It mitigates the race condition when DTH read lock file while VS is restoring projects.

int retry = 3;
while (true)
{
try
{
return new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (Exception)
{
if (retry > 0)
{
retry--;
Thread.Sleep(100);
}
else
{
throw;
}
}
}

}

internal LockFile Read(Stream stream)
{
Expand Down Expand Up @@ -254,7 +283,7 @@ private string ReadString(JsonValue json)
{
return (json as JsonString).Value;
}
else if(json is JsonNull)
else if (json is JsonNull)
{
return null;
}
Expand Down