Skip to content

Commit 711a4c9

Browse files
stIncMalejoykim1005
authored andcommitted
Connections must not be closed when timeoutMS expires before sending a request (#1573)
JAVA-5715
1 parent 1379c97 commit 711a4c9

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

Diff for: driver-core/src/main/com/mongodb/internal/TimeoutContext.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Optional;
3030
import java.util.function.LongConsumer;
3131

32+
import static com.mongodb.assertions.Assertions.assertNotNull;
3233
import static com.mongodb.assertions.Assertions.assertNull;
3334
import static com.mongodb.assertions.Assertions.isTrue;
3435
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
@@ -198,7 +199,7 @@ public void runMaxTimeMS(final LongConsumer onRemaining) {
198199
runWithFixedTimeout(timeoutSettings.getMaxTimeMS(), onRemaining);
199200
return;
200201
}
201-
timeout.shortenBy(minRoundTripTimeMS, MILLISECONDS)
202+
assertNotNull(timeoutIncludingRoundTrip())
202203
.run(MILLISECONDS,
203204
() -> {},
204205
onRemaining,

Diff for: driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,19 @@ private <T> void sendAndReceiveAsyncInternal(final CommandMessage message, final
582582
private <T> void sendCommandMessageAsync(final int messageId, final Decoder<T> decoder, final OperationContext operationContext,
583583
final SingleResultCallback<T> callback, final ByteBufferBsonOutput bsonOutput,
584584
final CommandEventSender commandEventSender, final boolean responseExpected) {
585-
List<ByteBuf> byteBuffers = bsonOutput.getByteBuffers();
586-
587585
boolean[] shouldReturn = {false};
588586
Timeout.onExistsAndExpired(operationContext.getTimeoutContext().timeoutIncludingRoundTrip(), () -> {
589-
callback.onResult(null, createMongoOperationTimeoutExceptionAndClose(commandEventSender));
587+
bsonOutput.close();
588+
MongoOperationTimeoutException operationTimeoutException = TimeoutContext.createMongoRoundTripTimeoutException();
589+
commandEventSender.sendFailedEvent(operationTimeoutException);
590+
callback.onResult(null, operationTimeoutException);
590591
shouldReturn[0] = true;
591592
});
592593
if (shouldReturn[0]) {
593594
return;
594595
}
595596

597+
List<ByteBuf> byteBuffers = bsonOutput.getByteBuffers();
596598
sendMessageAsync(byteBuffers, messageId, operationContext, (result, t) -> {
597599
ResourceUtil.release(byteBuffers);
598600
bsonOutput.close();
@@ -638,13 +640,6 @@ private <T> void sendCommandMessageAsync(final int messageId, final Decoder<T> d
638640
});
639641
}
640642

641-
private MongoOperationTimeoutException createMongoOperationTimeoutExceptionAndClose(final CommandEventSender commandEventSender) {
642-
MongoOperationTimeoutException e = TimeoutContext.createMongoRoundTripTimeoutException();
643-
close();
644-
commandEventSender.sendFailedEvent(e);
645-
return e;
646-
}
647-
648643
private <T> T getCommandResult(final Decoder<T> decoder,
649644
final ResponseBuffers responseBuffers,
650645
final int messageId,

0 commit comments

Comments
 (0)