Skip to content
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

Implement temporary workaround for race condition in REPL #1774

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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 @@ -27,6 +27,10 @@ internal class ConfigurationDoneHandler : IConfigurationDoneHandler
// instead hide from the user with pretty strings (or perhaps not write out at all).
private static readonly PowerShellExecutionOptions s_debuggerExecutionOptions = new()
{
// NOTE: We want to interrupt the current foreground task because otherwise we won't run
// this until the idle handler processes it, but we also don't want it to run under the
// idle handler, so both of these are true.
InterruptCurrentForeground = true,
MustRunInForeground = true,
WriteInputToHost = true,
WriteOutputToHost = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public PsrlReadLine(
_psrlProxy.OverrideIdleHandler(onIdleAction);
}

public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(representation: "ReadLine", new ExecutionOptions { MustRunInForeground = true }, InvokePSReadLine, cancellationToken);
public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(
representation: "ReadLine",
new ExecutionOptions { MustRunInForeground = true, InterruptCurrentForeground = true, },
InvokePSReadLine,
cancellationToken);

protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => _psesHost.ReadKey(intercept: true, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public bool TryTake(out T item)

public IDisposable BlockConsumers() => PriorityQueueBlockLifetime.StartBlocking(_blockConsumersEvent);

public void Dispose() => ((IDisposable)_blockConsumersEvent).Dispose();
public void Dispose() => _blockConsumersEvent.Dispose();

private class PriorityQueueBlockLifetime : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ private void RunExecutionLoop(bool isForDebug = false)

try
{
// TODO: This works around a race condition around executing our interactive
// tasks, and is a HIGH PRIORITY to fix.
Thread.Sleep(200);
DoOneRepl(cancellationScope.CancellationToken);
}
catch (OperationCanceledException)
Expand Down Expand Up @@ -736,6 +739,11 @@ private void DoOneRepl(CancellationToken cancellationToken)
StopDebugContext();
}

if (cancellationToken.IsCancellationRequested)
{
return;
Copy link
Member Author

Choose a reason for hiding this comment

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

LMAO it did need this?!

}

// When a task must run in the foreground, we cancel out of the idle loop and return to the top level.
// At that point, we would normally run a REPL, but we need to immediately execute the task.
// So we set _skipNextPrompt to do that.
Expand Down Expand Up @@ -949,6 +957,7 @@ private Runspace CreateInitialRunspace(InitialSessionState initialSessionState)
return runspace;
}

// NOTE: This token is received from PSReadLine, and it _is_ the ReadKey cancellation token!
private void OnPowerShellIdle(CancellationToken idleCancellationToken)
{
IReadOnlyList<PSEventSubscriber> eventSubscribers = _mainRunspaceEngineIntrinsics.Events.Subscribers;
Expand Down Expand Up @@ -1102,27 +1111,27 @@ private void OnDebuggerStopped(object sender, DebuggerStopEventArgs debuggerStop

void OnDebuggerStoppedImpl(object sender, DebuggerStopEventArgs debuggerStopEventArgs)
{
// If the debug server is NOT active, we need to synchronize state and start it.
if (!DebugContext.IsDebugServerActive)
{
_languageServer?.SendNotification("powerShell/startDebugger");
}
// If the debug server is NOT active, we need to synchronize state and start it.
if (!DebugContext.IsDebugServerActive)
{
_languageServer?.SendNotification("powerShell/startDebugger");
}

DebugContext.SetDebuggerStopped(debuggerStopEventArgs);
DebugContext.SetDebuggerStopped(debuggerStopEventArgs);

try
{
CurrentPowerShell.WaitForRemoteOutputIfNeeded();
PowerShellFrameType frameBase = CurrentFrame.FrameType & PowerShellFrameType.Remote;
PushPowerShellAndRunLoop(
CreateNestedPowerShell(CurrentRunspace),
frameBase | PowerShellFrameType.Debug | PowerShellFrameType.Nested | PowerShellFrameType.Repl);
CurrentPowerShell.ResumeRemoteOutputIfNeeded();
}
finally
{
DebugContext.SetDebuggerResumed();
}
try
{
CurrentPowerShell.WaitForRemoteOutputIfNeeded();
PowerShellFrameType frameBase = CurrentFrame.FrameType & PowerShellFrameType.Remote;
PushPowerShellAndRunLoop(
CreateNestedPowerShell(CurrentRunspace),
frameBase | PowerShellFrameType.Debug | PowerShellFrameType.Nested | PowerShellFrameType.Repl);
CurrentPowerShell.ResumeRemoteOutputIfNeeded();
}
finally
{
DebugContext.SetDebuggerResumed();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -109,8 +109,10 @@ internal CancellationScope(

public void Dispose()
{
// TODO: This is whack. It used to call `Cancel` on the cancellation source, but we
// shouldn't do that!
_cancellationSource.Dispose();
_cancellationStack.TryPop(out CancellationScope _);
_cancellationSource.Cancel();
}
}
}