From 153feb2e6022c138cfedc3c28f2e6745be13617d Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Mon, 17 Jul 2023 14:10:35 -0400 Subject: [PATCH] Fix deadlock when piping to shell associated file extension (#19940) --- .../engine/NativeCommandProcessor.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index b769bc026..326e96dfa 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -604,19 +604,6 @@ namespace System.Management.Automation { _nativeProcess = new Process() { StartInfo = startInfo }; _nativeProcess.Start(); - if (UpstreamIsNativeCommand) - { - SemaphoreSlim processInitialized = _processInitialized; - if (processInitialized is null) - { - lock (_sync) - { - processInitialized = _processInitialized ??= new SemaphoreSlim(0, 1); - } - } - - processInitialized?.Release(); - } } catch (Win32Exception) { @@ -695,6 +682,12 @@ namespace System.Management.Automation } #endif } + + if (UpstreamIsNativeCommand) + { + _processInitialized ??= new SemaphoreSlim(0, 1); + _processInitialized.Release(); + } } if (this.Command.MyInvocation.PipelinePosition < this.Command.MyInvocation.PipelineLength)