Skip to content

Fix lock up that occurs when WinForms is executed on the pipeline thread #1149

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 5 commits into from
Jan 9, 2020
Merged
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
15 changes: 10 additions & 5 deletions src/PowerShellEditorServices/Server/PsesDebugServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -21,7 +22,10 @@ namespace Microsoft.PowerShell.EditorServices.Server
/// </summary>
internal class PsesDebugServer : IDisposable
{
private static bool s_hasRunPsrlStaticCtor = false;
/// <summary>
/// This is a bool but must be an int, since Interlocked.Exchange can't handle a bool
/// </summary>
private static int s_hasRunPsrlStaticCtor = 0;
Copy link
Member

Choose a reason for hiding this comment

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


private readonly Stream _inputStream;
private readonly Stream _outputStream;
Expand Down Expand Up @@ -64,20 +68,21 @@ public async Task StartAsync()
options.Serializer = new DapProtocolSerializer();
options.Reciever = new DapReciever();
options.LoggerFactory = _loggerFactory;
Extensions.Logging.ILogger logger = options.LoggerFactory.CreateLogger("DebugOptionsStartup");
ILogger logger = options.LoggerFactory.CreateLogger("DebugOptionsStartup");

// We need to let the PowerShell Context Service know that we are in a debug session
// so that it doesn't send the powerShell/startDebugger message.
_powerShellContextService = ServiceProvider.GetService<PowerShellContextService>();
_powerShellContextService.IsDebugServerActive = true;

// Needed to make sure PSReadLine's static properties are initialized in the pipeline thread.
if (!s_hasRunPsrlStaticCtor && _usePSReadLine)
if (_usePSReadLine && Interlocked.Exchange(ref s_hasRunPsrlStaticCtor, 1) == 0)
{
s_hasRunPsrlStaticCtor = true;
// This must be run synchronously to ensure debugging works
_powerShellContextService
.ExecuteScriptStringAsync("[System.Runtime.CompilerServices.RuntimeHelpers]::RunClassConstructor([Microsoft.PowerShell.PSConsoleReadLine].TypeHandle)")
.Wait();
.GetAwaiter()
.GetResult();
}

options.Services = new ServiceCollection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2356,18 +2356,18 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
PowerShellExecutionResult.Stopped,
null));

// Get the session details and push the current
// runspace if the session has changed
SessionDetails sessionDetails = null;
try
{
sessionDetails = this.GetSessionDetailsInDebugger();
}
catch (InvalidOperationException)
{
this.logger.LogTrace(
"Attempting to get session details failed, most likely due to a running pipeline that is attempting to stop.");
}
// Get the session details and push the current
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cosmetic change to fix misleading indentation

// runspace if the session has changed
SessionDetails sessionDetails = null;
try
{
sessionDetails = this.GetSessionDetailsInDebugger();
}
catch (InvalidOperationException)
{
this.logger.LogTrace(
"Attempting to get session details failed, most likely due to a running pipeline that is attempting to stop.");
}

if (!localThreadController.FrameExitTask.Task.IsCompleted)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ internal async Task<IEnumerable<TResult>> RequestPipelineExecutionAsync<TResult>
/// A task object representing the asynchronous operation. The Result property will return
/// the retrieved pipeline execution request.
/// </returns>
internal async Task<IPipelineExecutionRequest> TakeExecutionRequestAsync()
internal Task<IPipelineExecutionRequest> TakeExecutionRequestAsync()
{
return await PipelineRequestQueue.DequeueAsync();
return PipelineRequestQueue.DequeueAsync();
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/PowerShellEditorServices/Utility/AsyncQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public async Task<T> DequeueAsync(CancellationToken cancellationToken)
{
Task<T> requestTask;

using (await queueLock.LockAsync(cancellationToken))
using (await queueLock.LockAsync(cancellationToken).ConfigureAwait(false))
{
if (this.itemQueue.Count > 0)
{
Expand All @@ -171,7 +171,7 @@ public async Task<T> DequeueAsync(CancellationToken cancellationToken)
}

// Wait for the request task to complete outside of the lock
return await requestTask;
return await requestTask.ConfigureAwait(false);
}

/// <summary>
Expand Down