Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpServer启动失败直接触发abort #2185

Open
TYUTthelastone opened this issue Oct 14, 2024 · 1 comment
Open

HttpServer启动失败直接触发abort #2185

TYUTthelastone opened this issue Oct 14, 2024 · 1 comment

Comments

@TYUTthelastone
Copy link
Contributor

HttpServer启动的时候,启用SSL的线程是app().run()的线程,enableSSL触发的runtime_error被HttpServer所属的loop捕获了,导致assertInLoopThread()断言了。

void ListenerManager::createListeners(
const std::string &globalCertFile,
const std::string &globalKeyFile,
const std::vector<std::pair<std::string, std::string>> &sslConfCmds,
const std::vector<trantor::EventLoop *> &ioLoops)
{
LOG_TRACE << "thread num=" << ioLoops.size();
#ifdef __linux__
for (size_t i = 0; i < ioLoops.size(); ++i)
{
for (auto const &listener : listeners_)
{
auto const &ip = listener.ip_;
bool isIpv6 = (ip.find(':') != std::string::npos);
InetAddress listenAddress(ip, listener.port_, isIpv6);
if (listenAddress.isUnspecified())
{
LOG_FATAL << "Failed to parse IP address '" << ip
<< "'. (Note: FQDN/domain names/hostnames are not "
"supported. Including 'localhost')";
abort();
}
if (i == 0 && !app().reusePort())
{
DrogonFileLocker lock;
// Check whether the port is in use.
TcpServer server(HttpAppFrameworkImpl::instance().getLoop(),
listenAddress,
"drogonPortTest",
true,
false);
}
std::shared_ptr<HttpServer> serverPtr =
std::make_shared<HttpServer>(ioLoops[i],
listenAddress,
"drogon");
if (beforeListenSetSockOptCallback_)
{
serverPtr->setBeforeListenSockOptCallback(
beforeListenSetSockOptCallback_);
}
if (afterAcceptSetSockOptCallback_)
{
serverPtr->setAfterAcceptSockOptCallback(
afterAcceptSetSockOptCallback_);
}
if (listener.useSSL_ && utils::supportsTls())
{
auto cert = listener.certFile_;
auto key = listener.keyFile_;
if (cert.empty())
cert = globalCertFile;
if (key.empty())
key = globalKeyFile;
if (cert.empty() || key.empty())
{
std::cerr
<< "You can't use https without cert file or key file"
<< std::endl;
exit(1);
}
auto cmds = sslConfCmds;
std::copy(listener.sslConfCmds_.begin(),
listener.sslConfCmds_.end(),
std::back_inserter(cmds));
auto policy =
trantor::TLSPolicy::defaultServerPolicy(cert, key);
policy->setConfCmds(cmds).setUseOldTLS(listener.useOldTLS_);
serverPtr->enableSSL(std::move(policy));
}
servers_.push_back(serverPtr);
}
}
#else
if (!listeners_.empty())
{
listeningThread_ =
std::make_unique<EventLoopThread>("DrogonListeningLoop");
listeningThread_->run();
for (auto const &listener : listeners_)
{
auto ip = listener.ip_;
bool isIpv6 = (ip.find(':') != std::string::npos);
auto serverPtr = std::make_shared<HttpServer>(
listeningThread_->getLoop(),
InetAddress(ip, listener.port_, isIpv6),
"drogon");
if (listener.useSSL_ && utils::supportsTls())
{
auto cert = listener.certFile_;
auto key = listener.keyFile_;
if (cert.empty())
cert = globalCertFile;
if (key.empty())
key = globalKeyFile;
if (cert.empty() || key.empty())
{
std::cerr
<< "You can't use https without cert file or key file"
<< std::endl;
exit(1);
}
auto cmds = sslConfCmds;
auto policy =
trantor::TLSPolicy::defaultServerPolicy(cert, key);
policy->setConfCmds(cmds).setUseOldTLS(listener.useOldTLS_);
serverPtr->enableSSL(std::move(policy));
}
serverPtr->setIoLoops(ioLoops);
servers_.push_back(serverPtr);
}
}
#endif
}

@TYUTthelastone
Copy link
Contributor Author

应该是enable中抛出异常后httpserver析构导致的,具体析构流程是
~HttpServer->~TcpServer->~Acceptor->Channel::disableAll()->Channel::update()->EventLoop::updateChannel()->EventLoop::assertInLoopThread()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant