Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Fix race conditions introduced by parallelisation
Browse files Browse the repository at this point in the history
  • Loading branch information
hach-que committed Jan 19, 2017
1 parent 20b46ef commit 79344d9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ Protobuild.FunctionalTests/TestData/PackageUnifiedNuGetLocalInstallNoDedup/TestI
Protobuild.FunctionalTests/TestData/ExtractXSLTWorks/Build/NuGetPlatformMappings.xml
Protobuild.FunctionalTests/TestData/SafeResolveDefaultsWithoutFeatureSet/Package/Test.txt
Protobuild.FunctionalTests/TestData/ModuleInfoGetsUpgraded/xunit/
.vs/
2 changes: 1 addition & 1 deletion Protobuild.Internal/BuildResources/ResourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private Stream LoadOverriddableResource(ResourceType resourceType, Language lang
if (File.Exists(path))
{
loadHash = "path:" + path;
source = File.Open(path, FileMode.Open);
source = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
break;
}
}
Expand Down
56 changes: 32 additions & 24 deletions Protobuild.Internal/Core/ModuleExecution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Protobuild
{
internal class ModuleExecution : IModuleExecution
{
private static object _selfWorkingDirectoryLock = new object();

public Tuple<int, string, string> RunProtobuild(ModuleInfo module, string args, bool capture = false)
{
var invokeInline = false;
Expand All @@ -23,35 +25,41 @@ public Tuple<int, string, string> RunProtobuild(ModuleInfo module, string args,
var ourBuffer = new RedirectableBuffer();
RedirectableConsole.TargetBuffer = ourBuffer;
var needsEndSelfInvoke = true;
var old = Environment.CurrentDirectory;
try
lock (_selfWorkingDirectoryLock)
{
Environment.CurrentDirectory = module.Path;
var exitCode = ExecEnvironment.InvokeSelf(args.SplitCommandLine().ToArray());
RedirectableConsole.TargetBuffer = oldBuffer;
needsEndSelfInvoke = false;
if (capture)
{
return new Tuple<int, string, string>(
exitCode,
ourBuffer.Stdout,
ourBuffer.Stderr);
}
else
var oldLock = _selfWorkingDirectoryLock;
_selfWorkingDirectoryLock = new object();
var old = Environment.CurrentDirectory;
try
{
return new Tuple<int, string, string>(
exitCode,
string.Empty,
string.Empty);
Environment.CurrentDirectory = module.Path;
var exitCode = ExecEnvironment.InvokeSelf(args.SplitCommandLine().ToArray());
RedirectableConsole.TargetBuffer = oldBuffer;
needsEndSelfInvoke = false;
if (capture)
{
return new Tuple<int, string, string>(
exitCode,
ourBuffer.Stdout,
ourBuffer.Stderr);
}
else
{
return new Tuple<int, string, string>(
exitCode,
string.Empty,
string.Empty);
}
}
}
finally
{
if (needsEndSelfInvoke)
finally
{
RedirectableConsole.TargetBuffer = oldBuffer;
if (needsEndSelfInvoke)
{
RedirectableConsole.TargetBuffer = oldBuffer;
}
Environment.CurrentDirectory = old;
_selfWorkingDirectoryLock = oldLock;
}
Environment.CurrentDirectory = old;
}
}

Expand Down
1 change: 1 addition & 0 deletions Protobuild.Internal/ExecEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static class ExecEnvironment
/// </summary>
public static bool DoNotWrapExecutionInTry = false;

[ThreadStatic]
private static int _selfInvokeCounter = 0;

/// <summary>
Expand Down
Binary file modified Protobuild.exe
Binary file not shown.
Binary file modified Protobuild/Protobuild.Internal.dll.lzma
Binary file not shown.

0 comments on commit 79344d9

Please sign in to comment.