Skip to content

Control exit-time destructors with new static variable macro #2105

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

Conversation

falbrechtskirchinger
Copy link
Contributor

@falbrechtskirchinger falbrechtskirchinger commented Mar 12, 2025

Introduce the CPPHTTPLIB_DEFINE_STATIC macro to define static variables with optional dynamic allocation, enabled by defining CPPHTTPLIB_NO_EXIT_TIME_DESTRUCTORS. Dynamic allocation prevents running their destructors, which avoids race conditions with atexit handlers that may depend on them. Also includes a test to verify this functionality and updates the documentation to explain the usage and rationale for disabling exit-time destructors.

Note: gtest v1.12.1 does not include the fix for exit-time destructors (after finding the last C++11 version, I forgot to re-check that it includes the relevant commit), necessitating compiling gtest separately and without -Wexit-time-destructors.

Resolves #2097.

To do:

By making the random engine thread_local, each thread now has its own
independent random sequence, ensuring safe concurrent access.
Additionally, using an immediately invoked lambda expression to
initialize the engine eliminates the need for separate static seed
variables.
Introduce macro CPPHTTPLIB_DEFINE_STATIC to define static variables,
with optional dynamic allocation to avoid exit-time destructors and race
conditions with atexit handlers.
@falbrechtskirchinger falbrechtskirchinger changed the title Improve static variable management Control exit-time destructors with new static variable macro Mar 13, 2025
@falbrechtskirchinger falbrechtskirchinger marked this pull request as ready for review March 13, 2025 12:01
@yhirose
Copy link
Owner

yhirose commented Mar 13, 2025

@falbrechtskirchinger thanks for the pull request. Could you please take a look at my #2097 (comment), and give me your thought?

@falbrechtskirchinger
Copy link
Contributor Author

Closing per our discussion in #2097.

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

Successfully merging this pull request may close these issues.

Crash when calling std::exit while server running or client requests in flight
2 participants