Skip to content

Commit

Permalink
fix: Fix deadlock on mutex protecting thread_ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Geer committed Jan 24, 2017
1 parent c94b392 commit f42d3e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions shttps/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,25 +1065,28 @@ namespace shttps {
old_ll = setlogmask(LOG_MASK(LOG_INFO));
syslog(LOG_INFO, "Server shutting down");
setlogmask(old_ll);
std::vector<pthread_t> threads_to_join;

{
std::lock_guard<std::mutex> thread_mutex_guard(threadlock);

// Send the close message to all running threads.
for (auto const &tid : thread_ids) {
threads_to_join.push_back(tid.first);
Server::CommMsg::send(tid.second.commpipe_write);
}
}

close(stoppipe[0]);
close(stoppipe[1]);
close(stoppipe[0]);
close(stoppipe[1]);

// We have closed all sockets, now wait for the threads to terminate.
for (auto const &tid : thread_ids) {
int err;
// We have closed all sockets, now wait for the threads to terminate.

if ((err = pthread_join(tid.first, nullptr)) != 0) {
syslog(LOG_ERR, "pthread_join failed with error code %d", err);
}
for (auto const &thread_to_join : threads_to_join) {
int err = pthread_join(thread_to_join, nullptr);

if (err != 0) {
syslog(LOG_ERR, "pthread_join failed with error code %d", err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion shttps/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ namespace shttps {
std::string semname; //!< name of the semaphore for restricting the number of threads
sem_t *_semaphore; //!< semaphore
std::atomic<int> _semcnt; //!< current value of semaphore (sem_getvalue() is not available on all systems)
std::map<pthread_t,GenericSockId> thread_ids; //!< Map of active worker threads
std::map<pthread_t, GenericSockId> thread_ids; //!< Map of active worker threads
int _keep_alive_timeout;
bool running; //!< Main runloop should keep on going
std::map<std::string, RequestHandler> handler[9]; // request handlers for the different 9 request methods
Expand Down

0 comments on commit f42d3e3

Please sign in to comment.