Repository: dotnet/roslyn
File: src/LanguageServer/Protocol/Handler/ServerLifetime/InitializeHandler.cs (C#)

Code before:
        var capabilitiesProvider = context.GetRequiredLspService<ICapabilitiesProvider>();
        var serverCapabilities = capabilitiesProvider.GetCapabilities(clientCapabilities);

        // Record a telemetry event indicating what capabilities are being provided by the server.
        // Useful for figuring out if a particular session is opted into an LSP feature.
        Logger.Log(FunctionId.LSP_Initialize, KeyValueLogMessage.Create(m =>
        {
            m["serverKind"] = context.ServerKind.ToTelemetryString();
            m["capabilities"] = JsonSerializer.Serialize(serverCapabilities, ProtocolConversions.LspJsonSerializerOptions);
        }));

        return new InitializeResult
        {
            Capabilities = serverCapabilities,
        };
    }

    static async Task WaitForClientProcessExitAsync(int clientProcessId)
    {
        try
        {
            var clientProcessExitTask = new TaskCompletionSource<bool>();

            using var clientProcess = Process.GetProcessById(clientProcessId);
            clientProcess.EnableRaisingEvents = true;
            clientProcess.Exited += (sender, args) => clientProcessExitTask.SetResult(true);

            if (!clientProcess.HasExited)
            {
                // Wait for the client process to exit.
                await clientProcessExitTask.Task.ConfigureAwait(false);
            }
        }
        finally
        {
            // The process didn't exist, exited, or we ran into
            // issues checking whether the process had exited.
            Process.GetCurrentProcess().Kill();
        }
    }
}


Review comment (suggestion):
The `TaskCompletionSource` is completed from a `Process.Exited` event handler. Using `SetResult` can throw (e.g., if the event fires after the TCS has already been completed due to a race), and without `RunContinuationsAsynchronously` the `await` continuation can run inline on the event-raising thread. Consider constructing the TCS with `TaskCreationOptions.RunContinuationsAsynchronously` and using `TrySetResult` in the event handler.
```suggestion
            var clientProcessExitTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            using var clientProcess = Process.GetProcessById(clientProcessId);
            clientProcess.EnableRaisingEvents = true;
            clientProcess.Exited += (sender, args) => clientProcessExitTask.TrySetResult(true);
```

Code after:
﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Internal.Log;
using Roslyn.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.LanguageServer.Handler;

[Method(Methods.InitializeName)]
internal sealed class InitializeHandler() : ILspServiceRequestHandler<InitializeParams, InitializeResult>
{
    public bool MutatesSolutionState => true;
    public bool RequiresLSPSolution => false;

    public async Task<InitializeResult> HandleRequestAsync(InitializeParams request, RequestContext context, CancellationToken cancellationToken)
    {
        var clientCapabilitiesManager = context.GetRequiredLspService<IInitializeManager>();
        var clientCapabilities = request.Capabilities;
        clientCapabilitiesManager.SetInitializeParams(request);

        if (request.ProcessId is int clientProcessId && RoslynLanguageServer.TryRegisterClientProcessId(clientProcessId))
            context.Logger.LogInformation("Monitoring client process {clientProcessId} for exit", clientProcessId);

        var capabilitiesProvider = context.GetRequiredLspService<ICapabilitiesProvider>();
        var serverCapabilities = capabilitiesProvider.GetCapabilities(clientCapabilities);

        // Record a telemetry event indicating what capabilities are being provided by the server.
        // Useful for figuring out if a particular session is opted into an LSP feature.
        Logger.Log(FunctionId.LSP_Initialize, KeyValueLogMessage.Create(m =>
        {
            m["serverKind"] = context.ServerKind.ToTelemetryString();
            m["capabilities"] = JsonSerializer.Serialize(serverCapabilities, ProtocolConversions.LspJsonSerializerOptions);
        }));

        return new InitializeResult
        {
            Capabilities = serverCapabilities,
        };
    }
}