Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition where child leaks signal to parent #349

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions include/boost/asio/detail/impl/signal_set_service.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ struct signal_state
// The write end of the pipe used for signal notifications.
int write_descriptor_;

#if !defined(BOOST_ASIO_WINDOWS) \
&& !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
&& !defined(__CYGWIN__)
// Our own PID. We need it so we can detect if we had recently
// fork()'ed but not yet been notified.
pid_t pid_;
#endif

// Whether the signal state has been prepared for a fork.
bool fork_prepared_;

Expand All @@ -55,7 +63,13 @@ struct signal_state
signal_state* get_signal_state()
{
static signal_state state = {
BOOST_ASIO_STATIC_MUTEX_INIT, -1, -1, false, 0, { 0 } };
BOOST_ASIO_STATIC_MUTEX_INIT, -1, -1,
#if !defined(BOOST_ASIO_WINDOWS) \
&& !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
&& !defined(__CYGWIN__)
::getpid(),
#endif
false, 0, { 0 } };
return &state;
}

Expand All @@ -70,6 +84,18 @@ void boost_asio_signal_handler(int signal_number)
// || defined(__CYGWIN__)
int saved_errno = errno;
signal_state* state = get_signal_state();
pid_t pid = ::getpid();
if (state->pid_ != pid)
{
// There is a small window between ::fork() and notify_fork() where
// we need to make sure we don't accidently deliver that signal to
// the parent process. If this happened, we'll close the pipes
// and create new ones immediately. This is safe to do inside
// the signal handler.
state->pid_ = pid;
signal_set_service::close_descriptors();
signal_set_service::open_descriptors();
}
signed_size_type result = ::write(state->write_descriptor_,
&signal_number, sizeof(signal_number));
(void)result;
Expand Down Expand Up @@ -205,8 +231,17 @@ void signal_set_service::notify_fork(execution_context::fork_event fork_ev)
if (state->fork_prepared_)
{
boost::asio::detail::signal_blocker blocker;
close_descriptors();
open_descriptors();
pid_t pid = ::getpid();
if (state->pid_ != pid)
{
// There is a small chance this was already done by a signal
// that was delivered between ::fork() and notify_fork(). If
// so, we don't want to re-open the pipe as we would lose that
// signal.
state->pid_ = pid;
close_descriptors();
open_descriptors();
}
int read_descriptor = state->read_descriptor_;
state->fork_prepared_ = false;
lock.unlock();
Expand Down
2 changes: 2 additions & 0 deletions include/boost/asio/detail/signal_set_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ extern "C" BOOST_ASIO_DECL void boost_asio_signal_handler(int signal_number);
class signal_set_service :
public execution_context_service_base<signal_set_service>
{
friend void boost_asio_signal_handler(int signal_number);

public:
// Type used for tracking an individual signal registration.
class registration
Expand Down