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

Drain io_context after shutdown of plugins #34

Merged
merged 12 commits into from
Oct 21, 2024
Merged

Drain io_context after shutdown of plugins #34

merged 12 commits into from
Oct 21, 2024

Conversation

heifner
Copy link
Member

@heifner heifner commented Sep 26, 2024

This solves the problem of hang on shutdown of nodeos. See AntelopeIO/spring#822.

Make sure to clear to the io_context after shutdown of the plugins. Also it uses stopped() of io_context instead of the application is_quiting() to avoid exiting the loop early.

Example failure: https://github.com/AntelopeIO/spring/actions/runs/11075714563
Example with this PR: https://github.com/AntelopeIO/spring/actions/runs/11076069453

Needs AntelopeIO/spring#842

@heifner heifner marked this pull request as draft September 26, 2024 13:11
@heifner heifner marked this pull request as ready for review September 27, 2024 18:51
@heifner heifner added the OCI Work exclusive to OCI team label Sep 27, 2024
@heifner heifner changed the title Clean shutdown Drain io_context after shutdown of plugins Sep 27, 2024
Copy link
Member

@spoonincode spoonincode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't seem sufficient for me; I wrote a small appbase test case yesterday, one moment while I post it

@heifner heifner marked this pull request as draft September 27, 2024 19:10
@heifner
Copy link
Member Author

heifner commented Sep 27, 2024

This change doesn't seem sufficient for me; I wrote a small appbase test case yesterday, one moment while I post it

Yeah, my nodeos test is still failing in ci/cd. It was working locally for me.

@spoonincode
Copy link
Member

It is overkill on threads and stuff. But just run and ctrl-C, and it will fail, where imo this should not.

Use your imagination that thing_better_be_alive is accessing some state that thready_plugin contains.

#include <appbase/application.hpp>
#include <iostream>

namespace bpo = boost::program_options;

using bpo::options_description;
using bpo::variables_map;

static bool thing = true;
struct thing_better_be_alive {
   ~thing_better_be_alive() {
      if(!thing)
         throw "BOOM";
   }
};

class thready_plugin : public appbase::plugin<thready_plugin> {
   public:
     APPBASE_PLUGIN_REQUIRES();

     void set_program_options( options_description& cli, options_description& cfg ) override {}

     void thread_work() {
        boost::asio::post(ctx, [&]() {
           thing_better_be_alive better_be;
           boost::asio::post(appbase::app().get_io_service(), [&,is_it=std::move(better_be)]() {
	      thread_work();
	   });
	});
     }

     void plugin_initialize( const variables_map& options ) {}
     void plugin_startup()  {
        for(unsigned i = 0; i < 48*4; i++)
           thread_work();

        for(unsigned i = 0; i < 48; ++i)
           threads.emplace_back([this]() {
              ctx.run();
           });
     }
     void plugin_shutdown() {
        usleep(100000);  //oh gee it takes a while to stop
        ctx.stop();
        for(unsigned i = 0; i < 48; ++i)
          threads[i].join();
     }

     ~thready_plugin() {
        thing = false;
     }

     boost::asio::io_context ctx;
     boost::asio::executor_work_guard<boost::asio::io_context::executor_type> wg = boost::asio::make_work_guard(ctx);

   private:
     std::vector<std::thread> threads;
};

int main(int argc, char** argv) {
   appbase::application::register_plugin<thready_plugin>(); 

   
   appbase::scoped_app app;
      
   if( !app->initialize<thready_plugin>( argc, argv ) )
      return -1;
   app->startup();
   app->exec();
   return 0;
}

Copy link
Member

@spoonincode spoonincode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm were you planning to bring it out of draft after more testing?

@@ -0,0 +1,79 @@
#include <appbase/application.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added this file as a test but didn't plumb it through cmake etc. not sure if that's intentional or not

Copy link
Member Author

@heifner heifner Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/CMakeFiles.txt has file(GLOB UNIT_TESTS "*.cpp")
I verified it runs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely missed the glob 👍

@spoonincode spoonincode dismissed their stale review September 27, 2024 22:19

test case works now

@heifner heifner marked this pull request as ready for review October 14, 2024 19:14
@heifner heifner requested a review from spoonincode October 14, 2024 19:15
@@ -152,8 +152,8 @@ void application_base::wait_for_signal(std::shared_ptr<boost::asio::signal_set>
});
}

void application_base::setup_signal_handling_on_ios(boost::asio::io_service& ios, bool startup) {
std::shared_ptr<boost::asio::signal_set> ss = std::make_shared<boost::asio::signal_set>(ios, SIGINT, SIGTERM);
void application_base::setup_signal_handling_on_ios(boost::asio::io_context& io_ctx, bool startup) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're touching everything to rename io_service->io_context, maybe might as well rename setup_signal_handling_on_ios to setup_signal_handling_on_ioc or such, since ios meant io_service 🤷‍♂️

@heifner heifner merged commit 1309099 into main Oct 21, 2024
2 checks passed
@heifner heifner deleted the clean-shutdown branch October 21, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCI Work exclusive to OCI team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants