8000 Only stop transcription when all runspaces are closed by chunqingchen · Pull Request #3896 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Only stop transcription when all runspaces are closed #3896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2017

Conversation

chunqingchen
Copy link
Contributor
@chunqingchen chunqingchen commented May 31, 2017

Fix issue #2334

Summary of the issue:

If user create any runspace within transcription and later close the runspace within the transcription, transcription get closed

Root cause of the issue:

When any runspace get closed, StopAllTranscribing() get called and the transcription get closed
Fix:

Check and make sure the we are closing the last runspace to stop transcription. otherwise user still need to explicitly call stop-transcript to close the transcription.

@@ -863,7 +863,13 @@ private void DoCloseHelper()
}

if ((hostRunspace == null) || (this == hostRunspace))
{
{
// We should not close transcripting if we are not under default runspace.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused by the code and the comment as I misread the comment initially. I think you should change the comment to avoid a double negative.

We should close transcripting only if we are running in default runspace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

@chunqingchen
Copy link
Contributor Author

@SteveL-MSFT your comment is resolved.

@adityapatwardhan
Copy link
Member

I tried fixing this in Windows Powershell 5.1, but it broke with the following repro. Powershell crashes. Try running the following with a debugger attached.

powershell.exe -c "start-transcript"

// We should close transcripting only if we are running in default runspace.
if (executionContext.CurrentRunspace != null && DefaultRunspace != null && executionContext.CurrentRunspace.Id != DefaultRunspace.Id)
{
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test case to cover the implicit closing of transcription

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

{
{
// We should close transcripting only if we are running in default runspace.
if (executionContext.CurrentRunspace != null && DefaultRunspace != null && executionContext.CurrentRunspace.Id != DefaultRunspace.Id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky because DefaultRunspace is a thread static property and so there can be multiple DefaultRunspaces for multiple threads. It is really hard to know when all runspaces are closed. We do now keep a list of all (non-disposed) local runspaces. See Connection.cs https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/hostifaces/Connection.cs#L799
You could search through this list and if there is only one runspace left in the "open" state (meaning this is the last runspace) then do the stop transcribing.
This list only shows runspaces that have not yet been disposed. But a runspace can be non-open and still be in the list if it was not disposed, so you need to check the state of each runspace.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I just realized that the "AmsiUtils.Uninitialize()" call should be done at the last runspace being closed as well. Can you please move that call into this logic too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved.

@chunqingchen chunqingchen force-pushed the bugfix1 branch 2 times, most recently from 07358e3 to 44aee25 Compare June 6, 2017 23:40
@chunqingchen
Copy link
Contributor Author

@adityapatwardhan I tried your repro and powershell isn't crashing.

$transcriptFilePath | Should contain "After Dispose"
}

It "Transcription should remain active if other runspace in the host get closed" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this exactly the same as above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops~ ! deleted.

@chunqingchen
Copy link
Contributor Author

@PaulHigin @SteveL-MSFT your comments are resolved.

PSHostUserInterface host = executionContext.EngineHostInterface.UI;
if (host != null)
{
host.StopAllTranscribing();
}

AmsiUtils.Uninitialize();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AmsiUtils.Uninitialize() is still being called at line 930 below. Please remove that call so that uninitialize only happens when the last runspace closes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

Test-Path $transcriptFilePath | Should be $true
$transcriptFilePath | Should contain "Before Dispose"
$transcriptFilePath | Should contain "Windows PowerShell transcript end"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add new line at the end of the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This problem is still in the file the image at the end of the review indicates there is not trailing newline.

}

It "Transcription should be closed if the only runspace gets closed" {
powershell "start-transcript $transcriptFilePath; Write-Host 'Before Dispose';"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On windows, this will start the inbox Powershell, not v6.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even on Linux, it will default to the installed one and not the running one. See https://github.com/PowerShell/PowerShell/blob/master/test/powershell/Host/ConsoleHost.Tests.ps1#L12

}

It "Transcription should be closed if the only runspace gets closed" {
powershell "start-transcript $transcriptFilePath; Write-Host 'Before Dispose';"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even on Linux, it will default to the installed one and not the running one. See https://github.com/PowerShell/PowerShell/blob/master/test/powershell/Host/ConsoleHost.Tests.ps1#L12

@chunqingchen chunqingchen force-pushed the bugfix1 branch 3 times, most recently from 80aac09 to 771e55a Compare June 8, 2017 00:11
@chunqingchen
Copy link
Contributor Author

@PaulHigin @SteveL-MSFT @adityapatwardhan your comments are resolved.

Copy link
Member
@adityapatwardhan adityapatwardhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the newline at the end of the file is fixed.

Copy link
Contributor
@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chunqingchen
Copy link
Contributor Author

@TravisEz13 Hi Travis, please merge the pr

@chunqingchen chunqingchen changed the title Fix the issue that Transcription for the host stops when any runspace… Fix the issue that PS only stops transcription when all runspaces are closed Jun 13, 2017
@TravisEz13 TravisEz13 changed the title Fix the issue that PS only stops transcription when all runspaces are closed Only stop transcription when all runspaces are closed Jun 13, 2017
@TravisEz13 TravisEz13 merged commit 65f96e0 into PowerShell:master Jun 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0