ASIO signal_set 对多个 IO 线程不可靠,具体取决于代码顺序?

ASIO signal_set not reliable with multiple IO threads, depending on code order?

编辑: 我无法再重现这个问题。在不改变任何东西的情况下,signal_set 现在无论块的顺序如何都能可靠地工作。

我在一个程序中使用(独立的)ASIO,为了在 Ctrl+C 下正常关机,我使用了 signal_set。当只有我的主线程调用 io_context.run().

时一切正常

然后,我添加了一个选项来使用多个线程进行 IO。它看起来像这样:

// begin block 1
asio::signal_set signals(io_context, SIGINT, SIGTERM);
signals.async_wait([&server, &signals] (const asio::error_code& ec, int signal) {
    std::cerr << "Received signal " << signal << ", exiting" << std::endl;
    server.shutdown();
    signals.clear();
});
// end block 1

// begin block 2
std::vector<std::thread> io_threads;
if (num_io_threads > 1) {
    for (int i = 1; i < num_io_threads; ++i) {
        io_threads.emplace_back([&io_context] () {io_context.run();});
    }
}
// end block 2

io_context.run();

for (auto& thread: io_threads) {
    thread.join();
}

但是,当我 运行 和 num_io_threads > 1 并按 Ctrl+C 时,程序突然停止,而不是正常关闭。我认为这可能是因为额外的线程“偷走了”信号,因为我没有在这些线程中屏蔽任何信号。

然后我有预感,重新排序代码,将块 1 移动到块 2 下方,果然,正常关机再次可靠地工作。

我可以依赖这种行为吗? 具体来说,是不是因为我创建了 signal_set 并在创建所有内容后调用了它的 async_wait 方法线程,信号回调是可靠触发的,还是因为其他原因?如果是其他原因,可靠触发信号回调的正确解决方案是什么?

我试图找到相关文档,但没有找到。文档只说 programs must ensure that any signals registered using signal_set objects are unblocked in at least one thread.

一切都在带有 g++ 4.8.5 的 CentOS 7 上。

是的,你可以信赖它。

令我个人感到有点惊讶的是,您看到了按顺序(#1、#2)报告的块的效果。

我也复制不了:

Live On Coliru

#include <boost/asio.hpp>
#include <iostream>

namespace boost::asio {
    using boost::system::error_code; // huh - maybe this is a non-boost Asio thing
}
namespace asio = boost::asio;

template <typename Executor> struct Server {
    Server(Executor ex)
            : s(make_strand(ex)),
              timer(s, std::chrono::high_resolution_clock::time_point::max())
    {
        timer.async_wait([](asio::error_code ec) {
            std::cout << "Server shutdown (" << ec.message() << ")" << std::endl;
        });
    }
    void shutdown() {
        post(s, [this] { timer.cancel(); });
    };

  private:
    asio::strand<Executor> s;
    asio::high_resolution_timer timer;
};

int main(int argc, char**) {
    std::vector<std::thread> io_threads;
    boost::asio::io_context io_context;
    const int num_io_threads = 30;

    Server server(io_context.get_executor());

    auto start_threads = [&io_threads, &io_context] { //"block #2"
        // "block 2"
        if (auto n = num_io_threads - (io_threads.size() + 1); n > 0) {
            std::cerr << "Starting " << n << " threads...\n";
            while (n--)
                io_threads.emplace_back([&io_context] { io_context.run(); });
        }
    };

    if (argc > 1)
        start_threads();

    std::cerr << "Starting signal_set...\n";
    // begin block 1
    asio::signal_set signals(io_context, SIGINT, SIGTERM);
    signals.async_wait(
        [&server, &signals](const asio::error_code& ec, int signal) {
            std::cerr << "Received signal " << ::strsignal(signal) << ", " << ec.message() << std::endl;
            if (!ec)
            {
                std::cerr << "Exiting" << std::endl;
                server.shutdown();
                signals.clear();
            }
        });
    // end block 1

    start_threads();

    io_context.run();

    for (auto& thread : io_threads) {
        thread.join();
    }
}

以相同的“成功”运行两个订单:

./a.out        & sleep 1; kill -INT $!
./a.out order2 & sleep 1; kill -INT $!
Starting signal_set...
Starting 29 threads...
Received signal Interrupt, Success
Exiting
Server shutdown (Operation canceled)
bash: fork: retry: Resource temporarily unavailable
Starting 29 threads...
Starting signal_set...
bash: fork: retry: Resource temporarily unavailable
Received signal Interrupt, Success
Exiting
Server shutdown (Operation canceled)

一些想法:

  • signal_set 不是线程安全的,因此请确保不要同时访问它。
  • server.shutdown() 同样的想法。在我的复制中,我将 shutdown post 放在一条链上以避免比赛。
  • 我在信号处理程序中添加了对 ec 的检查
  • 你真的应该在 io 线程中处理异常:
  • 更简单,考虑使用asio::tread_pool (Coliru)

总结

如果你能用上面的代码重现,我怀疑信号集服务实现中有一个(依赖于平台?)错误,值得 reporting/asking Asio 开发人员。