Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

Commit

Permalink
fork support by detecting pid in get_default_io_service
Browse files Browse the repository at this point in the history
  • Loading branch information
Cylix committed Nov 5, 2017
1 parent 2ac82e4 commit 926510f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sources/network/io_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,23 @@ namespace tacopie {

static std::shared_ptr<io_service> io_service_default_instance = nullptr;

#ifdef _WIN32
static DWORD current_pid = 0;
#define getpid GetCurrentProcessId
#else
static pid_t current_pid = 0;
#endif

const std::shared_ptr<io_service>&
get_default_io_service(std::uint32_t num_io_workers) {
if (io_service_default_instance == nullptr) {
//! pid needs to be checked too in case of a fork
//!
//! indeed, is default io service instance is created before fork
//! then, after the fork, both process will use the same instance
//! this will create issues when process will start to issue socket with the same fd number
if (io_service_default_instance == nullptr || getpid() != current_pid) {
io_service_default_instance = std::make_shared<io_service>(num_io_workers);
current_pid = getpid();
}
else {
io_service_default_instance->set_nb_workers(num_io_workers);
Expand Down

0 comments on commit 926510f

Please sign in to comment.