Move io context to singleton
The way we pass around io contexts is somewhat odd. Boost maintainers
in slack recommended that we just have a method that returns an io
context, and from there we can control this (context link lost years
ago).
The new version of clang claims the singleton pattern of passing in an
io_context pattern is a potential nullptr dereference. It's technically
correct, as calling the singleton without immediately initializing the
io context will lead to a crash.
This commit implements what the boost maintainers suggested, having a
single method that returns "the context" that should be used. This also
helps to maintain isolation, as some pieces are no longer tied directly
to dbus to get their reactor.
Tested: WIP
Change-Id: Ifaa11335ae00a3d092ecfdfb26a38380227e8576
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/http/http_server.hpp b/http/http_server.hpp
index b48137f..a7c2459 100644
--- a/http/http_server.hpp
+++ b/http/http_server.hpp
@@ -38,10 +38,10 @@
public:
Server(Handler* handlerIn, boost::asio::ip::tcp::acceptor&& acceptorIn,
std::shared_ptr<boost::asio::ssl::context> adaptorCtxIn,
- std::shared_ptr<boost::asio::io_context> io) :
- ioService(std::move(io)), acceptor(std::move(acceptorIn)),
+ boost::asio::io_context& io) :
+ ioService(io), acceptor(std::move(acceptorIn)),
// NOLINTNEXTLINE(misc-include-cleaner)
- signals(*ioService, SIGINT, SIGTERM, SIGHUP), handler(handlerIn),
+ signals(ioService, SIGINT, SIGTERM, SIGHUP), handler(handlerIn),
adaptorCtx(std::move(adaptorCtxIn))
{}
@@ -120,7 +120,7 @@
void stop()
{
- ioService->stop();
+ ioService.stop();
}
using Socket = boost::beast::lowest_layer_type<Adaptor>;
using SocketPtr = std::unique_ptr<Socket>;
@@ -133,7 +133,7 @@
return;
}
- boost::asio::steady_timer timer(*ioService);
+ boost::asio::steady_timer timer(ioService);
std::shared_ptr<Connection<Adaptor, Handler>> connection;
if constexpr (std::is_same<Adaptor,
@@ -157,20 +157,14 @@
Adaptor(std::move(*socket)));
}
- boost::asio::post(*ioService, [connection] { connection->start(); });
+ boost::asio::post(ioService, [connection] { connection->start(); });
doAccept();
}
void doAccept()
{
- if (ioService == nullptr)
- {
- BMCWEB_LOG_CRITICAL("IoService was null");
- return;
- }
-
- SocketPtr socket = std::make_unique<Socket>(*ioService);
+ SocketPtr socket = std::make_unique<Socket>(ioService);
// Keep a raw pointer so when the socket is moved, the pointer is still
// valid
Socket* socketPtr = socket.get();
@@ -181,7 +175,7 @@
}
private:
- std::shared_ptr<boost::asio::io_context> ioService;
+ boost::asio::io_context& ioService;
std::function<std::string()> getCachedDateStr;
boost::asio::ip::tcp::acceptor acceptor;
boost::asio::signal_set signals;