Skip to content

Commit fb58542

Browse files
committed
Ensure async bindings are released before calling callback
JAVA-3662
1 parent e52a276 commit fb58542

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

driver-core/src/main/com/mongodb/internal/async/client/OperationExecutorImpl.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,10 @@ public void onResult(final AsyncReadWriteBinding binding, final Throwable t) {
8686
operation.executeAsync(binding, new SingleResultCallback<T>() {
8787
@Override
8888
public void onResult(final T result, final Throwable t) {
89-
try {
90-
labelException(t, session);
91-
unpinServerAddressOnTransientTransactionError(session, t);
92-
errHandlingCallback.onResult(result, t);
93-
} finally {
94-
binding.release();
95-
}
89+
labelException(session, t);
90+
unpinServerAddressOnTransientTransactionError(session, t);
91+
binding.release();
92+
errHandlingCallback.onResult(result, t);
9693
}
9794
});
9895
}
@@ -132,13 +129,10 @@ public void onResult(final AsyncReadWriteBinding binding, final Throwable t) {
132129
operation.executeAsync(binding, new SingleResultCallback<T>() {
133130
@Override
134131
public void onResult(final T result, final Throwable t) {
135-
try {
136-
labelException(t, session);
137-
unpinServerAddressOnTransientTransactionError(session, t);
138-
errHandlingCallback.onResult(result, t);
139-
} finally {
140-
binding.release();
141-
}
132+
labelException(session, t);
133+
unpinServerAddressOnTransientTransactionError(session, t);
134+
binding.release();
135+
errHandlingCallback.onResult(result, t);
142136
}
143137
});
144138
}
@@ -149,7 +143,7 @@ public void onResult(final T result, final Throwable t) {
149143
});
150144
}
151145

152-
private void labelException(final Throwable t, final AsyncClientSession session) {
146+
private void labelException(@Nullable final AsyncClientSession session, @Nullable final Throwable t) {
153147
if (session != null && session.hasActiveTransaction()
154148
&& (t instanceof MongoSocketException || t instanceof MongoTimeoutException
155149
|| (t instanceof MongoQueryException && ((MongoQueryException) t).getErrorCode() == 91))
@@ -158,8 +152,9 @@ private void labelException(final Throwable t, final AsyncClientSession session)
158152
}
159153
}
160154

161-
private void unpinServerAddressOnTransientTransactionError(final @Nullable AsyncClientSession session, final Throwable throwable) {
162-
if (session != null && throwable != null && throwable instanceof MongoException
155+
private void unpinServerAddressOnTransientTransactionError(@Nullable final AsyncClientSession session,
156+
@Nullable final Throwable throwable) {
157+
if (session != null && throwable instanceof MongoException
163158
&& ((MongoException) throwable).hasErrorLabel(TRANSIENT_TRANSACTION_ERROR_LABEL)) {
164159
session.setPinnedServerAddress(null);
165160
}

driver-sync/src/test/functional/com/mongodb/client/AbstractUnifiedTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.List;
5959
import java.util.Map;
6060
import java.util.concurrent.TimeUnit;
61+
import java.util.stream.Collectors;
6162

6263
import static com.mongodb.ClusterFixture.getConnectionString;
6364
import static com.mongodb.ClusterFixture.getMultiMongosConnectionString;
@@ -414,11 +415,18 @@ public Void execute() {
414415
}
415416
} else if (operationName.equals("assertDifferentLsidOnLastTwoCommands")) {
416417
List<CommandEvent> events = lastTwoCommandEvents();
417-
assertNotEquals(((CommandStartedEvent) events.get(0)).getCommand().getDocument("lsid"),
418+
String eventsJson = commandListener.getCommandStartedEvents().stream()
419+
.map(e -> ((CommandStartedEvent) e).getCommand().toJson())
420+
.collect(Collectors.joining(", "));
421+
422+
assertNotEquals(eventsJson, ((CommandStartedEvent) events.get(0)).getCommand().getDocument("lsid"),
418423
((CommandStartedEvent) events.get(1)).getCommand().getDocument("lsid"));
419424
} else if (operationName.equals("assertSameLsidOnLastTwoCommands")) {
420425
List<CommandEvent> events = lastTwoCommandEvents();
421-
assertEquals(((CommandStartedEvent) events.get(0)).getCommand().getDocument("lsid"),
426+
String eventsJson = commandListener.getCommandStartedEvents().stream()
427+
.map(e -> ((CommandStartedEvent) e).getCommand().toJson())
428+
.collect(Collectors.joining(", "));
429+
assertEquals(eventsJson, ((CommandStartedEvent) events.get(0)).getCommand().getDocument("lsid"),
422430
((CommandStartedEvent) events.get(1)).getCommand().getDocument("lsid"));
423431
} else if (operationName.equals("assertSessionDirty")) {
424432
assertNotNull(clientSession);

0 commit comments

Comments
 (0)