From 95a23dc7ec9a2ce55cab337b78b662a30ec2b986 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 9 Aug 2013 17:20:40 -0400 Subject: Fix for another wonderful C++ threading race condition. --- node/EthernetTap.cpp | 4 ++++ node/Network.cpp | 4 ++++ node/Network.hpp | 1 + 3 files changed, 9 insertions(+) (limited to 'node') diff --git a/node/EthernetTap.cpp b/node/EthernetTap.cpp index 4c300d0b..e6536223 100644 --- a/node/EthernetTap.cpp +++ b/node/EthernetTap.cpp @@ -557,6 +557,10 @@ void EthernetTap::threadMain() char getBuf[4096 + 14]; Buffer<4096> data; + // Wait for a moment after startup -- wait for Network to finish + // constructing itself. + Thread::sleep(500); + FD_ZERO(&readfds); FD_ZERO(&nullfds); int nfds = (int)std::max(_shutdownSignalPipe[0],_fd) + 1; diff --git a/node/Network.cpp b/node/Network.cpp index 9227cd34..3593b732 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -139,6 +139,7 @@ SharedPtr Network::newInstance(const RuntimeEnvironment *renv,uint64_t // that then causes the Network instance to be deleted before it is finished // being constructed. C++ edge cases, how I love thee. SharedPtr nw(new Network()); + nw->_ready = false; // disable handling of Ethernet frames during construct nw->_r = renv; nw->_rlLimit.bytesPerSecond = ZT_MULTICAST_DEFAULT_BYTES_PER_SECOND; nw->_rlLimit.maxBalance = ZT_MULTICAST_DEFAULT_RATE_MAX_BALANCE; @@ -150,6 +151,7 @@ SharedPtr Network::newInstance(const RuntimeEnvironment *renv,uint64_t if (nw->controller() == renv->identity.address()) // sanity check, this isn't supported for now throw std::runtime_error("cannot add a network for which I am the netconf master"); nw->_restoreState(); + nw->_ready = true; // enable handling of Ethernet frames nw->requestConfiguration(); return nw; } @@ -267,6 +269,8 @@ Network::Status Network::status() const void Network::_CBhandleTapData(void *arg,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data) { + if (!((Network *)arg)->_ready) + return; const RuntimeEnvironment *_r = ((Network *)arg)->_r; try { _r->sw->onLocalEthernet(SharedPtr((Network *)arg),from,to,etherType,data); diff --git a/node/Network.hpp b/node/Network.hpp index 312912db..e80d2e65 100644 --- a/node/Network.hpp +++ b/node/Network.hpp @@ -472,6 +472,7 @@ private: uint64_t _id; volatile uint64_t _lastConfigUpdate; volatile bool _destroyOnDelete; + volatile bool _ready; Mutex _lock; -- cgit v1.2.3