pistacheio / pistache

A high-performance REST toolkit written in C++
https://pistacheio.github.io/pistache/
Apache License 2.0
3.12k stars 690 forks source link

Add/remove routes at runtime #1100

Open arghness opened 1 year ago

arghness commented 1 year ago

I started writing a thread-safe wrapper for Router / Private::RouterHandler to support adding and removing routes dynamically, when I saw in the RouterHandler comments:

            /**
             * Used for mutable router. Useful if it is required to
             * add/remove routes at runtime.
             * \param[in] router Pointer to a (mutable) router.
             */
            explicit RouterHandler(std::shared_ptr<Rest::Router> router);

I wondered how this might be done safely, once the server is running? Can it only be done when processing a request on a single-threaded server?

chorfa007 commented 1 year ago

The same problem like you i used the API Routes::Remove but i still access to the server after removing routes in many use case the application crash due an issue of bind ...

arghness commented 1 year ago

The same problem like you i used the API Routes::Remove but i still access to the server after removing routes in many use case the application crash due an issue of bind ...

I'm pretty sure that's still not thread safe, it still just calls the methods on Router / Private::RouterHandler, so you'll run in to issues unless you're very careful.

I'm not entirely sure if my wrappers are fully thread-safe, but they seem okay so far.

dennisjenkins75 commented 1 year ago

I've not looked at the code, but I suspect that you are correct. Adding and removing routes is likely not thread safe; it should take a writer lock on the routing table, and doing route lookups should take a reader lock. This locking is likely non-existent.

You can probably verify this with valgrind --fair-sched=yes ${your_program}.