~afrantzis/mir/reproduce-1444047

« back to all changes in this revision

Viewing changes to src/common/dispatch/simple_dispatch_thread.cpp

  • Committer: Tarmac
  • Author(s): Christopher James Halse Rogers, Christopher James Halse Rogers
  • Date: 2015-04-15 05:07:08 UTC
  • mfrom: (2465.4.4 master)
  • Revision ID: tarmac-20150415050708-bq08i0c0n00cn6km
Introduce trivial RAII helper for signal blocking.

Use it to close a tiny race in SimpleDispatchThread - there was a time between spawning the thread and blocking signals on it during which a signal could theoretically be caught.
.

Approved by PS Jenkins bot, Alexandros Frantzis, Cemil Azizoglu, Alan Griffiths, Kevin DuBois, Robert Carr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include "mir/dispatch/dispatchable.h"
21
21
#include "mir/logging/logger.h"
22
22
#include "utils.h"
 
23
#include "mir/signal_blocker.h"
23
24
 
24
25
#include <sys/epoll.h>
25
26
#include <unistd.h>
115
116
    }
116
117
    shutdown_fd = mir::Fd{pipefds[1]};
117
118
    mir::Fd const terminate_fd = mir::Fd{pipefds[0]};
118
 
    eventloop = std::thread{[dispatchee, terminate_fd]()
119
 
                            {
120
 
                                // Our IO threads must not receive any signals
121
 
                                sigset_t all_signals;
122
 
                                sigfillset(&all_signals);
123
 
 
124
 
                                if (auto error = pthread_sigmask(SIG_BLOCK, &all_signals, NULL))
125
 
                                    BOOST_THROW_EXCEPTION((
126
 
                                                std::system_error{error,
127
 
                                                                  std::system_category(),
128
 
                                                                  "Failed to block signals on IO thread"}));
129
 
 
130
 
                                wait_for_events_forever(dispatchee, terminate_fd);
131
 
                            }};
 
119
    {
 
120
        // The newly spawned thread inherits the current signal mask; block everything
 
121
        // before creating the new thread so that there's no race between thread start
 
122
        // and signal blocking.
 
123
        mir::SignalBlocker block_signals;
 
124
        eventloop = std::thread{&wait_for_events_forever, dispatchee, terminate_fd};
 
125
    }
132
126
}
133
127
 
134
128
md::SimpleDispatchThread::~SimpleDispatchThread() noexcept