Repository navigation
Fix publication of once-block initialization between threads - #261
Conversation
f4e9122 to
18229fd
Compare
|
Thanks. This code is ancient and it needs cleanup. But I don't like using The current code, while not clean in terms of the formal language rules, works in practice because of the synchronization imposed by the underlying mutexes and condition variables. The So, as is, I don't think I will accept this PR. I'm not sure when I will get to the rewrite, but if you're interested, you can have a try. |
The unlocked fast path reads a plain status byte while initializer threads write it under a mutex. It can observe completion without acquiring the initialized data. Store status in boost::atomic. Successful commit uses release and the unlocked fast path uses acquire; mutex-protected transitions remain relaxed. Add a regression test for publication to a fast-path reader.
18229fd to
a18108c
Compare
|
@Lastique Thanks for taking a look. I changed the flag storage to I was also able to reproduce an incorrect payload on a real Apple ARM64 device without ThreadSanitizer. This standalone reproducer mirrors the current fast and slow paths and runs the plain-byte and atomic versions side by side: once_block_publication_repro.cpp Repeated runs of the plain-byte version produced stale payload reads, while the release/acquire version didn't produce any. The reproducer records whether the reader entered the mutex slow path; no slow-path read failed, so the stale reads are specifically from that unlocked fast path. I got 172–289 failures per 5,000,000 iterations with the reproducer, which is about 1 in ~17k or so. I did an earlier reproducer run against the actual Boost implementation and that one showed 26,021 stale payload reads (~1 per 192 iters). The exact rate varies substantially with optimization and scheduling - with ARM's weaker memory model is probably key in practice here - on x86 the compiler might produce code naturally that might be safe in practice (but technically I don't think it guarantees it still). |
|
Thank you for the fix and especially for the testing. |
The once-block fast path reads its status without taking the mutex used by the initializer. With a plain byte, a reader can observe
initializedwithout being guaranteed to see the data written during initialization.This is observable on ARM64 hardware. A two-thread stress reproducer puts the status and payload on separate cache lines and records whether the reader used the mutex slow path. In one five-million-iteration Apple ARM64 run against the original code, 26,021 fast-path reads saw the stale zero payload. None of the 168,934 slow-path reads failed. The same run with this change had no stale reads.
Store the status in
boost::atomic<unsigned char>. A release store publishes successful initialization, and an acquire load on the unlocked fast path receives it. Accesses protected by the once-block mutex remain relaxed.BOOST_LOG_ONCE_BLOCK_INITnow value-initializes the atomic, preserving constant zero initialization.The regression test has one thread initialize a plain value and another read it through the once-block fast path. A relaxed atomic flag controls scheduling without independently publishing the value, so the once-block must provide the ordering.
Against the original implementation, ThreadSanitizer reports the status-byte race. With this change, the same test runs clean. All four threaded once-block tests pass under ThreadSanitizer on macOS ARM64 with C++11, and all three single-threaded tests pass, including exception retry.