From c6a9c223be9265f72b042dfcf41aad7006e9f9ba Mon Sep 17 00:00:00 2001 From: MikhailArkhipov Date: Mon, 30 Sep 2019 12:03:05 -0700 Subject: [PATCH 1/4] Remove stale reference --- .../Impl/Microsoft.Python.LanguageServer.csproj | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/LanguageServer/Impl/Microsoft.Python.LanguageServer.csproj b/src/LanguageServer/Impl/Microsoft.Python.LanguageServer.csproj index ca455737f..a33882e0d 100644 --- a/src/LanguageServer/Impl/Microsoft.Python.LanguageServer.csproj +++ b/src/LanguageServer/Impl/Microsoft.Python.LanguageServer.csproj @@ -33,14 +33,6 @@ - - - $(AnalysisReference)/Microsoft.Python.Analysis.Engine.dll - - - PreserveNewest - - From 0d94d040045149fd0d0a8f109f8d39efc474c538 Mon Sep 17 00:00:00 2001 From: MikhailArkhipov Date: Thu, 10 Oct 2019 12:37:32 -0700 Subject: [PATCH 2/4] Restructure async --- .../Indexing/MostRecentDocumentSymbols.cs | 51 ++++++++----------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs index fe8b6969b..192fe65fb 100644 --- a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs +++ b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs @@ -18,26 +18,22 @@ class MostRecentDocumentSymbols : IMostRecentDocumentSymbols { private CancellationTokenSource _fileCts = new CancellationTokenSource(); private TaskCompletionSource> _fileTcs = new TaskCompletionSource>(); - private WorkQueueState state = WorkQueueState.WaitingForWork; + private WorkQueueState _state = WorkQueueState.WaitingForWork; public MostRecentDocumentSymbols(string path, IIndexParser indexParser) { _path = path; _indexParser = indexParser; } - public void Parse() { - WorkAndSetTcs(ParseAsync); - } + public void Parse() => WorkAndSetTcs(ParseAsync).DoNotWait(); - public void Index(IDocument doc) { - WorkAndSetTcs(ct => IndexAsync(doc, ct)); - } + public void Index(IDocument doc) => WorkAndSetTcs(ct => IndexAsync(doc, ct)).DoNotWait(); - public void WorkAndSetTcs(Func>> asyncWork) { + public async Task WorkAndSetTcs(Func>> asyncWork) { CancellationTokenSource currentCts; TaskCompletionSource> currentTcs; lock (_syncObj) { - switch (state) { + switch (_state) { case WorkQueueState.Working: CancelExistingWork(); RenewTcs(); @@ -50,40 +46,33 @@ public void WorkAndSetTcs(Func> DoWork(CancellationTokenSource tcs, Func>> asyncWork) { - var token = tcs.Token; - try { - return await asyncWork(token); + var result = await asyncWork(currentCts.Token); + currentTcs.TrySetResult(result); + } catch(OperationCanceledException) { + currentTcs.TrySetCanceled(); + } catch(Exception ex) { + currentTcs.TrySetException(ex); } finally { lock (_syncObj) { - tcs.Dispose(); - if (!token.IsCancellationRequested) { - state = WorkQueueState.FinishedWork; + if (!currentCts.Token.IsCancellationRequested) { + _state = WorkQueueState.FinishedWork; } + currentCts.Dispose(); } } } - public Task> GetSymbolsAsync(CancellationToken ct = default) { - TaskCompletionSource> currentTcs; - lock (_syncObj) { - currentTcs = _fileTcs; - } - return currentTcs.Task.ContinueWith(t => t.GetAwaiter().GetResult(), ct); - } + public Task> GetSymbolsAsync(CancellationToken ct = default) => _fileTcs.Task; public void MarkAsPending() { lock (_syncObj) { - switch (state) { + switch (_state) { case WorkQueueState.WaitingForWork: break; case WorkQueueState.Working: @@ -96,13 +85,13 @@ public void MarkAsPending() { default: throw new InvalidOperationException(); } - state = WorkQueueState.WaitingForWork; + _state = WorkQueueState.WaitingForWork; } } public void Dispose() { lock (_syncObj) { - switch (state) { + switch (_state) { case WorkQueueState.Working: CancelExistingWork(); break; @@ -116,7 +105,7 @@ public void Dispose() { default: throw new InvalidOperationException(); } - state = WorkQueueState.FinishedWork; + _state = WorkQueueState.FinishedWork; } } From 43f01b60536a38f5e46930d1e336eae62f119c7d Mon Sep 17 00:00:00 2001 From: MikhailArkhipov Date: Thu, 10 Oct 2019 13:09:13 -0700 Subject: [PATCH 3/4] Add lock --- .../Impl/Indexing/MostRecentDocumentSymbols.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs index 192fe65fb..a01aa34d2 100644 --- a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs +++ b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs @@ -68,7 +68,11 @@ public async Task WorkAndSetTcs(Func> GetSymbolsAsync(CancellationToken ct = default) => _fileTcs.Task; + public Task> GetSymbolsAsync(CancellationToken ct = default) { + lock (_syncObj) { + return _fileTcs.Task; + } + } public void MarkAsPending() { lock (_syncObj) { From c815ab85902a49567a3f1488aa8fd58e0eff094f Mon Sep 17 00:00:00 2001 From: MikhailArkhipov Date: Thu, 10 Oct 2019 13:42:42 -0700 Subject: [PATCH 4/4] formatting --- .../Impl/Indexing/MostRecentDocumentSymbols.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs index a01aa34d2..e1739af6e 100644 --- a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs +++ b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs @@ -10,7 +10,7 @@ using Microsoft.Python.Parsing.Ast; namespace Microsoft.Python.LanguageServer.Indexing { - class MostRecentDocumentSymbols : IMostRecentDocumentSymbols { + internal sealed class MostRecentDocumentSymbols : IMostRecentDocumentSymbols { private readonly object _syncObj = new object(); private readonly IIndexParser _indexParser; private readonly string _path; @@ -54,9 +54,9 @@ public async Task WorkAndSetTcs(Func