Skip to content

Commit

Permalink
Rewrite direct SessionStateProxy calls
Browse files Browse the repository at this point in the history
All interactions with the runspace must be done through
PowerShellContext now that nested PowerShell instances are
encountered frequently.

Also fix a bunch of race conditions that were made more obvious
with the changes.
  • Loading branch information
SeeminglyScience committed Jun 2, 2018
1 parent 21e6b5f commit fa2faba
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 194 deletions.
8 changes: 8 additions & 0 deletions PowerShellEditorServices.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,18 @@ task LayoutModule -After Build {
New-Item -Force $PSScriptRoot\module\PowerShellEditorServices\bin\Core -Type Directory | Out-Null

Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
if ($Configuration -eq 'Debug') {
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\* -Filter Microsoft.PowerShell.EditorServices*.pdb -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
}

Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\UnixConsoleEcho.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\libdisablekeyecho.* -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
if (!$script:IsUnix) {
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
if ($Configuration -eq 'Debug') {
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\* -Filter Microsoft.PowerShell.EditorServices*.pdb -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
}

Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\Newtonsoft.Json.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\UnixConsoleEcho.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
}
Expand Down
6 changes: 5 additions & 1 deletion module/Start-EditorServices.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ param(
[ValidateSet("Normal", "Verbose", "Error","Diagnostic")]
$LogLevel,

[string[]]
$FeatureFlags = @(),

[switch]
$WaitForDebugger,

Expand Down Expand Up @@ -163,7 +166,8 @@ $editorServicesHost =
-LanguageServicePort $languageServicePort `
-DebugServicePort $debugServicePort `
-BundledModulesPath $BundledModulesPath `
-WaitForDebugger:$WaitForDebugger.IsPresent
-WaitForDebugger:$WaitForDebugger.IsPresent `
-FeatureFlags $FeatureFlags

# TODO: Verify that the service is started

Expand Down
36 changes: 30 additions & 6 deletions src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public DebugAdapter(
}

/// <summary>
/// Gets a boolean that indicates whether the current debug adapter is
/// Gets a boolean that indicates whether the current debug adapter is
/// using a temporary integrated console.
/// </summary>
public bool IsUsingTempIntegratedConsole { get; private set; }
Expand Down Expand Up @@ -118,6 +118,17 @@ protected Task LaunchScript(RequestContext<object> requestContext)

private async Task OnExecutionCompleted(Task executeTask)
{
try
{
await executeTask;
}
catch (Exception e)
{
Logger.Write(
LogLevel.Error,
"Exception occurred while awaiting debug launch task.\n\n" + e.ToString());
}

Logger.Write(LogLevel.Verbose, "Execution completed, terminating...");

this.executionCompleted = true;
Expand Down Expand Up @@ -471,7 +482,7 @@ protected async Task HandleDisconnectRequest(
if (this.executionCompleted == false)
{
this.disconnectRequestContext = requestContext;
this.editorSession.PowerShellContext.AbortExecution();
this.editorSession.PowerShellContext.AbortExecution(shouldAbortDebugSession: true);

if (this.isInteractiveDebugSession)
{
Expand Down Expand Up @@ -506,7 +517,7 @@ protected async Task HandleSetBreakpointsRequest(
}
}
catch (Exception e) when (
e is FileNotFoundException ||
e is FileNotFoundException ||
e is DirectoryNotFoundException ||
e is IOException ||
e is NotSupportedException ||
Expand Down Expand Up @@ -653,7 +664,7 @@ protected async Task HandleSetExceptionBreakpointsRequest(
RequestContext<object> requestContext)
{
// TODO: When support for exception breakpoints (unhandled and/or first chance)
// are added to the PowerShell engine, wire up the VSCode exception
// are added to the PowerShell engine, wire up the VSCode exception
// breakpoints here using the pattern below to prevent bug regressions.
//if (!this.noDebug)
//{
Expand Down Expand Up @@ -756,6 +767,20 @@ protected async Task HandleStackTraceRequest(
StackFrameDetails[] stackFrames =
editorSession.DebugService.GetStackFrames();

// Handle a rare race condition where the adapter requests stack frames before they've
// begun building.
if (stackFrames == null)
{
await requestContext.SendResult(
new StackTraceResponseBody
{
StackFrames = new StackFrame[0],
TotalFrames = 0
});

return;
}

List<StackFrame> newStackFrames = new List<StackFrame>();

int startFrameIndex = stackTraceParams.StartFrame ?? 0;
Expand All @@ -779,8 +804,7 @@ protected async Task HandleStackTraceRequest(
i));
}

await requestContext.SendResult(
new StackTraceResponseBody
await requestContext.SendResult( new StackTraceResponseBody
{
StackFrames = newStackFrames.ToArray(),
TotalFrames = newStackFrames.Count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,15 @@ private static async Task DelayThenInvokeDiagnostics(
catch (TaskCanceledException)
{
// If the task is cancelled, exit directly
foreach (var script in filesToAnalyze)
{
await PublishScriptDiagnostics(
script,
script.SyntaxMarkers,
correctionIndex,
eventSender);
}

return;
}

Expand Down
Loading

0 comments on commit fa2faba

Please sign in to comment.