Skip to content

Commit fe82d06

Browse files
committed
pr
1 parent 04e6c89 commit fe82d06

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

src/MongoDB.Driver/Core/ConnectionPools/ExclusiveConnectionPool.Helpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ private void AcquireWaitQueueSlot()
304304
private TimeSpan GetWaitQueueTimeout(OperationContext operationContext)
305305
{
306306
var waitQueueTimeout = _pool.Settings.WaitQueueTimeout;
307-
if (operationContext.RemainingTimeout != Timeout.InfiniteTimeSpan)
307+
if (operationContext.RemainingTimeout != Timeout.InfiniteTimeSpan && operationContext.RemainingTimeout < waitQueueTimeout)
308308
{
309-
waitQueueTimeout = operationContext.RemainingTimeout < _pool.Settings.WaitQueueTimeout ? operationContext.RemainingTimeout : _pool.Settings.WaitQueueTimeout;
309+
waitQueueTimeout = operationContext.RemainingTimeout;
310310
}
311311

312312
return waitQueueTimeout;

src/MongoDB.Driver/Core/Connections/BinaryConnection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ private void OpenHelper(OperationContext operationContext)
283283
}
284284
catch (OperationCanceledException) when (operationContext.IsTimedOut())
285285
{
286+
// OperationCanceledException could be thrown because of CombinedCancellationToken (see line 273),
287+
// if we face it and operation context is timed out we should throw TimeoutException instead.
286288
throw new TimeoutException();
287289
}
288290
catch (Exception ex)
@@ -314,6 +316,8 @@ private async Task OpenHelperAsync(OperationContext operationContext)
314316
}
315317
catch (OperationCanceledException) when (operationContext.IsTimedOut())
316318
{
319+
// OperationCanceledException could be thrown because of CombinedCancellationToken (see line 307),
320+
// if we face it and operation context is timed out we should throw TimeoutException instead.
317321
throw new TimeoutException();
318322
}
319323
catch (Exception ex)

src/MongoDB.Driver/Core/Misc/StreamExtensionMethods.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public static void ReadBytes(this Stream stream, OperationContext operationConte
118118

119119
while (count > 0)
120120
{
121-
var timeout = operationContext.Timeout == null ? socketTimeout : operationContext.RemainingTimeout;
122-
var bytesRead = stream.Read(buffer, offset, count, timeout, operationContext.CancellationToken);
121+
var bytesRead = stream.Read(buffer, offset, count, operationContext.RemainingTimeoutOrDefault(socketTimeout), operationContext.CancellationToken);
123122
if (bytesRead == 0)
124123
{
125124
throw new EndOfStreamException();
@@ -140,8 +139,7 @@ public static void ReadBytes(this Stream stream, OperationContext operationConte
140139
{
141140
var backingBytes = buffer.AccessBackingBytes(offset);
142141
var bytesToRead = Math.Min(count, backingBytes.Count);
143-
var timeout = operationContext.Timeout == null ? socketTimeout : operationContext.RemainingTimeout;
144-
var bytesRead = stream.Read(backingBytes.Array, backingBytes.Offset, bytesToRead, timeout, operationContext.CancellationToken);
142+
var bytesRead = stream.Read(backingBytes.Array, backingBytes.Offset, bytesToRead, operationContext.RemainingTimeoutOrDefault(socketTimeout), operationContext.CancellationToken);
145143
if (bytesRead == 0)
146144
{
147145
throw new EndOfStreamException();
@@ -160,8 +158,7 @@ public static async Task ReadBytesAsync(this Stream stream, OperationContext ope
160158

161159
while (count > 0)
162160
{
163-
var timeout = operationContext.Timeout == null ? socketTimeout : operationContext.RemainingTimeout;
164-
var bytesRead = await stream.ReadAsync(buffer, offset, count, timeout, operationContext.CancellationToken).ConfigureAwait(false);
161+
var bytesRead = await stream.ReadAsync(buffer, offset, count, operationContext.RemainingTimeoutOrDefault(socketTimeout), operationContext.CancellationToken).ConfigureAwait(false);
165162
if (bytesRead == 0)
166163
{
167164
throw new EndOfStreamException();
@@ -182,8 +179,7 @@ public static async Task ReadBytesAsync(this Stream stream, OperationContext ope
182179
{
183180
var backingBytes = buffer.AccessBackingBytes(offset);
184181
var bytesToRead = Math.Min(count, backingBytes.Count);
185-
var timeout = operationContext.Timeout == null ? socketTimeout : operationContext.RemainingTimeout;
186-
var bytesRead = await stream.ReadAsync(backingBytes.Array, backingBytes.Offset, bytesToRead, timeout, operationContext.CancellationToken).ConfigureAwait(false);
182+
var bytesRead = await stream.ReadAsync(backingBytes.Array, backingBytes.Offset, bytesToRead, operationContext.RemainingTimeoutOrDefault(socketTimeout), operationContext.CancellationToken).ConfigureAwait(false);
187183
if (bytesRead == 0)
188184
{
189185
throw new EndOfStreamException();
@@ -279,8 +275,7 @@ public static void WriteBytes(this Stream stream, OperationContext operationCont
279275
{
280276
var backingBytes = buffer.AccessBackingBytes(offset);
281277
var bytesToWrite = Math.Min(count, backingBytes.Count);
282-
var timeout = operationContext.Timeout == null ? socketTimeout : operationContext.RemainingTimeout;
283-
stream.Write(backingBytes.Array, backingBytes.Offset, bytesToWrite, timeout, operationContext.CancellationToken);
278+
stream.Write(backingBytes.Array, backingBytes.Offset, bytesToWrite, operationContext.RemainingTimeoutOrDefault(socketTimeout), operationContext.CancellationToken);
284279
offset += bytesToWrite;
285280
count -= bytesToWrite;
286281
}
@@ -297,8 +292,7 @@ public static async Task WriteBytesAsync(this Stream stream, OperationContext op
297292
{
298293
var backingBytes = buffer.AccessBackingBytes(offset);
299294
var bytesToWrite = Math.Min(count, backingBytes.Count);
300-
var timeout = operationContext.Timeout == null ? socketTimeout : operationContext.RemainingTimeout;
301-
await stream.WriteAsync(backingBytes.Array, backingBytes.Offset, bytesToWrite, timeout, operationContext.CancellationToken).ConfigureAwait(false);
295+
await stream.WriteAsync(backingBytes.Array, backingBytes.Offset, bytesToWrite, operationContext.RemainingTimeoutOrDefault(socketTimeout), operationContext.CancellationToken).ConfigureAwait(false);
302296
offset += bytesToWrite;
303297
count -= bytesToWrite;
304298
}

src/MongoDB.Driver/Core/WireProtocol/CommandUsingCommandMessageWireProtocol.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ private Type0CommandMessageSection<BsonDocument> CreateType0Section(OperationCon
381381
{
382382
serverTimeout -= _roundTripTime;
383383
// Server expects maxTimeMS as an integer, we should truncate it to give server a chance to reply with Timeout.
384+
// Do not want to use MaxTimeHelper here, because it has different logic (rounds up, allow zero value and throw ArgumentException on negative values instead of TimeoutException).
384385
var maxtimeMs = (int)serverTimeout.TotalMilliseconds;
385386
if (maxtimeMs <= 0)
386387
{

src/MongoDB.Driver/OperationContextExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
17+
1618
namespace MongoDB.Driver
1719
{
1820
internal static class OperationContextExtensions
1921
{
20-
public static bool IsRootContextTimeoutConfigured(this OperationContext operationContext)
21-
=> operationContext.RootContext.Timeout.HasValue;
22+
public static bool IsRootContextTimeoutConfigured(this OperationContext operationContext) =>
23+
operationContext.RootContext.Timeout.HasValue;
24+
25+
public static TimeSpan RemainingTimeoutOrDefault(this OperationContext operationContext, TimeSpan defaultValue) =>
26+
operationContext.Timeout == null ? defaultValue : operationContext.RemainingTimeout;
2227
}
2328
}

0 commit comments

Comments
 (0)