Elffuss BraidHub

kikocis/java-driver public

Prevent cancel and timeout from cancelling unrelated ResponseHandler if streamId was already released and reused (JAVA-632).
Changeset9a38148beb2bc26fa58399dce41cf3709763082c50f826530fb16d886cd4f6e8
Authorolim7t <omichallat+github@gmail.com> · 2015-01-27 19:56 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-01-30 15:41 +01:00
Parentsd9bbc2be11c7

Browse the files at this changeset

2 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -5,6 +5,8 @@
55 --------
66
77 - [bug] Prevent race between cancellation and query completion (JAVA-614)
8+- [bug] Prevent cancel and timeout from cancelling unrelated ResponseHandler if
9+ streamId was already released and reused (JAVA-632).
810
911
1012 2.0.9:
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +19 −11
@@ -364,12 +364,12 @@
364364 * never leave a handler that won't get an answer or be errored out.
365365 */
366366 if (isDefunct) {
367- dispatcher.removeHandler(handler.streamId, true);
367+ dispatcher.removeHandler(handler, true);
368368 throw new ConnectionException(address, "Write attempt on defunct connection");
369369 }
370370
371371 if (isClosed()) {
372- dispatcher.removeHandler(handler.streamId, true);
372+ dispatcher.removeHandler(handler, true);
373373 throw new ConnectionException(address, "Connection has been closed");
374374 }
375375
@@ -390,7 +390,7 @@
390390 logger.debug("{} Error writing request {}", Connection.this, request);
391391 // Remove this handler from the dispatcher so it don't get notified of the error
392392 // twice (we will fail that method already)
393- dispatcher.removeHandler(handler.streamId, true);
393+ dispatcher.removeHandler(handler, true);
394394
395395 final ConnectionException ce;
396396 if (writeFuture.getCause() instanceof java.nio.channels.ClosedChannelException) {
@@ -623,19 +623,27 @@
623623 assert old == null;
624624 }
625625
626- public void removeHandler(int streamId, boolean releaseStreamId) {
626+ public void removeHandler(ResponseHandler handler, boolean releaseStreamId) {
627627
628628 // If we don't release the ID, mark first so that we can rely later on the fact that if
629629 // we receive a response for an ID with no handler, it's that this ID has been marked.
630630 if (!releaseStreamId)
631- streamIdHandler.mark(streamId);
632-
633- ResponseHandler handler = pending.remove(streamId);
634- if (handler != null)
635- handler.cancelTimeout();
631+ streamIdHandler.mark(handler.streamId);
632+
633+ // If a RequestHandler is cancelled right when the response arrives, this method (called with releaseStreamId=false) will race with messageReceived.
634+ // messageReceived could have already released the streamId, which could have already been reused by another request. We must not remove the handler
635+ // if it's not ours, because that would cause the other request to hang forever.
636+ boolean removed = pending.remove(handler.streamId, handler);
637+ if (!removed) {
638+ // We raced, so if we marked the streamId above, that was wrong.
639+ if (!releaseStreamId)
640+ streamIdHandler.unmark(handler.streamId);
641+ return;
642+ }
643+ handler.cancelTimeout();
636644
637645 if (releaseStreamId)
638- streamIdHandler.release(streamId);
646+ streamIdHandler.release(handler.streamId);
639647
640648 if (isClosed())
641649 tryTerminate(false);
@@ -864,7 +872,7 @@
864872 // request and there is no point in holding the handler, but we don't release the streamId. If we
865873 // were, a new request could reuse that ID but get the answer to the request we just gave up on instead
866874 // of its own answer, and we would have no way to detect that.
867- connection.dispatcher.removeHandler(streamId, false);
875+ connection.dispatcher.removeHandler(this, false);
868876 if (connection instanceof PooledConnection)
869877 ((PooledConnection)connection).release();
870878 }