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

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

本文关键字:取决于 代码 顺序 不可靠 signal set 多个 线程 IO ASIO      更新时间:2023-10-16

编辑:我无法再重现此问题。无需更改任何内容,无论现在块的顺序如何,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方法,信号回调被可靠地触发,还是因为其他原因?如果是其他原因,可靠触发信号回调的正确解决方案是什么?

我试图找到相关文档,但找不到任何文档。文档只说程序必须确保使用signal_set对象注册的任何信号在至少一个线程中被解锁。

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

是的,你可以依靠它。

我个人有点惊讶你看到了你报告的块顺序(#1,#2(的效果。

我也无法重现它:

住在科里鲁

#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帖子以避免比赛。
  • 我在信号处理程序中添加了对ec的检查
  • 你真的应该在 io 线程中处理异常:https://stackoverflow.com/a/44500924/85371
  • 更简单的是,考虑使用asio::tread_pool(Coliru(

总结

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