Skip to content

Commit c91ea1d

Browse files
rozzajyemin
authored andcommitted
Ensure async bindings are released before calling callback
JAVA-3662
1 parent 41a1bad commit c91ea1d

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,10 @@ public void onResult(final AsyncReadWriteBinding binding, final Throwable t) {
8888
operation.executeAsync(binding, new SingleResultCallback<T>() {
8989
@Override
9090
public void onResult(final T result, final Throwable t) {
91-
try {
92-
labelException(t, session);
93-
unpinServerAddressOnTransientTransactionError(session, t);
94-
errHandlingCallback.onResult(result, t);
95-
} finally {
96-
binding.release();
97-
}
91+
labelException(session, t);
92+
unpinServerAddressOnTransientTransactionError(session, t);
93+
binding.release();
94+
errHandlingCallback.onResult(result, t);
9895
}
9996
});
10097
}
@@ -134,13 +131,10 @@ public void onResult(final AsyncReadWriteBinding binding, final Throwable t) {
134131
operation.executeAsync(binding, new SingleResultCallback<T>() {
135132
@Override
136133
public void onResult(final T result, final Throwable t) {
137-
try {
138-
labelException(t, session);
139-
unpinServerAddressOnTransientTransactionError(session, t);
140-
errHandlingCallback.onResult(result, t);
141-
} finally {
142-
binding.release();
143-
}
134+
labelException(session, t);
135+
unpinServerAddressOnTransientTransactionError(session, t);
136+
binding.release();
137+
errHandlingCallback.onResult(result, t);
144138
}
145139
});
146140
}
@@ -151,7 +145,7 @@ public void onResult(final T result, final Throwable t) {
151145
});
152146
}
153147

154-
private void labelException(final Throwable t, final ClientSession session) {
148+
private void labelException(@Nullable final ClientSession session, @Nullable final Throwable t) {
155149
if (session != null && session.hasActiveTransaction()
156150
&& (t instanceof MongoSocketException || t instanceof MongoTimeoutException
157151
|| (t instanceof MongoQueryException && ((MongoQueryException) t).getErrorCode() == 91))
@@ -160,8 +154,9 @@ private void labelException(final Throwable t, final ClientSession session) {
160154
}
161155
}
162156

163-
private void unpinServerAddressOnTransientTransactionError(final @Nullable ClientSession session, final Throwable throwable) {
164-
if (session != null && throwable != null && throwable instanceof MongoException
157+
private void unpinServerAddressOnTransientTransactionError(@Nullable final ClientSession session,
158+
@Nullable final Throwable throwable) {
159+
if (session != null && throwable instanceof MongoException
165160
&& ((MongoException) throwable).hasErrorLabel(TRANSIENT_TRANSACTION_ERROR_LABEL)) {
166161
session.setPinnedServerAddress(null);
167162
}

0 commit comments

Comments
 (0)