Skip to content

Commit

Permalink
(#36): Guard against KeyNotFound in dictionary
Browse files Browse the repository at this point in the history
This is done since the timer dispatcher may be invoked at the same time as
the message is recieved, and then there is a race condition where we might
end up with a KeyNotFoundException.
  • Loading branch information
par.dahlman committed Feb 10, 2016
1 parent 526b1b3 commit d49167c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/RawRabbit/Operations/Requester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Task<TResponse> RequestAsync<TRequest, TResponse>(TRequest message, Guid
_logger.LogWarning($"Unable to find request timer for {props.CorrelationId}.");
}
timer?.Dispose();
responseTcs.TrySetException(new TimeoutException($"The request timed out after {_requestTimeout.ToString("g")}."));
responseTcs.TrySetException(new TimeoutException($"The request '{props.CorrelationId}' timed out after {_requestTimeout.ToString("g")}."));
}, null, _requestTimeout, new TimeSpan(-1)));

consumer.Model.BasicPublish(
Expand Down Expand Up @@ -143,7 +143,16 @@ private IRawConsumer GetOrCreateConsumerForType<TResponse>(IConsumerConfiguratio
{
var tcs = tcsAsObj as TaskCompletionSource<TResponse>;
_logger.LogDebug($"Recived response with correlationId {args.BasicProperties.CorrelationId}.");
_requestTimerDictionary[args.BasicProperties.CorrelationId]?.Dispose();

Timer timer;
if (_requestTimerDictionary.TryRemove(args.BasicProperties.CorrelationId, out timer))
{
timer?.Dispose();
}
else
{
_logger.LogInformation($"Unable to find request timer for message {args.BasicProperties.CorrelationId}.");
}
_errorStrategy.OnResponseRecievedAsync(args, tcs);
if (tcs?.Task?.IsFaulted ?? true)
{
Expand Down

0 comments on commit d49167c

Please sign in to comment.