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

Posted

技术标签:

【中文标题】ASIO signal_set 对于多个 IO 线程不可靠,取决于代码顺序?【英文标题】:ASIO signal_set not reliable with multiple IO threads, depending on code order? 【发布时间】:2020-06-22 10:29:43 【问题描述】:

编辑:我无法再重现此问题。在不改变任何东西的情况下,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 &gt; 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】:

是的,您可以信赖它。

我个人对您看到按顺序(#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() 也有同样的想法。在我的 repro 中,我将shutdown 发布在一条线上以避免比赛。 我在信号处理程序中添加了对 ec 的检查 你真的应该在 io 线程中处理异常:https://***.com/a/44500924/85371 更简单,考虑使用asio::tread_pool (Coliru)

总结

如果您可以使用上述代码重现,我怀疑信号集服务实现中存在(平台相关?)错误,值得报告/询问 Asio 开发人员。

【讨论】:

所以你是说顺序不应该重要,并且 signal_set 应该在任一顺序中可靠地工作? 我没有改变任何东西,但现在 signal_set 可以工作,无论两个块的顺序如何。很奇怪。

以上是关于ASIO signal_set 对于多个 IO 线程不可靠,取决于代码顺序?的主要内容,如果未能解决你的问题,请参考以下文章

boost:asio信号量signal_set源码分析及使用

对于带有单个接受器的线程 boost::asio 服务器,我们是不是需要每个线程多个 io_service

从多个线程调用 boost::asio::io_service 运行函数

多个 ASIO io_services 是好事吗?

boost::asio 在线程中启动不同的服务?

boost::asio io_service 停止特定线程