Skip to content

fix(happens-before): a lost atomic update attempt withdraws its release (#1316, part) - #1447

Merged
PIsberg merged 4 commits into
mainfrom
fix/p4-lost-attempt-withdraw
Nov 21, 2026
Merged

PIsberg merged 4 commits into
mainfrom
fix/p4-lost-attempt-withdraw

Conversation

@PIsberg

@PIsberg PIsberg commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Part of #1316. Stacked on #1444 and #1446, review the last commit.

Why

The woven increment, add and update forms of AtomicInteger and AtomicLong release the value they are about to store before each compare-and-set attempt (#1315). An attempt that lost to another thread storing the same value left its release as the atomic's latest. A get that saw the winner's value then took the loser's release, so it was ordered after the loser's earlier writes too. The program has no such edge, and it can hide a race on those writes.

Change

Verified

  • AgentHappensBeforeFeedTest.aLostAttemptWithdrawsItsRelease: the update function lets another thread win the first swap by storing the same value through a woven set, then a reader gets it. This was red before the change (the reader was ordered after the loser's write) and is green after. The twin, where the attempt itself stored the value, stays ordered throughout.
  • Library -P fast: 4339 tests green. Agent AtomicFlagPublicationWeavingTest: 35/35 under -P e2e. Full corpus (6 lanes) green after a fresh install. Static gates and the javadoc jar are clean.

Docs

  • docs/CHANGELOG.md.
  • Javadoc on withdrawVolatile and FieldClock.withdraw, and addPublishing's javadoc, which described the extra edge as unconditional.

🤖 Generated with Claude Code

PIsberg and others added 4 commits November 20, 2026 18:59
…#1317, part)

#1315 made set, getAndSet, compareAndSet and the arithmetic forms of
AtomicBoolean/AtomicInteger/AtomicLong release and get acquire. A field
published through lazySet, setRelease, updateAndGet/getAndUpdate/
accumulateAndGet/getAndAccumulate, a won compareAndExchange(Release) or
a won weakCompareAndSetVolatile/Release, and read after a get that saw
the value, was still reported by AtomicityValidator as an unordered
publication on correct code. And a reader whose read was getAndSet's
previous value, a compareAndExchange witness, or what an add or update
returned acquired nothing.

Those hooks now call primitiveSlotStoring (before the store; after a
won swap for the compare forms) and HappensBefore.acquireVolatile on
the value read. The update and accumulate forms run as a CAS loop like
addPublishing, so the value to be stored is known before the store.
setPlain, setOpaque and weakCompareAndSetPlain stay unordered, as the
JMM says; AtomicFlagPublicationWeavingTest pins setPlain.

Verified failing first: 23 new AtomicFlagPublicationWeavingTest rows
(one per form) failed before the hook change and pass after; the
setPlain negative passed throughout. Library atomic, spinlock,
happens-before and atomicity tests plus MinTrustGateTest under -P e2e,
the full agent suite under -P e2e and the corpus eval are green;
static gates and the javadoc jar are clean.

Still open on #1317: getAcquire, intValue and longValue reads, the
AtomicLong forms the weaver does not substitute, and the updater,
VarHandle and array forms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The previous commit made the value an increment, add, update or
accumulate returns acquire. The corpus idioms lane then filled a 2 GB
heap within seconds on CI (Java 26 OOM; 21 and 25 hung past 25 min),
reproduced locally under -Xmx2g. A heap histogram of the dead fork
showed 1.38 GB of long[] and 690 MB of int[] averaging 32 KB and 14 KB:
HappensBefore stamps near MAX_ENTRIES (4096). The add releases before
its store, so a counter every thread bumps (CorpusRecorder's static
BODY_EXECUTIONS, bumped once per body execution) collects every
bumper's clock; acquiring on the read handed that whole clock to each
bumper, and every access it recorded afterwards pinned a copy. A user
suite with a global id counter would hit the same OOM.

The add and update forms now release only, as on main before #1317.
getAndSet and the compareAndExchange witness still acquire, and a get
of the counter still acquires what it published.

Verified failing first: AtomicCounterClockTest (11 forms, 200 earlier
bumpers) failed with a clock of 201 threads on every form and passes
after. AtomicFlagPublicationWeavingTest's two add/update-result rows
are inverted to assertRaced, pinning the decision. Idioms lane under
-Xmx2g: OOM before, 220/220 in 37 s after; full corpus (6 lanes)
green; library -P fast 4336 tests green; static gates and javadoc jar
clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The woven addAll released every element before the call and, outside
queues, withdrew nothing. An element a concurrent set refused (it held
an equal one) or one a call throwing part way never added stayed
published, so a thread that reached it another way was ordered after
the producer by a hand-off that never happened, hiding a race.
HappensBefore.retract could not be reused: it keeps only the thread's
last release and forgets it at the next stamped access, which the
element's own equals/hashCode makes inside addAll.

HappensBefore.releaseWithdrawable hands each release back and withdraw
takes it back exactly (CAS of the element's clock from what the release
installed to what it held before; a later release by another thread
keeps it). collectionAddAll withdraws every element's release when the
call returns false, and those of elements the collection does not hold
by identity when it throws.

Verified failing first: AgentHappensBeforeFeedTest's two new tests
(newKeySet already holding equal tags; newKeySet.addAll stopping at a
null) failed before and pass after; the twin (the element that went in
stays published) passes throughout. Library -P fast 4338 green, full
corpus green after a fresh install, static gates and javadoc jar clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…se (#1316, part)

The woven increment, add and update forms of AtomicInteger/AtomicLong
release the value they are about to store before each compare-and-set
attempt (#1315). An attempt that lost to another thread storing the
same value left its release as the atomic's latest, so a get that saw
the winner's value was ordered after the loser's earlier writes too:
an edge the program does not have, which can hide a race on them.

HappensBefore.releaseVolatile now remembers, per thread and without
allocating, the field clock and the release it installed;
withdrawVolatile puts the field back to the release before it while it
is still the latest. addPublishing (int, long) and updatePublishing
call it after a lost compareAndSet. When the winner released after the
loser, its cumulative release already holds the loser's clock and
nothing is withdrawn; #1316 stays open for that order.

Verified failing first: AgentHappensBeforeFeedTest
.aLostAttemptWithdrawsItsRelease (the update function lets another
thread win the first swap, then a reader gets the winner's value) was
red before and is green after; its twin (the attempt stored the value)
stays silent throughout. Library -P fast 4339, agent
AtomicFlagPublicationWeavingTest 35/35 under -P e2e, full corpus after
a fresh install, static gates and javadoc jar all green.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

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.

1 participant