gpt4 book ai didi

c++ - Poco HTTPServer 连接在调用 stop() 和析构函数后仍然有效

转载 作者:IT王子 更新时间:2023-10-29 01:11:08 29 4
gpt4 key购买 nike

我在使用 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-library的编程错误?

编辑:我使用 GNU/Linux (Ubuntu 10.04) 和 eclipse + cdt 作为 IDE,目标系统是嵌入式 Linux(内核 2.6.9)。在这两个系统上,我都遇到了所描述的行为。

我正在处理的应用程序应通过网络界面进行配置。所以服务器发送一个事件(在上传新配置时)到 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()。我在上游报告了错误,包括建议的修复:

https://github.com/pocoproject/poco/issues/436

关于c++ - Poco HTTPServer 连接在调用 stop() 和析构函数后仍然有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6340753/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com