Skip to content

Fix test deadlocks #2159

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
Apr 15, 2024
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
18 changes: 9 additions & 9 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ dotnet_diagnostic.CA1068.severity = error
# CA1501: Avoid excessive inheritance
dotnet_diagnostic.CA1501.severity = error
# CA1502: Avoid excessive complexity
dotnet_diagnostic.CA1502.severity = warning
dotnet_diagnostic.CA1502.severity = silent
# CA1505: Avoid unmaintainable code
dotnet_diagnostic.CA1505.severity = error
# CA1506: Avoid excessive class coupling
dotnet_diagnostic.CA1506.severity = warning
dotnet_diagnostic.CA1506.severity = silent
# CA1507: Use nameof in place of string
dotnet_diagnostic.CA1507.severity = error
# CA1508: Avoid dead conditional code
Expand Down Expand Up @@ -95,22 +95,22 @@ dotnet_diagnostic.RCS1210.severity = error
# RCS1036: Remove unnecessary blank line
dotnet_diagnostic.RCS1036.severity = error
# RCS1075: Avoid empty catch clause that catches System.Exception
dotnet_diagnostic.RCS1075.severity = suggestion
dotnet_diagnostic.RCS1075.severity = error
# RCS1170: Use read-only auto-implemented property
dotnet_diagnostic.RCS1170.severity = error

# VSTHRD002: Avoid problematic synchronous waits
dotnet_diagnostic.VSTHRD002.severity = suggestion
dotnet_diagnostic.VSTHRD002.severity = error
# VSTHRD003: Avoid awaiting foreign Tasks
dotnet_diagnostic.VSTHRD003.severity = suggestion
dotnet_diagnostic.VSTHRD003.severity = error
# VSTHRD105: Avoid method overloads that assume TaskScheduler.Current
dotnet_diagnostic.VSTHRD105.severity = suggestion
dotnet_diagnostic.VSTHRD105.severity = error
# VSTHRD100: Avoid async void methods
dotnet_diagnostic.VSTHRD100.severity = suggestion
dotnet_diagnostic.VSTHRD100.severity = error
# VSTHRD103: Call async methods when in an async method
dotnet_diagnostic.VSTHRD103.severity = suggestion
dotnet_diagnostic.VSTHRD103.severity = error
# VSTHRD110: Observe result of async calls
dotnet_diagnostic.VSTHRD110.severity = suggestion
dotnet_diagnostic.VSTHRD110.severity = error
# VSTHRD114: Avoid returning a null Task
dotnet_diagnostic.VSTHRD114.severity = error
# VSTHRD200: Use "Async" suffix for awaitable methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ protected override void BeginProcessing()
}
#pragma warning restore IDE0022

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Uses ThrowTerminatingError() instead")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "We have to wait here, it's the whole program.")]
protected override void EndProcessing()
{
_logger.Log(PsesLogLevel.Diagnostic, "Beginning EndProcessing block");
Expand All @@ -232,9 +232,7 @@ protected override void EndProcessing()
using EditorServicesLoader psesLoader = EditorServicesLoader.Create(_logger, editorServicesConfig, SessionDetailsPath, _loggerUnsubscribers);
_logger.Log(PsesLogLevel.Verbose, "Loading EditorServices");
// Synchronously start editor services and wait here until it shuts down.
#pragma warning disable VSTHRD002
psesLoader.LoadAndRunEditorServicesAsync().GetAwaiter().GetResult();
#pragma warning restore VSTHRD002
}
catch (Exception e)
{
Expand Down
5 changes: 2 additions & 3 deletions src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,18 @@ private void ValidateConfiguration()
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1825:Avoid zero-length array allocations", Justification = "Cannot use Array.Empty, since it must work in net452")]
private static Version GetPSVersion()
{
// In order to read the $PSVersionTable variable,
// we are forced to create a new runspace to avoid concurrency issues,
// which is expensive.
// Rather than do that, we instead go straight to the source,
// which is a static property, internal in WinPS and public in PS 6+
#pragma warning disable CA1825
return typeof(PSObject).Assembly
.GetType("System.Management.Automation.PSVersionInfo")
.GetMethod("get_PSVersion", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Invoke(null, new object[0] /* Cannot use Array.Empty, since it must work in net452 */) as Version;
#pragma warning restore CA1825
.Invoke(null, new object[0]) as Version;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ private async Task RunTempDebugSessionAsync(HostStartupInfo hostDetails)
await debugServer.WaitForShutdown().ConfigureAwait(false);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks", Justification = "It's a wrapper.")]
private async Task StartDebugServer(Task<PsesDebugServer> debugServerCreation)
{
PsesDebugServer debugServer = await debugServerCreation.ConfigureAwait(false);
Expand Down Expand Up @@ -304,6 +305,7 @@ private void WriteStartupBanner()
_config.PSHost.UI.WriteLine(_config.StartupBanner);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "Intentionally fire and forget.")]
private void DebugServer_OnSessionEnded(object sender, EventArgs args)
{
_logger.Log(PsesLogLevel.Verbose, "Debug session ended, restarting debug service...");
Expand Down
3 changes: 1 addition & 2 deletions src/PowerShellEditorServices/Extensions/EditorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ public void SetSelection(
/// Sets a selection in the host editor's active buffer.
/// </summary>
/// <param name="selectionRange">The range of the selection.</param>
#pragma warning disable VSTHRD002
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void SetSelection(FileRange selectionRange) => editorOperations.SetSelectionAsync(selectionRange.ToBufferRange()).Wait();
#pragma warning restore VSTHRD002

#endregion
}
Expand Down
5 changes: 2 additions & 3 deletions src/PowerShellEditorServices/Extensions/EditorObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,12 @@ internal EditorObject(
/// at the time this method is invoked.
/// </summary>
/// <returns>A instance of the EditorContext class.</returns>
#pragma warning disable VSTHRD002, VSTHRD104
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public EditorContext GetEditorContext() => _editorOperations.GetEditorContextAsync().Result;
#pragma warning restore VSTHRD002, VSTHRD104

internal void SetAsStaticInstance()
{
EditorObject.Instance = this;
Instance = this;
s_editorObjectReady.TrySetResult(true);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/PowerShellEditorServices/Extensions/EditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,36 @@ internal EditorWindow(IEditorOperations editorOperations)
/// Shows an informational message to the user.
/// </summary>
/// <param name="message">The message to be shown.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void ShowInformationMessage(string message) => editorOperations.ShowInformationMessageAsync(message).Wait();

/// <summary>
/// Shows an error message to the user.
/// </summary>
/// <param name="message">The message to be shown.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void ShowErrorMessage(string message) => editorOperations.ShowErrorMessageAsync(message).Wait();

/// <summary>
/// Shows a warning message to the user.
/// </summary>
/// <param name="message">The message to be shown.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void ShowWarningMessage(string message) => editorOperations.ShowWarningMessageAsync(message).Wait();

/// <summary>
/// Sets the status bar message in the editor UI (if applicable).
/// </summary>
/// <param name="message">The message to be shown.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void SetStatusBarMessage(string message) => editorOperations.SetStatusBarMessageAsync(message, null).Wait();

/// <summary>
/// Sets the status bar message in the editor UI (if applicable).
/// </summary>
/// <param name="message">The message to be shown.</param>
/// <param name="timeout">A timeout in milliseconds for how long the message should remain visible.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void SetStatusBarMessage(string message, int timeout) => editorOperations.SetStatusBarMessageAsync(message, timeout).Wait();

#endregion
Expand Down
7 changes: 7 additions & 0 deletions src/PowerShellEditorServices/Extensions/EditorWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ public sealed class EditorWorkspace
/// <summary>
/// Creates a new file in the editor.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void NewFile() => editorOperations.NewFileAsync(string.Empty).Wait();

/// <summary>
/// Creates a new file in the editor.
/// </summary>
/// <param name="content">The content to place in the new file.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void NewFile(string content) => editorOperations.NewFileAsync(content).Wait();

/// <summary>
/// Opens a file in the workspace. If the file is already open
/// its buffer will be made active.
/// </summary>
/// <param name="filePath">The path to the file to be opened.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void OpenFile(string filePath) => editorOperations.OpenFileAsync(filePath).Wait();

/// <summary>
Expand All @@ -64,25 +67,29 @@ public sealed class EditorWorkspace
/// </summary>
/// <param name="filePath">The path to the file to be opened.</param>
/// <param name="preview">Determines wether the file is opened as a preview or as a durable editor.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void OpenFile(string filePath, bool preview) => editorOperations.OpenFileAsync(filePath, preview).Wait();

/// <summary>
/// Closes a file in the workspace.
/// </summary>
/// <param name="filePath">The path to the file to be closed.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void CloseFile(string filePath) => editorOperations.CloseFileAsync(filePath).Wait();

/// <summary>
/// Saves an open file in the workspace.
/// </summary>
/// <param name="filePath">The path to the file to be saved.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void SaveFile(string filePath) => editorOperations.SaveFileAsync(filePath).Wait();

/// <summary>
/// Saves a file with a new name AKA a copy.
/// </summary>
/// <param name="oldFilePath">The file to copy.</param>
/// <param name="newFilePath">The file to create.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void SaveFile(string oldFilePath, string newFilePath) => editorOperations.SaveFileAsync(oldFilePath, newFilePath).Wait();

#endregion
Expand Down
7 changes: 3 additions & 4 deletions src/PowerShellEditorServices/Extensions/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,13 @@ public void InsertText(
/// </summary>
/// <param name="textToInsert">The text string to insert.</param>
/// <param name="insertRange">The buffer range which will be replaced by the string.</param>
#pragma warning disable VSTHRD002
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void InsertText(string textToInsert, IFileRange insertRange)
{
editorOperations
.InsertTextAsync(scriptFile.DocumentUri.ToString(), textToInsert, insertRange.ToBufferRange())
.Wait();
}
#pragma warning restore VSTHRD002

#endregion

Expand All @@ -224,6 +223,7 @@ public void InsertText(string textToInsert, IFileRange insertRange)
/// <summary>
/// Saves this file.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "Supporting synchronous API.")]
public void Save() => editorOperations.SaveFileAsync(scriptFile.FilePath);

/// <summary>
Expand All @@ -233,7 +233,7 @@ public void InsertText(string textToInsert, IFileRange insertRange)
/// the path where the file should be saved,
/// including the file name with extension as the leaf
/// </param>
#pragma warning disable VSTHRD002
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
public void SaveAs(string newFilePath)
{
// Do some validation here so that we can provide a helpful error if the path won't work
Expand All @@ -248,7 +248,6 @@ public void SaveAs(string newFilePath)

editorOperations.SaveFileAsync(scriptFile.FilePath, newFilePath).Wait();
}
#pragma warning restore VSTHRD002

#endregion
}
Expand Down
9 changes: 0 additions & 9 deletions src/PowerShellEditorServices/GlobalSuppressions.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/PowerShellEditorServices/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma warning disable IDE0073
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#if NET5_0_OR_GREATER
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.IsExternalInit))]
#else
Expand Down
2 changes: 0 additions & 2 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public PsesLanguageServer(
/// cref="PsesServiceCollectionExtensions.AddPsesLanguageServices"/>.
/// </remarks>
/// <returns>A task that completes when the server is ready and listening.</returns>
#pragma warning disable CA1506 // Coupling complexity we don't care about
public async Task StartAsync()
#pragma warning restore CA1506
{
LanguageServer = await OmniSharp.Extensions.LanguageServer.Server.LanguageServer.From(options =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Microsoft.PowerShell.EditorServices.Server
{
internal static class PsesServiceCollectionExtensions
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "Using lazy initialization.")]
public static IServiceCollection AddPsesLanguageServices(
this IServiceCollection collection,
HostStartupInfo hostStartupInfo)
Expand Down Expand Up @@ -48,9 +49,7 @@ public static IServiceCollection AddPsesLanguageServices(
// is ready, it will be available. NOTE: We cannot await this because it
// uses a lazy initialization to avoid a race with the dependency injection
// framework, see the EditorObject class for that!
#pragma warning disable VSTHRD110
extensionService.InitializeAsync();
#pragma warning restore VSTHRD110
return extensionService;
})
.AddSingleton<AnalysisService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ private static string TrimScriptListingLine(PSObject scriptLineObj, ref int pref
/// </summary>
public event EventHandler<DebuggerStoppedEventArgs> DebuggerStopped;

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "It has to be async.")]
internal async void OnDebuggerStopAsync(object sender, DebuggerStopEventArgs e)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ public async Task OnStarted(IDebugAdapterServer server, CancellationToken cancel
// be sent to the client.
await _debugStateService.ServerStarted.Task.ConfigureAwait(false);

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks", Justification = "It's a wrapper.")]
private async Task OnExecutionCompletedAsync(Task executeTask)
{
bool isRunspaceClosed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public LegacyReadLine(
_onIdleAction = onIdleAction;
}

#pragma warning disable CA1502 // Cyclomatic complexity we don't care about
public override string ReadLine(CancellationToken cancellationToken)
#pragma warning restore CA1502
{
string inputBeforeCompletion = null;
string inputAfterCompletion = null;
Expand Down Expand Up @@ -394,6 +392,7 @@ protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken)
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "This is a legacy implementation.")]
private ConsoleKeyInfo ReadKeyWithIdleSupport(CancellationToken cancellationToken)
{
// We run the readkey function on another thread so we can run an idle handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
using Microsoft.PowerShell.EditorServices.Server;
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;

#pragma warning disable CA1506 // Coupling complexity we don't care about
internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRunspaceContext, IInternalPowerShellExecutionService
#pragma warning restore CA1506
{
internal const string DefaultPrompt = "> ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public DidChangeWatchedFilesHandler(
public DidChangeWatchedFilesRegistrationOptions GetRegistrationOptions(
DidChangeWatchedFilesCapability capability,
ClientCapabilities clientCapabilities)
#pragma warning disable CS8601 // Possible null reference assignment (it's from the library).
=> new()
{
Watchers = new[]
Expand All @@ -52,6 +53,7 @@ public DidChangeWatchedFilesRegistrationOptions GetRegistrationOptions(
},
},
};
#pragma warning restore CS8601 // Possible null reference assignment.

public Task<Unit> Handle(DidChangeWatchedFilesParams request, CancellationToken cancellationToken)
{
Expand Down
Loading