From 4e95b6e6cbd7f09c25dbba5d0f20c669b1b790f6 Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Thu, 5 May 2016 09:13:34 +0200 Subject: [PATCH] Avoid race condition on timer creation in ``GetDataRequestHanler.handle()`` 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). --- .../peers/getdata/GetDataRequestHandler.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/peers/getdata/GetDataRequestHandler.java b/network/src/main/java/io/bitsquare/p2p/peers/getdata/GetDataRequestHandler.java index 8f62f2a605..bb9aebdd8a 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/getdata/GetDataRequestHandler.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/getdata/GetDataRequestHandler.java @@ -66,6 +66,16 @@ public class GetDataRequestHandler { Log.traceCall(getDataRequest + "\n\tconnection=" + connection); GetDataResponse getDataResponse = new GetDataResponse(new HashSet<>(dataStorage.getMap().values()), getDataRequest.getNonce()); + + if (timeoutTimer == null) { // setup before sending to avoid race conditions + timeoutTimer = UserThread.runAfter(() -> { + String errorMessage = "A timeout occurred for getDataResponse:" + getDataResponse + + " on connection:" + connection; + handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection); + }, + TIME_OUT_SEC, TimeUnit.SECONDS); + } + SettableFuture future = networkNode.sendMessage(connection, getDataResponse); Futures.addCallback(future, new FutureCallback() { @Override @@ -84,15 +94,6 @@ public class GetDataRequestHandler { handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, connection); } }); - - if (timeoutTimer == null) { - timeoutTimer = UserThread.runAfter(() -> { - String errorMessage = "A timeout occurred for getDataResponse:" + getDataResponse + - " on connection:" + connection; - handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection); - }, - TIME_OUT_SEC, TimeUnit.SECONDS); - } } ///////////////////////////////////////////////////////////////////////////////////////////