Skip to content

Commit 8696b8e

Browse files
[Internal]: Fixes NullRef in TransportHandler when client is concurrently disposed. (#5062)
# Pull Request Template ## Description Fixes a race conditions where StoreProxy returned can be null when the client is getting disposed at the same time. NOTE: Testing this is not easy due to this ebing a race condition. I evaluated using the ExceptionLess tests (which is using MockTransportHandler) - but it was not sutainable - because the race condition to hit the NullRef is tiny. Given that throwing ObjectDisposedException happens in other places regularly in this code path I think it is acceptable to not add a unit tests trying to provoke this edge condition. ## Type of change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) - [] New feature (non-breaking change which adds functionality) - [] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [] This change requires a documentation update ## Closing issues To automatically close an issue: closes #IssueNumber
1 parent 93358ac commit 8696b8e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Microsoft.Azure.Cosmos/src/Handler/TransportHandler.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ internal async Task<ResponseMessage> ProcessMessageAsync(
108108
serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization;
109109

110110
IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest);
111+
if (storeProxy == null)
112+
{
113+
// storeProxy being null should indicate that there was a race condition and the Client was
114+
// disposed between getting teh DocumentClient and calling GetStoreProxy
115+
// in this case repeating this call will result in throwing an ObjectDisposedException
116+
storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest);
117+
118+
if (storeProxy == null)
119+
{
120+
throw new InvalidOperationException("StoreProxy cannot be null");
121+
}
122+
}
123+
111124
using (ITrace processMessageAsyncTrace = request.Trace.StartChild(
112125
name: $"{storeProxy.GetType().FullName} Transport Request",
113126
component: TraceComponent.Transport,

0 commit comments

Comments
 (0)