Skip to content

Commit 113e676

Browse files
Martin LercherMartin Lercher
authored andcommitted
await server.WasShutDown;
1 parent 771ceff commit 113e676

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

sample/SampleServer/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ static async Task MainAsync(string[] args)
2929
server.AddHandler(new TextDocumentHandler(server));
3030

3131
await server.Initialize();
32-
33-
await new TaskCompletionSource<object>().Task;
32+
await server.WasShutDown;
3433
}
3534
}
3635

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Lsp
2+
{
3+
public interface IAwaitableTermination
4+
{
5+
System.Threading.Tasks.Task WasShutDown { get; }
6+
}
7+
}

src/Lsp/Handlers/ShutdownHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
using System;
12
using System.Threading.Tasks;
23
using Lsp.Protocol;
34

45
namespace Lsp.Handlers
56
{
6-
public class ShutdownHandler : IShutdownHandler
7+
public class ShutdownHandler : IShutdownHandler, IAwaitableTermination
78
{
89
public Task Handle()
910
{
1011
ShutdownRequested = true;
1112
Shutdown?.Invoke(ShutdownRequested);
13+
shutdownSource.SetResult(true); // after all event sinks were notified
1214
return Task.CompletedTask;
1315
}
1416

1517
public event ShutdownEventHandler Shutdown;
1618

1719
public bool ShutdownRequested { get; private set; }
20+
21+
private TaskCompletionSource<bool> shutdownSource = new TaskCompletionSource<bool>(TaskContinuationOptions.LongRunning);
22+
Task IAwaitableTermination.WasShutDown => shutdownSource.Task;
1823
}
1924
}

src/Lsp/LanguageServer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace Lsp
1818
{
19-
public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedHandler, IDisposable
19+
public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedHandler, IDisposable, IAwaitableTermination
2020
{
2121
private readonly Connection _connection;
2222
private readonly LspRequestRouter _requestRouter;
@@ -250,6 +250,8 @@ public TaskCompletionSource<JToken> GetRequest(long id)
250250
return _responseRouter.GetRequest(id);
251251
}
252252

253+
public Task WasShutDown => ((IAwaitableTermination)_shutdownHandler).WasShutDown;
254+
253255
public void Dispose()
254256
{
255257
_connection?.Dispose();

0 commit comments

Comments
 (0)