Skip to content

Fix publication of once-block initialization between threads - #261

Merged
Lastique merged 1 commit into
boostorg:developfrom
dchristle:fix/once-block-publication
Nov 9, 2026
Merged

Lastique merged 1 commit into
boostorg:developfrom
dchristle:fix/once-block-publication

Conversation

@dchristle

@dchristle dchristle commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

The once-block fast path reads its status without taking the mutex used by the initializer. With a plain byte, a reader can observe initialized without 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_INIT now 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.

@dchristle
dchristle force-pushed the fix/once-block-publication branch from f4e9122 to 18229fd Compare November 8, 2026 21:20
@Lastique

Lastique commented Nov 8, 2026

Copy link
Copy Markdown
Member

Thanks. This code is ancient and it needs cleanup. But I don't like using atomic_ref as it may be inefficient and fail where atomic wouldn't. Better use atomic instead. Its default constructor is constexpr and zero-initializing now, so it should satisfy the requirement of zero static initialization. The underlying implementation might also avoid dealing with mutexes and condition variables and instead use waiting/notifying operations on the atomic instead.

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 status member being one byte makes the accesses actually atomic on real hardware.

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.
@dchristle
dchristle force-pushed the fix/once-block-publication branch from 18229fd to a18108c Compare November 8, 2026 22:32
@dchristle

dchristle commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@Lastique Thanks for taking a look. I changed the flag storage to boost::atomic<unsigned char> as suggested.

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

clang++ -std=c++11 -O3 -pthread once_block_publication_repro.cpp -o once_block_repro
./once_block_repro 5000000

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 -O0 the Boost-implementation-specific reproducer rose to 49,854 failures in 1,000,000 iterations. But anyway the main thing is the failures only occur on the unlocked fast path & the mutex path and release/acquire version consistently produced zero.

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).

@Lastique
Lastique merged commit 1e22683 into boostorg:develop Nov 9, 2026
48 of 49 checks passed
@Lastique

Lastique commented Nov 9, 2026

Copy link
Copy Markdown
Member

Thank you for the fix and especially for the testing.

Lastique added a commit that referenced this pull request Nov 9, 2026
@dchristle
dchristle deleted the fix/once-block-publication branch November 9, 2026 19:47
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.

2 participants