Elffuss BraidHub

kikocis/java-driver public

Avoid deadlock when multiple connections to the same host get write errors (JAVA-557).
Changeset5816920af92f1424598ac02ab2892cd30baa77d7daf55e741014f2792ec6ef95
Authorolim7t <omichallat+github@gmail.com> · 2014-12-07 12:07 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-12-09 18:30 +01:00
Parents6068c037a5f5

Browse the files at this changeset

2 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -9,6 +9,8 @@
99 - [bug] Don't check rpc_address for control host (JAVA-546)
1010 - [improvement] Improve message of NoHostAvailableException (JAVA-409)
1111 - [bug] Rework connection reaper to avoid deadlock (JAVA-556)
12+- [bug] Avoid deadlock when multiple connections to the same host get write
13+ errors (JAVA-557)
1214
1315
1416 2.0.8:
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +13 −2
@@ -380,13 +380,24 @@
380380 // twice (we will fail that method already)
381381 dispatcher.removeHandler(handler.streamId, true);
382382
383- ConnectionException ce;
383+ final ConnectionException ce;
384384 if (writeFuture.getCause() instanceof java.nio.channels.ClosedChannelException) {
385385 ce = new TransportException(address, "Error writing: Closed channel");
386386 } else {
387387 ce = new TransportException(address, "Error writing", writeFuture.getCause());
388388 }
389- handler.callback.onException(Connection.this, defunct(ce), System.nanoTime() - handler.startTime, handler.retryCount);
389+ final long latency = System.nanoTime() - handler.startTime;
390+ // This handler is executed while holding the writeLock of the channel.
391+ // defunct might close the pool, which will close all of its connections; closing a connection also
392+ // requires its writeLock.
393+ // Therefore if multiple connections in the same pool get a write error, they could deadlock;
394+ // we run defunct on a separate thread to avoid that.
395+ factory.manager.executor.execute(new Runnable() {
396+ @Override
397+ public void run() {
398+ handler.callback.onException(Connection.this, defunct(ce), latency, handler.retryCount);
399+ }
400+ });
390401 } else {
391402 logger.trace("{} request sent successfully", Connection.this);
392403 }