Skip to content

Commit

Permalink
Destroy io_context after plugin shutdown to clear it out before plugi…
Browse files Browse the repository at this point in the history
…n destroy
  • Loading branch information
heifner committed Sep 30, 2024
1 parent da11ec0 commit 6bfc1be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 5 additions & 3 deletions examples/executor_example/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class my_executor {
public:
template <typename Func>
auto post( int priority, Func&& func ) {
return boost::asio::post(io_serv, pri_queue.wrap(priority, --order, std::forward<Func>(func)));
return boost::asio::post(*io_serv, pri_queue.wrap(priority, --order, std::forward<Func>(func)));
}

auto& get_priority_queue() { return pri_queue; }
Expand All @@ -31,11 +31,13 @@ class my_executor {

void clear() { pri_queue.clear(); }

boost::asio::io_service& get_io_service() { return io_serv; }
void reset() { io_serv.emplace(); }

boost::asio::io_service& get_io_service() { return *io_serv; }

private:
// members are ordered taking into account that the last one is destructed first
boost::asio::io_service io_serv;
std::optional<boost::asio::io_service> io_serv{std::in_place};
appbase::execution_priority_queue pri_queue;
std::size_t order = std::numeric_limits<size_t>::max(); // to maintain FIFO ordering in queue within priority
};
Expand Down
8 changes: 4 additions & 4 deletions include/appbase/application_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ class application_base {
}

work.reset();
io_serv.restart();
// plugins shutdown down, drain io_context of anything posted while shutting down before destroying plugins
while (io_serv.poll())
;

try {
// plugins shutdown down at this point,
exec.clear(); // make sure the queue is empty
// Recreate the io_context since it doesn't provide a clear and we want the destructors of all the
// lambdas posted to the io_context to execute before destroying the plugins
exec.reset();
destroy_plugins();
} catch (...) {
if (!eptr)
Expand Down
10 changes: 7 additions & 3 deletions include/appbase/default_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class default_executor {
public:
template <typename Func>
auto post(int priority, Func&& func) {
return boost::asio::post(io_serv, pri_queue.wrap(priority, --order, std::forward<Func>(func)));
return boost::asio::post(*io_serv, pri_queue.wrap(priority, --order, std::forward<Func>(func)));
}

/**
Expand All @@ -32,17 +32,21 @@ class default_executor {
pri_queue.clear();
}

void reset() {
io_serv.emplace();
}

/**
* Do not run io_service in any other threads, as application assumes single-threaded execution in exec().
* @return io_serivice of application
*/
boost::asio::io_service& get_io_service() {
return io_serv;
return *io_serv;
}

private:
// members are ordered taking into account that the last one is destructed first
boost::asio::io_service io_serv;
std::optional<boost::asio::io_service> io_serv{std::in_place};
execution_priority_queue pri_queue;
std::size_t order = std::numeric_limits<size_t>::max(); // to maintain FIFO ordering in queue within priority
};
Expand Down

0 comments on commit 6bfc1be

Please sign in to comment.