Skip to content
Snippets Groups Projects
Commit 84d322e4 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

peer discovery: check running before select

parent d807af39
No related branches found
No related tags found
No related merge requests found
...@@ -301,17 +301,22 @@ PeerDiscovery::DomainPeerDiscovery::listenerpack_thread() ...@@ -301,17 +301,22 @@ PeerDiscovery::DomainPeerDiscovery::listenerpack_thread()
stop_writefd_ = stopfds_pipe[1]; stop_writefd_ = stopfds_pipe[1];
while (true) { while (true) {
{
std::lock_guard<std::mutex> lck(dmtx_);
if (not drunning_)
break;
}
fd_set readfds; fd_set readfds;
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(stop_readfd, &readfds); FD_SET(stop_readfd, &readfds);
FD_SET(sockfd_, &readfds); FD_SET(sockfd_, &readfds);
int data_coming = select(std::max(sockfd_, stop_readfd) + 1, &readfds, nullptr, nullptr, nullptr); int data_coming = select(std::max(sockfd_, stop_readfd) + 1, &readfds, nullptr, nullptr, nullptr);
{ {
std::unique_lock<std::mutex> lck(dmtx_); std::lock_guard<std::mutex> lck(dmtx_);
if (not drunning_) if (not drunning_)
break; break;
} }
...@@ -437,12 +442,14 @@ PeerDiscovery::DomainPeerDiscovery::stopDiscovery() ...@@ -437,12 +442,14 @@ PeerDiscovery::DomainPeerDiscovery::stopDiscovery()
#ifdef _WIN32 #ifdef _WIN32
#define write(s, b, f) send((s), (b), (f), 0) #define write(s, b, f) send((s), (b), (f), 0)
#endif #endif
if (drunning_) {
drunning_ = false; drunning_ = false;
if (stop_writefd_ != -1) { if (stop_writefd_ != -1) {
if (write(stop_writefd_, "\0", 1) == -1) { if (write(stop_writefd_, "\0", 1) == -1) {
std::cerr << "Can't send stop message: " << strerror(errno) << std::endl; std::cerr << "Can't send stop message: " << strerror(errno) << std::endl;
} }
} }
}
#ifdef _WIN32 #ifdef _WIN32
#undef write #undef write
#endif #endif
...@@ -451,9 +458,11 @@ PeerDiscovery::DomainPeerDiscovery::stopDiscovery() ...@@ -451,9 +458,11 @@ PeerDiscovery::DomainPeerDiscovery::stopDiscovery()
void void
PeerDiscovery::DomainPeerDiscovery::stopPublish() PeerDiscovery::DomainPeerDiscovery::stopPublish()
{ {
if (lrunning_) {
lrunning_ = false; lrunning_ = false;
cv_.notify_all(); cv_.notify_all();
} }
}
void void
PeerDiscovery::DomainPeerDiscovery::stop() PeerDiscovery::DomainPeerDiscovery::stop()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment