mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-31 10:49:19 -04:00
Avoid race condition on timer creation in `PeerExchangeHandler.sendGetPeersRequest()
`
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).
This commit is contained in:
parent
4e95b6e6cb
commit
704b985121
1 changed files with 16 additions and 15 deletions
|
@ -84,6 +84,22 @@ class PeerExchangeHandler implements MessageListener {
|
||||||
if (!stopped) {
|
if (!stopped) {
|
||||||
if (networkNode.getNodeAddress() != null) {
|
if (networkNode.getNodeAddress() != null) {
|
||||||
GetPeersRequest getPeersRequest = new GetPeersRequest(networkNode.getNodeAddress(), nonce, peerManager.getConnectedNonSeedNodeReportedPeers(nodeAddress));
|
GetPeersRequest getPeersRequest = new GetPeersRequest(networkNode.getNodeAddress(), nonce, peerManager.getConnectedNonSeedNodeReportedPeers(nodeAddress));
|
||||||
|
|
||||||
|
if (timeoutTimer == null) { // setup before sending to avoid race conditions
|
||||||
|
timeoutTimer = UserThread.runAfter(() -> {
|
||||||
|
if (!stopped) {
|
||||||
|
String errorMessage = "A timeout occurred at sending getPeersRequest:" + getPeersRequest + " for nodeAddress:" + nodeAddress;
|
||||||
|
log.info(errorMessage + " / PeerExchangeHandler=" +
|
||||||
|
PeerExchangeHandler.this);
|
||||||
|
log.info("timeoutTimer called on " + this);
|
||||||
|
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, nodeAddress);
|
||||||
|
} else {
|
||||||
|
log.trace("We have stopped that handler already. We ignore that timeoutTimer.run call.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
TIME_OUT_SEC, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getPeersRequest);
|
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getPeersRequest);
|
||||||
Futures.addCallback(future, new FutureCallback<Connection>() {
|
Futures.addCallback(future, new FutureCallback<Connection>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -116,21 +132,6 @@ class PeerExchangeHandler implements MessageListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (timeoutTimer == null) {
|
|
||||||
timeoutTimer = UserThread.runAfter(() -> {
|
|
||||||
if (!stopped) {
|
|
||||||
String errorMessage = "A timeout occurred at sending getPeersRequest:" + getPeersRequest + " for nodeAddress:" + nodeAddress;
|
|
||||||
log.info(errorMessage + " / PeerExchangeHandler=" +
|
|
||||||
PeerExchangeHandler.this);
|
|
||||||
log.info("timeoutTimer called on " + this);
|
|
||||||
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, nodeAddress);
|
|
||||||
} else {
|
|
||||||
log.trace("We have stopped that handler already. We ignore that timeoutTimer.run call.");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TIME_OUT_SEC, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
log.debug("My node address is still null at sendGetPeersRequest. We ignore that call.");
|
log.debug("My node address is still null at sendGetPeersRequest. We ignore that call.");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue