c – 在调用stop()和析构函数后仍然提供Poco HTTPServer连接
作者:互联网
我使用Poco :: HTTPServer时遇到问题.正如在TCPServer的文档中所描述的那样:
After calling stop(), no new connections will be accepted and all
queued connections will be discarded. Already served connections,
however, will continue being served.
每个连接都在自己的线程中执行.
虽然似乎析构函数被成功地称为连接线程仍然存在并且提供连接,这导致分段错误.
我想取消所有连接.因此我使用Poco :: ThreadPool :: defaultPool().stopAll();在我的服务器类的析构函数中,这导致了ThreadPool文档中描述的行为(它需要10秒而对象不会被删除):
If a thread fails to stop within 10 seconds (due to a programming
error, for example), the underlying thread object will not be deleted
and this method will return anyway. This allows for a more or less
graceful shutdown in case of a misbehaving thread.
我的问题是:我如何实现更优雅的方式? Poco库中是否存在编程错误?
编辑:我使用GNU / Linux(Ubuntu 10.04)和eclipse cdt作为IDE,目标系统是嵌入式Linux(Kernel 2.6.9).在两个系统上,我都经历了所描述的行为
我正在处理的应用程序应通过Web界面进行配置.因此服务器发送一个事件(在上传新配置时)到main重启.
这是大纲:
main{
while (true){
server = new Server(...);
server->start();
// wait for termination request
server->stop();
delete server;
}
}
class Server{
Poco:HTTPServer m_Server;
Server(...):
m_Server(requestHandlerFactory, socket, params);
{
}
~Server(){
[...]
Poco::ThreadPool::defaultPool().stopAll(); // This takes 10 seconds!
// without the above line I get segmentation faults,
// because connections are still being served.
}
start() { m_Server.start(); }
stop() { m_Server.stop(); }
}
解决方法:
这实际上是stopAll()方法实现中的一个错误.关闭当前活动连接后,侦听套接字正在关闭,这允许服务器接受其间的新连接,而这些连接又不会关闭并继续运行.解决方法是调用HTTPServer :: stop(),然后调用HTTPServer :: stopAll().我报告了上游的bug,包括一个建议的修复:
https://github.com/pocoproject/poco/issues/436
标签:c-2,linux,multithreading,poco-libraries 来源: https://codeday.me/bug/20190530/1187023.html