Skip to content

Conversation

@quantpoet
Copy link

This commit fixes a race condition in set_logger_racy where multiple threads could simultaneously initialize different loggers.

The fix uses atomic compare_exchange to ensure only one thread can transition from UNINITIALIZED to INITIALIZING state, eliminating the TOCTOU bug while preserving the original API.

Signed-off-by: quantpoet <quantway@outlook.com>
@KodrAus
Copy link
Contributor

KodrAus commented Sep 20, 2025

Thanks for the PR @quantpoet.

set_logger_racy is racy by definition, and is intended for embedded environments that don’t have multiple threads. As a caller you’re required to ensure there is only a single call to the function. Since the compare exchange here isn’t actually atomic I don’t think it really buys us any additional safety.

@quantpoet
Copy link
Author

Thanks for the PR @quantpoet.

set_logger_racy is racy by definition, and is intended for embedded environments that don’t have multiple threads. As a caller you’re required to ensure there is only a single call to the function. Since the compare exchange here isn’t actually atomic I don’t think it really buys us any additional safety.

Oh indeed. Thanks for your reply!

@quantpoet quantpoet closed this Sep 24, 2025
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.

2 participants