Skip to content

base: never-destroy tls_queues to fix an exit-time mutex crash#213

Merged
chen3feng merged 2 commits into
masterfrom
fix-monitoring-shutdown-mutex
Jul 3, 2026
Merged

base: never-destroy tls_queues to fix an exit-time mutex crash#213
chen3feng merged 2 commits into
masterfrom
fix-monitoring-shutdown-mutex

Conversation

@chen3feng

Copy link
Copy Markdown
Collaborator

A static MonitoredTimer (and similar globals) calls DeleteThreadOutOfDutyCallback in its destructor during static destruction, which locks the tls_queues ThreadLocal's internal mutex. tls_queues was a plain namespace static, so on macOS it could already be destroyed by then — std::mutex::lock() then throws std::system_error EINVAL ("mutex lock failed") → std::terminate at process exit. monitoring_test reproduced this reliably (all cases pass, then SIGABRT on shutdown).

Wrap it in NeverDestroyed, exactly as GetGlobalQueue() already does in the same file, so the lock stays valid for the whole process lifetime. Leaking it is harmless (the OS reclaims it at exit).

Verified: monitoring_test, monitoring_with_global_tags_test, out_of_duty_callback_test all pass; the shutdown SIGABRT is gone.

chen3feng and others added 2 commits July 3, 2026 08:12
A static `MonitoredTimer` (and similar globals) calls
DeleteThreadOutOfDutyCallback in its destructor during static destruction, which
locks the `tls_queues` ThreadLocal's internal mutex. `tls_queues` was a plain
namespace static, so on macOS it could already be destroyed by then, and
std::mutex::lock() throws std::system_error EINVAL ("mutex lock failed") ->
std::terminate at process exit. monitoring_test reproduced this reliably
(all cases pass, then SIGABRT on shutdown).

Wrap it in NeverDestroyed, exactly as GetGlobalQueue() already does in the same
file, so the lock stays valid for the whole process lifetime. Leaking it is
harmless (the OS reclaims it at exit).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@chen3feng chen3feng merged commit 8833dfa into master Jul 3, 2026
10 checks passed
@chen3feng chen3feng deleted the fix-monitoring-shutdown-mutex branch July 3, 2026 00:32
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.

1 participant