I have to handle SIGINT
and SIGTERM
in my program that uses Boost.Asio. I use boost::asio::signal_set::async_wait()
for this.
The problem is that the signal handler gets the control only when I simply run the app but does not when I debug it.
Here is some code:
Proxy::Proxy():
signals_(ioContext_, SIGINT, SIGTERM)
{
signals_.async_wait(
[this](const boost::system::error_code& error, int)
{
if (!error)
ioContext_.stop();
}
);
}
Proxy::run()
{
ioContext_.run();
}
When we run()
the Proxy
, ioContext_
starts processing events. If we just simply run the program and do Ctrl+C
in terminal, the signal handler (that is lambda) stops ioContext_
(as we expect) and io_context::run
gives the control back. But in debug mode the program reacts to Ctrl+C
but the execution stops somewhere in epoll_wait()
. If we continue the execution, it hangs on somewhere in epoll_wait()
and so on.
Here is a stack trace of where the execution stops:
epoll_wait
boost::asio::detail::epoll_reactor::run
boost::asio::detail::scheduler::do_one_run
boost::asio::detail::scheduler::run
boost::asio::io_context::run
Proxy::run
main
Why does it happen in debug mode but does not otherwise?