Skip to content

Commit badb3e5

Browse files
committed
Fix false positive unexpected connection close
Motivation: The connection could be incorrectly detected as unexpectedly closed due to a race condition where the exit message was sent before updating the state to `ST_CLOSING`. Modifications: Ensured state update to `ST_CLOSING` happens before sending the exit message. Result: State update to `ST_CLOSING` happens before sending the exit message, preventing false positive unexpected close detections. resolves #275
1 parent a1c99ea commit badb3e5

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Diff for: r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/client/ReactorNettyClient.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ final class ReactorNettyClient implements Client {
131131
logger.debug("Request: {}", message);
132132
}
133133

134+
if (message == ExitMessage.INSTANCE) {
135+
if (STATE_UPDATER.compareAndSet(this, ST_CONNECTED, ST_CLOSING)) {
136+
logger.debug("Exit message sent");
137+
} else {
138+
logger.debug("Exit message sent (duplicated / connection already closed)");
139+
}
140+
}
141+
134142
if (message.isSequenceReset()) {
135143
resetSequence(connection);
136144
}
@@ -213,15 +221,8 @@ public Mono<Void> close() {
213221

214222
requestQueue.submit(RequestTask.wrap(sink, Mono.fromRunnable(() -> {
215223
Sinks.EmitResult result = requests.tryEmitNext(ExitMessage.INSTANCE);
216-
217224
if (result != Sinks.EmitResult.OK) {
218225
logger.error("Exit message sending failed due to {}, force closing", result);
219-
} else {
220-
if (STATE_UPDATER.compareAndSet(this, ST_CONNECTED, ST_CLOSING)) {
221-
logger.debug("Exit message sent");
222-
} else {
223-
logger.debug("Exit message sent (duplicated / connection already closed)");
224-
}
225226
}
226227
})));
227228
}).flatMap(Function.identity()).onErrorResume(e -> {

0 commit comments

Comments
 (0)