Debugging a Race Condition: The RtlRunOnceExecuteOnce Trap

A colleague encountered a tricky concurrency issue during a weekly debug session: a critical section failed to prevent two threads from entering the same code block, leading to a `TraceLoggingRegister` double-registration failure. Deep debugging revealed the root cause: the initialization function `InitializeCriticalSectionOnce` for `RtlRunOnceExecuteOnce` incorrectly returned `STATUS_SUCCESS` (0). This led `RtlRunOnceExecuteOnce` to believe initialization failed, causing it to re-initialize the critical section on every call, triggering the race condition. The solution was to change the return value to `TRUE`, or more elegantly, replace `CRITICAL_SECTION` with `SRWLOCK`. This case highlights how subtle return value errors can lead to severe consequences and underscores the importance of choosing the appropriate synchronization primitive.