Skip to content

Commit db18a94

Browse files
Merge pull request #219 from OmniSharp/fix/cancellation
if cancellation was not found, place in a cancelled cts
2 parents d1d867f + 7bfe162 commit db18a94

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/JsonRpc/RequestRouterBase.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,22 @@ public virtual async Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Re
9292
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
9393

9494
var id = GetId(request.Id);
95-
var cts = new CancellationTokenSource();
95+
if (!_requests.TryGetValue(id, out var cts))
96+
{
97+
cts = new CancellationTokenSource();
98+
_requests.TryAdd(id, cts);
99+
}
96100
token.Register(cts.Cancel);
97-
_requests.TryAdd(id, cts);
98101

99102
// TODO: Try / catch for Internal Error
100103
try
101104
{
105+
if (cts.IsCancellationRequested)
106+
{
107+
_logger.LogDebug("Request {Id} was cancelled", id);
108+
return new RequestCancelled();
109+
}
110+
102111
// To avoid boxing, the best way to compare generics for equality is with EqualityComparer<T>.Default.
103112
// This respects IEquatable<T> (without boxing) as well as object.Equals, and handles all the Nullable<T> "lifted" nuances.
104113
// https://stackoverflow.com/a/864860
@@ -176,13 +185,17 @@ public virtual async Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Re
176185

177186
public void CancelRequest(object id)
178187
{
179-
if (_requests.TryGetValue(GetId(id), out var cts))
188+
var idValue = GetId(id);
189+
if (_requests.TryGetValue(idValue, out var cts))
180190
{
181191
cts.Cancel();
182192
}
183193
else
184194
{
185-
_logger.LogDebug("Request {Id} was not found to cancel", id);
195+
cts = new CancellationTokenSource();
196+
_requests.TryAdd(idValue, cts);
197+
cts.Cancel();
198+
_logger.LogDebug("Request {Id} was not found to cancel, stubbing it in.", id);
186199
}
187200
}
188201

0 commit comments

Comments
 (0)