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

Mutating Middleware context in CROW_CATCHALL_ROUTE causes Segmentation fault #859

Open
lbatuska opened this issue Jul 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@lbatuska
Copy link

When I try to modify a value contained in the context struct in a CROW_CATCHALL_ROUTE it causes a segfault but works perfectly with a CROW_ROUTE

A very minimal example to reproduce it:

struct TestMW {
  struct context {  // NOLINT(readability-identifier-naming)
    int id{};
    std::string s;
    context() : s("Test") {}
  };

  void before_handle(  // NOLINT(readability-convert-member-functions-to-static)
      crow::request& req,  // NOLINT(misc-unused-parameters)
      crow::response& res,
      context& ctx) {  // NOLINT(misc-unused-parameters)
  }

  void after_handle(  // NOLINT(readability-convert-member-functions-to-static)
      crow::request& req,   // NOLINT(misc-unused-parameters)
      crow::response& res,  // NOLINT(misc-unused-parameters)
      context& ctx          // NOLINT(misc-unused-parameters)
  ) {}
};

int main() {
  crow::App<TestMW> app;
  app.loglevel(crow::LogLevel::Debug);
  CROW_CATCHALL_ROUTE(app)
  ([&](crow::request const& req, crow::response& res) {
    auto& ctx = app.get_context<TestMW>(req);
    ctx.s = "Something else";
    res.write("Test Write");
    res.end();
  });

  app.port(8181).multithreaded().run();
}

When I run the following code and try curl-ing on any url on localhost:8181 I get the following on the server side:

(2024-07-27 19:19:54) [INFO    ] Crow/master server is running at http://0.0.0.0:8181 using 12 threads
(2024-07-27 19:19:54) [INFO    ] Call app.loglevel(crow::LogLevel::Warning) to hide Info level logs.
(2024-07-27 19:19:54) [DEBUG   ] 0xaac179bbc840 {0} queue length: 1
(2024-07-27 19:19:58) [DEBUG   ] 0xaac179bbc450 {1} queue length: 1
(2024-07-27 19:19:58) [DEBUG   ] 0xaac179bc7960 timer cancelled: 0xff930fdfdfa8 0
(2024-07-27 19:19:58) [DEBUG   ] task_timer cancelled: 0xff930fdfdfa8 0
(2024-07-27 19:19:58) [DEBUG   ] task_timer scheduled: 0xff930fdfdfa8 1
(2024-07-27 19:19:58) [DEBUG   ] 0xaac179bc7960 timer added: 0xff930fdfdfa8 1
Segmentation fault (core dumped)

I have tested it on 2 different systems with different architectures ARM64 / aarch64 (Win11 wsl2) and x86_64 (Arch linux) unfortunately with the same result.
I'm using the origin/v1.2.0 tag with it's latest commit.

Thank You in Advance for the help and let me know if I can provide any extra info!

@gittiver gittiver added the bug Something isn't working label Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants