8000 Fix the issue that PS only stops transcription when all runspaces are… · PowerShell/PowerShell@65f96e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65f96e0

Browse files
chunqingchenTravisEz13
authored andcommitted
Fix the issue that PS only stops transcription when all runspaces are closed (#3896)
Only stop transcription when all runspaces are closed
1 parent 6bb12c9 commit 65f96e0

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/System.Management.Automation/engine/hostifaces/LocalConnection.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -863,12 +863,24 @@ private void DoCloseHelper()
863863
}
864864

865865
if ((hostRunspace == null) || (this == hostRunspace))
866-
{
866+
{
867+
// We should close transcripting only if we are closing the last opened runspace.
868+
foreach (Runspace runspace in RunspaceList)
869+
{
870+
// At this stage, the last opened runspace should be at closing state.
871+
if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
872+
{
873+
return;
874+
}
875+
}
876+
867877
PSHostUserInterface host = executionContext.EngineHostInterface.UI;
868878
if (host != null)
869879
{
870880
host.StopAllTranscribing();
871881
}
882+
883+
AmsiUtils.Uninitialize();
872884
}
873885
}
874886

@@ -913,10 +925,7 @@ private void DoCloseHelper()
913925

914926
//Log engine lifecycle event.
915927
MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Stopped);
916-
917-
// Uninitialize the AMSI scan interface
918-
AmsiUtils.Uninitialize();
919-
928+
920929
//All pipelines have been canceled. Close the runspace.
921930
_engine = null;
922931
_commandFactory = null;

test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,34 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" {
106106
$expectedError = "CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand"
107107
ValidateTranscription -scriptToExecute $script -outputFilePath $null -expectedError $expectedError
108108
}
109+
It "Transcription should remain active if other runspace in the host get closed" {
110+
try{
111+
$ps = [powershell]::Create()
112+
$ps.addscript("Start-Transcript -path $transcriptFilePath").Invoke()
113+
$ps.addscript('$rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace()').Invoke()
114+
$ps.addscript('$rs.open()').Invoke()
115+
$ps.addscript('$rs.Dispose()').Invoke()
116+
$ps.addscript('Write-Host "After Dispose"').Invoke()
117+
$ps.addscript("Stop-Transcript").Invoke()
118+
} finally {
119+
if ($ps -ne $null) {
120+
$ps.Dispose()
121+
}
122+
}
123+
124+
125+
Test-Path $transcriptFilePath | Should be $true
126+
$transcriptFilePath | Should contain "After Dispose"
127+
}
128+
129+
It "Transcription should be closed if the only runspace gets closed" {
130+
$powerShellPath = [System.Diagnostics.Process]::GetCurrentProcess().Path
131+
$powerShellCommand = $powerShellPath + ' "start-transcript $transcriptFilePath; Write-Host ''Before 624B Dispose'';"'
132+
Invoke-Expression $powerShellCommand
133+
134+
Test-Path $transcriptFilePath | Should be $true
135+
$transcriptFilePath | Should contain "Before Dispose"
136+
$transcriptFilePath | Should contain "Windows PowerShell transcript end"
137+
}
138+
109139
}

0 commit comments

Comments
 (0)
0