Skip to content

Fix bad context state after debugging #1210

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 6 commits into from
Feb 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ namespace Microsoft.PowerShell.EditorServices.Services
{
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation;
using System.Runtime.InteropServices;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;

/// <summary>
Expand Down Expand Up @@ -68,6 +66,7 @@ static PowerShellContextService()
#region Fields

private readonly SemaphoreSlim resumeRequestHandle = AsyncUtils.CreateSimpleLockingSemaphore();
private readonly SemaphoreSlim sessionStateLock = AsyncUtils.CreateSimpleLockingSemaphore();

private readonly OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer _languageServer;
private readonly bool isPSReadLineEnabled;
Expand Down Expand Up @@ -745,6 +744,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
// Don't change our SessionState for ReadLine.
if (!executionOptions.IsReadLine)
{
await this.sessionStateLock.WaitAsync().ConfigureAwait(false);
shell.InvocationStateChanged += PowerShell_InvocationStateChanged;
}

Expand All @@ -768,6 +768,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
if (!executionOptions.IsReadLine)
{
shell.InvocationStateChanged -= PowerShell_InvocationStateChanged;
this.sessionStateLock.Release();
}

if (shell.HadErrors)
Expand Down Expand Up @@ -1204,45 +1205,58 @@ public void AbortExecution()
/// </param>
public void AbortExecution(bool shouldAbortDebugSession)
{
if (this.SessionState != PowerShellContextState.Aborting &&
this.SessionState != PowerShellContextState.Disposed)
if (this.SessionState == PowerShellContextState.Aborting
|| this.SessionState == PowerShellContextState.Disposed)
{
this.logger.LogTrace("Execution abort requested...");
this.logger.LogTrace(
string.Format(
$"Execution abort requested when already aborted (SessionState = {this.SessionState})"));
return;
}

this.logger.LogTrace("Execution abort requested...");

if (shouldAbortDebugSession)
{
this.ExitAllNestedPrompts();
}

if (this.PromptNest.IsInDebugger)
{
this.versionSpecificOperations.StopCommandInDebugger(this);
if (shouldAbortDebugSession)
{
this.ExitAllNestedPrompts();
this.ResumeDebugger(DebuggerResumeAction.Stop);
}
}
else
{
this.PromptNest.GetPowerShell(isReadLine: false).BeginStop(null, null);
}

if (this.PromptNest.IsInDebugger)
// TODO:
// This lock and state reset are a temporary fix at best.
// We need to investigate how the debugger should be interacting
// with PowerShell in this cancellation scenario.
//
// Currently we try to acquire a lock on the execution status changed event.
// If we can't, it's because a command is executing, so we shouldn't change the status.
// If we can, we own the status and should fire the event.
if (this.sessionStateLock.Wait(0))
{
try
{
if (shouldAbortDebugSession)
{
this.versionSpecificOperations.StopCommandInDebugger(this);
this.ResumeDebugger(DebuggerResumeAction.Stop);
}
else
{
this.versionSpecificOperations.StopCommandInDebugger(this);
}
this.SessionState = PowerShellContextState.Aborting;
this.OnExecutionStatusChanged(
ExecutionStatus.Aborted,
null,
false);
}
else
finally
{
this.PromptNest.GetPowerShell(isReadLine: false).BeginStop(null, null);
this.SessionState = PowerShellContextState.Ready;
this.sessionStateLock.Release();
}

this.SessionState = PowerShellContextState.Aborting;

this.OnExecutionStatusChanged(
ExecutionStatus.Aborted,
null,
false);
}
else
{
this.logger.LogTrace(
string.Format(
$"Execution abort requested when already aborted (SessionState = {this.SessionState})"));
}
}

Expand Down