connectedhomeip
connectedhomeip copied to clipboard
Platform::MemoryInit pretends to be threadsafe but is not
Problem
MemoryInit has an atomic counter that it uses to ensure MemoryAllocatorInit
is only called once if multiple MemoryInit
calls happen. But you can easily get a race as follows:
- Thread 1 calls
MemoryInit
, increments counter, loses timeslice before callingMemoryAllocatorInit
. - Thread 2 calls
MemoryInit
, counter is nonzero, MemoryInit returns. - Thread 2 does
Platform::MemoryAlloc
before things are properly initialized.
Proposed Solution
Either properly synchronize MemoryInit
(with a lock around the "increment the counter and call MemoryAllocatorInit" bits), or just use a non-atomic counter and clearly document that synchronization is the consumer's problem.
Reopening, since #18854 was reverted.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.