Skip to content

Commit fa2faba

Browse files
Rewrite direct SessionStateProxy calls
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.
1 parent 21e6b5f commit fa2faba

File tree

13 files changed

+477
-194
lines changed

13 files changed

+477
-194
lines changed

PowerShellEditorServices.build.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,18 @@ task LayoutModule -After Build {
189189
New-Item -Force $PSScriptRoot\module\PowerShellEditorServices\bin\Core -Type Directory | Out-Null
190190

191191
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
192+
if ($Configuration -eq 'Debug') {
193+
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\* -Filter Microsoft.PowerShell.EditorServices*.pdb -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
194+
}
195+
192196
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\UnixConsoleEcho.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
193197
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\libdisablekeyecho.* -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
194198
if (!$script:IsUnix) {
195199
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
200+
if ($Configuration -eq 'Debug') {
201+
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\* -Filter Microsoft.PowerShell.EditorServices*.pdb -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
202+
}
203+
196204
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\Newtonsoft.Json.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
197205
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\UnixConsoleEcho.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
198206
}

module/Start-EditorServices.ps1

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ param(
4747
[ValidateSet("Normal", "Verbose", "Error","Diagnostic")]
4848
$LogLevel,
4949

50+
[string[]]
51+
$FeatureFlags = @(),
52+
5053
[switch]
5154
$WaitForDebugger,
5255

@@ -163,7 +166,8 @@ $editorServicesHost =
163166
-LanguageServicePort $languageServicePort `
164167
-DebugServicePort $debugServicePort `
165168
-BundledModulesPath $BundledModulesPath `
166-
-WaitForDebugger:$WaitForDebugger.IsPresent
169+
-WaitForDebugger:$WaitForDebugger.IsPresent `
170+
-FeatureFlags $FeatureFlags
167171

168172
# TODO: Verify that the service is started
169173

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

+30-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public DebugAdapter(
5555
}
5656

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

119119
private async Task OnExecutionCompleted(Task executeTask)
120120
{
121+
try
122+
{
123+
await executeTask;
124+
}
125+
catch (Exception e)
126+
{
127+
Logger.Write(
128+
LogLevel.Error,
129+
"Exception occurred while awaiting debug launch task.\n\n" + e.ToString());
130+
}
131+
121132
Logger.Write(LogLevel.Verbose, "Execution completed, terminating...");
122133

123134
this.executionCompleted = true;
@@ -471,7 +482,7 @@ protected async Task HandleDisconnectRequest(
471482
if (this.executionCompleted == false)
472483
{
473484
this.disconnectRequestContext = requestContext;
474-
this.editorSession.PowerShellContext.AbortExecution();
485+
this.editorSession.PowerShellContext.AbortExecution(shouldAbortDebugSession: true);
475486

476487
if (this.isInteractiveDebugSession)
477488
{
@@ -506,7 +517,7 @@ protected async Task HandleSetBreakpointsRequest(
506517
}
507518
}
508519
catch (Exception e) when (
509-
e is FileNotFoundException ||
520+
e is FileNotFoundException ||
510521
e is DirectoryNotFoundException ||
511522
e is IOException ||
512523
e is NotSupportedException ||
@@ -653,7 +664,7 @@ protected async Task HandleSetExceptionBreakpointsRequest(
653664
RequestContext<object> requestContext)
654665
{
655666
// TODO: When support for exception breakpoints (unhandled and/or first chance)
656-
// are added to the PowerShell engine, wire up the VSCode exception
667+
// are added to the PowerShell engine, wire up the VSCode exception
657668
// breakpoints here using the pattern below to prevent bug regressions.
658669
//if (!this.noDebug)
659670
//{
@@ -756,6 +767,20 @@ protected async Task HandleStackTraceRequest(
756767
StackFrameDetails[] stackFrames =
757768
editorSession.DebugService.GetStackFrames();
758769

770+
// Handle a rare race condition where the adapter requests stack frames before they've
771+
// begun building.
772+
if (stackFrames == null)
773+
{
774+
await requestContext.SendResult(
775+
new StackTraceResponseBody
776+
{
777+
StackFrames = new StackFrame[0],
778+
TotalFrames = 0
779+
});
780+
781+
return;
782+
}
783+
759784
List<StackFrame> newStackFrames = new List<StackFrame>();
760785

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

782-
await requestContext.SendResult(
783-
new StackTraceResponseBody
807+
await requestContext.SendResult( new StackTraceResponseBody
784808
{
785809
StackFrames = newStackFrames.ToArray(),
786810
TotalFrames = newStackFrames.Count

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,15 @@ private static async Task DelayThenInvokeDiagnostics(
14631463
catch (TaskCanceledException)
14641464
{
14651465
// If the task is cancelled, exit directly
1466+
foreach (var script in filesToAnalyze)
1467+
{
1468+
await PublishScriptDiagnostics(
1469+
script,
1470+
script.SyntaxMarkers,
1471+
correctionIndex,
1472+
eventSender);
1473+
}
1474+
14661475
return;
14671476
}
14681477

0 commit comments

Comments
 (0)