[go: nahoru, domu]

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

Crash when exiting program, after exiting event loop via exception #228

Open
timomrs opened this issue Sep 26, 2022 · 0 comments
Open

Crash when exiting program, after exiting event loop via exception #228

timomrs opened this issue Sep 26, 2022 · 0 comments

Comments

@timomrs
Copy link
Contributor
timomrs commented Sep 26, 2022

When a controller constructor throws, the event loop appears to exit cleanly after merging PR #224 . However, when the program exits, a fatal error is triggered. Minimal reproduction using Drogon:

Create a Drogon project with a controller:

drogon_ctl create project CrashExample
cd CrashExample/controllers
drogon_ctl create controller ExceptionalController

Edit main.cc and controllers/ExceptionalController.h to read:

// ExceptionalController.h
#pragma once

#include <drogon/HttpSimpleController.h>

using namespace drogon;

class ExceptionalController : public drogon::HttpSimpleController<ExceptionalController>
{
  public:
    ExceptionalController() {
      throw std::runtime_error("I'm exceptional, and therefore won't start. That should be fine.");
    }

    void asyncHandleHttpRequest(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback) override;
    PATH_LIST_BEGIN
    // list path definitions here;
    //PATH_ADD("/path", "filter1", "filter2", HttpMethod1, HttpMethod2...);

    PATH_ADD("/crash", Get);
    PATH_LIST_END
};
//main.cc

#include <drogon/drogon.h>
int main() {
    //Set HTTP listener address and port
    drogon::app().addListener("0.0.0.0",80);
    //Load config file
    //drogon::app().loadConfigFile("../config.json");
    //Run HTTP framework,the method will block in the internal event loop
    try {
        drogon::app().run();
    } catch (std::exception & e) {
        std::cout << "Caught exception \"" << e.what() << "\"... this is exiting.\n";
    }

    std::cout << "Press enter to exit...\n";
    std::getchar();

    return 0;
}

Build and run the app, press enter when prompted. Expected output:

20220926 06:35:01.622763 UTC 41933 WARN  Exception thrown from event loop, rethrowing after running functions on quit - EventLoop.cc:243
20220926 06:35:01.623295 UTC 41933 WARN  Rethrowing exception from event loop - EventLoop.cc:260
Caught exception "I'm exceptional, and therefore won't start. That should be fine."... this is exiting.
Press enter to exit...

Actual output:

20220926 06:35:01.622763 UTC 41933 WARN  Exception thrown from event loop, rethrowing after running functions on quit - EventLoop.cc:243
20220926 06:35:01.623295 UTC 41933 WARN  Rethrowing exception from event loop - EventLoop.cc:260
Caught exception "I'm exceptional, and therefore won't start. That should be fine."... this is exiting.
Press enter to exit...

20220926 06:35:05.766527 UTC 41933 FATAL It is forbidden to run loop on threads other than event-loop thread - EventLoop.cc:266

In other words, the event loop is still left in some illegal state. Note that the fatal error only appears after pressing enter.

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