kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 9a38148beb2bc26fa58399dce41cf3709763082c50f826530fb16d886cd4f6e8 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-01-27 19:56 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-01-30 15:41 +01:00 |
| Parents | d9bbc2be11c7 |
2 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -5,6 +5,8 @@ | ||
| 5 | 5 | -------- |
| 6 | 6 | |
| 7 | 7 | - [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). | |
| 8 | 10 | |
| 9 | 11 | |
| 10 | 12 | 2.0.9: |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +19 −11
| @@ -364,12 +364,12 @@ | ||
| 364 | 364 | * never leave a handler that won't get an answer or be errored out. |
| 365 | 365 | */ |
| 366 | 366 | if (isDefunct) { |
| 367 | - dispatcher.removeHandler(handler.streamId, true); | |
| 367 | + dispatcher.removeHandler(handler, true); | |
| 368 | 368 | throw new ConnectionException(address, "Write attempt on defunct connection"); |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | if (isClosed()) { |
| 372 | - dispatcher.removeHandler(handler.streamId, true); | |
| 372 | + dispatcher.removeHandler(handler, true); | |
| 373 | 373 | throw new ConnectionException(address, "Connection has been closed"); |
| 374 | 374 | } |
| 375 | 375 | |
| @@ -390,7 +390,7 @@ | ||
| 390 | 390 | logger.debug("{} Error writing request {}", Connection.this, request); |
| 391 | 391 | // Remove this handler from the dispatcher so it don't get notified of the error |
| 392 | 392 | // twice (we will fail that method already) |
| 393 | - dispatcher.removeHandler(handler.streamId, true); | |
| 393 | + dispatcher.removeHandler(handler, true); | |
| 394 | 394 | |
| 395 | 395 | final ConnectionException ce; |
| 396 | 396 | if (writeFuture.getCause() instanceof java.nio.channels.ClosedChannelException) { |
| @@ -623,19 +623,27 @@ | ||
| 623 | 623 | assert old == null; |
| 624 | 624 | } |
| 625 | 625 | |
| 626 | - public void removeHandler(int streamId, boolean releaseStreamId) { | |
| 626 | + public void removeHandler(ResponseHandler handler, boolean releaseStreamId) { | |
| 627 | 627 | |
| 628 | 628 | // If we don't release the ID, mark first so that we can rely later on the fact that if |
| 629 | 629 | // we receive a response for an ID with no handler, it's that this ID has been marked. |
| 630 | 630 | 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(); | |
| 636 | 644 | |
| 637 | 645 | if (releaseStreamId) |
| 638 | - streamIdHandler.release(streamId); | |
| 646 | + streamIdHandler.release(handler.streamId); | |
| 639 | 647 | |
| 640 | 648 | if (isClosed()) |
| 641 | 649 | tryTerminate(false); |
| @@ -864,7 +872,7 @@ | ||
| 864 | 872 | // request and there is no point in holding the handler, but we don't release the streamId. If we |
| 865 | 873 | // were, a new request could reuse that ID but get the answer to the request we just gave up on instead |
| 866 | 874 | // 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); | |
| 868 | 876 | if (connection instanceof PooledConnection) |
| 869 | 877 | ((PooledConnection)connection).release(); |
| 870 | 878 | } |