From 7ac5aa48ed6e3940eff1b0f84a9ba4f22e971f0e Mon Sep 17 00:00:00 2001 From: Lukas Joswiak Date: Tue, 8 Dec 2020 17:38:32 +0000 Subject: [PATCH] Fix UDP simulation bug with duplicate ports Occasionally, a second UDP socket would be created with a randomly generated port equal to the randomly generated port of an earlier socket. --- fdbrpc/sim2.actor.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fdbrpc/sim2.actor.cpp b/fdbrpc/sim2.actor.cpp index 1dd1202a12..84bbd15c7b 100644 --- a/fdbrpc/sim2.actor.cpp +++ b/fdbrpc/sim2.actor.cpp @@ -1796,6 +1796,7 @@ public: : id(deterministicRandom()->randomUniqueID()), process(g_simulator.getCurrentProcess()), peerAddress(peerAddress), actors(false), _localAddress(localAddress) { g_sim2.addressMap.emplace(_localAddress, process); + ASSERT(process->boundUDPSockets.find(localAddress) == process->boundUDPSockets.end()); process->boundUDPSockets.emplace(localAddress, this); } ~UDPSimSocket() { @@ -1909,6 +1910,9 @@ Future> Sim2::createUDPSocket(NetworkAddress toAddr) { localAddress.ip = IPAddress(process->address.ip.toV4() + deterministicRandom()->randomInt(0, 256)); } localAddress.port = deterministicRandom()->randomInt(40000, 60000); + while (process->boundUDPSockets.find(localAddress) != process->boundUDPSockets.end()) { + localAddress.port = deterministicRandom()->randomInt(40000, 60000); + } return Reference(new UDPSimSocket(localAddress, toAddr)); }