Remove the unnecessary use of lock in PseudoParameterBinder to avoid deadlock (#2501)

This is the fix to MSFT:9039127 [RFC] : SMA Jobs stop processing, show various status but never change or progress; due to deadlock in sandbox process.
The fix is brought to GitHub to make sure GitHub powershell always has the latest changes.
This commit is contained in:
Chunqing Chen
2016-10-18 17:32:56 -07:00
committed by Dongbo Wang
parent 1d29d42ff7
commit 0908547083
@@ -333,24 +333,21 @@ namespace System.Management.Automation.Language
PseudoBindingInfo pseudoBinding = null;
if (Runspace.DefaultRunspace == null)
{
lock (s_bindCommandLock)
// Handle static binding from a non-PowerShell / C# application
// DefaultRunspace is a thread static field, so race condition will not happen because different threads will access different instances of "DefaultRunspace"
if (s_bindCommandRunspace == null)
{
if (s_bindCommandPowerShell == null)
{
// Create a mini runspace by remove the types and formats
InitialSessionState minimalState = InitialSessionState.CreateDefault2();
minimalState.Types.Clear();
minimalState.Formats.Clear();
s_bindCommandPowerShell = PowerShell.Create(minimalState);
}
// Handle static binding from a non-PowerShell / C# application
Runspace.DefaultRunspace = s_bindCommandPowerShell.Runspace;
// Static binding always does argument binding (not argument or parameter completion).
pseudoBinding = new PseudoParameterBinder().DoPseudoParameterBinding(commandAst, null, null, PseudoParameterBinder.BindingType.ArgumentBinding);
Runspace.DefaultRunspace = null;
// Create a mini runspace by remove the types and formats
InitialSessionState minimalState = InitialSessionState.CreateDefault2();
minimalState.Types.Clear();
minimalState.Formats.Clear();
s_bindCommandRunspace = RunspaceFactory.CreateRunspace(minimalState);
s_bindCommandRunspace.Open();
}
Runspace.DefaultRunspace = s_bindCommandRunspace;
// Static binding always does argument binding (not argument or parameter completion).
pseudoBinding = new PseudoParameterBinder().DoPseudoParameterBinding(commandAst, null, null, PseudoParameterBinder.BindingType.ArgumentBinding);
Runspace.DefaultRunspace = null;
}
else
{
@@ -360,8 +357,8 @@ namespace System.Management.Automation.Language
return new StaticBindingResult(commandAst, pseudoBinding);
}
private static Object s_bindCommandLock = new Object();
private static PowerShell s_bindCommandPowerShell = null;
[ThreadStatic]
static Runspace s_bindCommandRunspace = null;
}
/// <summary>