From c3fa4831c5ac6b415346fb47f2619e03b2b400a9 Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Thu, 5 May 2016 10:05:17 +0200 Subject: [PATCH] Avoid race condition on timer creation in ``RequestDataHandler.requestData()`` If the timer is created after sending the message and establishing callbacks, they may have already run on timer creation so it would be pointless (and issue a warning when triggered). --- .../p2p/peers/getdata/RequestDataHandler.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataHandler.java b/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataHandler.java index 7410d2e4b3..95d7ba448e 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataHandler.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataHandler.java @@ -90,6 +90,21 @@ public class RequestDataHandler implements MessageListener { log.info("We send a {} to peer {}. ", getDataRequest.getClass().getSimpleName(), nodeAddress); + if (timeoutTimer == null) { // setup before sending to avoid race conditions + timeoutTimer = UserThread.runAfter(() -> { + if (!stopped) { + String errorMessage = "A timeout occurred at sending getDataRequest:" + getDataRequest + + " on nodeAddress:" + nodeAddress; + log.info(errorMessage + " / RequestDataHandler=" + RequestDataHandler.this); + handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_TIMEOUT); + } else { + log.trace("We have stopped already. We ignore that timeoutTimer.run call. " + + "Might be caused by an previous networkNode.sendMessage.onFailure."); + } + }, + TIME_OUT_SEC); + } + SettableFuture future = networkNode.sendMessage(nodeAddress, getDataRequest); Futures.addCallback(future, new FutureCallback() { @Override @@ -119,21 +134,6 @@ public class RequestDataHandler implements MessageListener { } } }); - - if (timeoutTimer == null) { - timeoutTimer = UserThread.runAfter(() -> { - if (!stopped) { - String errorMessage = "A timeout occurred at sending getDataRequest:" + getDataRequest + - " on nodeAddress:" + nodeAddress; - log.info(errorMessage + " / RequestDataHandler=" + RequestDataHandler.this); - handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_TIMEOUT); - } else { - log.trace("We have stopped already. We ignore that timeoutTimer.run call. " + - "Might be caused by an previous networkNode.sendMessage.onFailure."); - } - }, - TIME_OUT_SEC); - } } else { log.warn("We have stopped already. We ignore that requestData call."); }