Class NonAtomicConcurrentMapUpdateDetector
ConcurrentMap.
Why it matters. A ConcurrentMap makes each
individual operation atomic, which lulls callers into writing
compound sequences that are not:
if (!map.containsKey(k)) { // thread A and B both see "absent"
map.put(k, compute()); // ...both put; one result is lost
}
or the read-modify-write variant:
Value v = map.get(k); // both read the same snapshot
map.put(k, v.withIncrement); // last writer wins; updates vanish
The thread-safe primitive that makes the operation atomic is
putIfAbsent, computeIfAbsent, compute, or merge.
This is distinct from AtomicNonAtomicUpdateDetector (which covers
Atomic* types) and from ConcurrentMapComputeRecursionDetector
(which covers re-entrancy inside computeIfAbsent).
Because the bug is a usage pattern rather than an object property, this
detector is cooperative: the code under test reports each non-atomic
check-then-act it performs via recordCheckThenAct(java.util.concurrent.ConcurrentMap<?, ?>, java.lang.Object, java.lang.String, java.lang.Thread). The detector flags
a violation when two or more threads perform such a sequence against the same
map and key — the exact precondition for a lost update — and no one lock covered
every one of them. A compound operation inside a critical section every caller enters
is atomic in effect, so it is not reported. The map's own monitor counts with no
declaration (synchronized (map)); any other lock is seen only when declared
through AsyncTestContext.holdingLock(...) or woven by the agent, and a lock
the library never saw leaves the finding standing.
Callers that all put the very same instance lose nothing: the map ends as
putIfAbsent would leave it, and no caller holds a value the map dropped. A caller
that says what it put, through the overload taking the value, lets the detector excuse such
a site. The excuse is judged per round, as the sharing is: callers that each put their
round's one instance are not reported for putting a different one in another round, which
they never overlapped, while a round whose racing callers put two instances, or one of which
recorded no value, is. Values are compared by identity, not equals: two new empty
lists are equal, yet the one the map dropped loses whatever its caller adds to it next. So
equal immutable values built per caller, such as "v" + i or a Long outside the
boxing cache, are still reported; a caller that means one value passes one canonical instance.
With the agent attached, a check that names no key is paired too: if (map.size() <
LIMIT) map.put(k, v) overfills a bounded map, if (map.isEmpty()) map.put(k, v) seeds it
twice, and if (map.size() > MAX) map.remove(oldest) evicts twice, when two threads pass
the check together (#1249). Such a pair is reported when another thread's put or remove on the
map landed between one thread's check and its own put or remove, and no lock was held from the
check through the act. The agent cannot see whether the put depended
on the check, so the finding is graded PROMPT; a keyed pair stays VERDICT. See
agentSizeCheck(java.util.Map<?, ?>, boolean).
Usage:
var d = new NonAtomicConcurrentMapUpdateDetector();
// inside the code path that does get/containsKey-then-put:
d.recordCheckThenAct(cache, userId, "lazy-cache-fill", Thread.currentThread());
- Since:
- 1.7.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRecords the act half: a wovenputorremoveonmap.voidagentCheck(Map<?, ?> map, @Nullable Object key, boolean absent, boolean read) Records the check half of a check-then-act the agent saw: a wovencontainsKeyorgeton a map whose own calls are each atomic.voidagentSizeCheck(Map<?, ?> map, boolean isEmpty) Records a check that names no key, a wovensizeorisEmptyon a map whose own calls are each atomic (#1249).analyze()Analyses what has been recorded about the observation and builds the report for it.voidrecordCheckThenAct(ConcurrentMap<?, ?> map, Object key, @Nullable Object value, String operation, Thread thread) Record acontainsKey-then-putthat putsvaluewhenkeyis absent.voidrecordCheckThenAct(ConcurrentMap<?, ?> map, Object key, String operation, Thread thread) Record that the calling thread performed a non-atomic check-then-act (e.g.
-
Constructor Details
-
NonAtomicConcurrentMapUpdateDetector
public NonAtomicConcurrentMapUpdateDetector()
-
-
Method Details
-
recordCheckThenAct
Record that the calling thread performed a non-atomic check-then-act (e.g.containsKey/getthenput) againstmapforkey. The detector is not told what was put, so a site recorded here is reported whatever every caller put; seerecordCheckThenAct(ConcurrentMap, Object, Object, String, Thread).- Parameters:
map- the ConcurrentMap being mutated (null-safe)key- the key involved (may benull)operation- descriptive label for reports (may benull)thread- the thread performing the sequence
-
recordCheckThenAct
public void recordCheckThenAct(ConcurrentMap<?, ?> map, Object key, @Nullable Object value, String operation, Thread thread) Record acontainsKey-then-putthat putsvaluewhenkeyis absent. When every caller of a round put the same instance, that round's race lost nothing and is not reported.Only for a value chosen without reading the map. After
v = map.get(k); map.put(k, v + 1)two callers putting one value is the lost update itself, and a check that also decides other work, such as sending once, is a defect whatever is put: record those withrecordCheckThenAct(ConcurrentMap, Object, String, Thread). The detector cannot tell those shapes from the absent-check one: it takes the caller's word for what was put, so recording them here excuses a real lost update.- Parameters:
map- the ConcurrentMap being mutated (null-safe)key- the key involved (may benull)value- what the caller put, or would have put had the key been absent, compared by identity;nullsays nothing and keeps the site reportableoperation- descriptive label for reports (may benull)thread- the thread performing the sequence- Since:
- 1.12.3
-
agentCheck
Records the check half of a check-then-act the agent saw: a wovencontainsKeyorgeton a map whose own calls are each atomic.Called by the agent's map hooks, never by a test body. Only the shapes that lose an update on their own are kept: a key found absent, by either call, and a value read by
get, which a following put may derive from. AcontainsKeythat found the key decides an overwrite, which loses nothing unless a third party removes the key in between, so it clears the thread's pending check rather than setting one.- Parameters:
map- the map, aConcurrentMap, aHashtableor aCollections.synchronized*wrapperkey- the key the check readabsent- whether the check found the key absentread- whether the check was aget, which hands the value to the caller- Since:
- 1.13.2
-
agentSizeCheck
Records a check that names no key, a wovensizeorisEmptyon a map whose own calls are each atomic (#1249).Called by the agent's map hooks, never by a test body. It is the check half of
if (map.size() < LIMIT) map.put(k, v): the thread's next put or remove on the same map, in the same round, completes the pair, and the pair is reported when another thread's put or remove on the map landed between the two and no lock was held across them. A later keyed check does not replace it, sosizethencontainsKeythenputis judged both ways.- Parameters:
map- the map, aConcurrentMap, aHashtableor aCollections.synchronized*wrapperisEmpty- whether the check wasisEmpty, for the report- Since:
- 1.14.3
-
agentAct
Records the act half: a wovenputorremoveonmap. It completes a check-then-act when the calling thread's last check was on the same map, by identity, and the same key, by the key's equality, in the same round; the check is consumed either way. A completed pair is recorded with the value put, so a round whose callers all put one instance is excused exactly asrecordCheckThenAct(ConcurrentMap, Object, Object, String, Thread)excuses it, unless the value is a boxed primitive or an enum constant, which is one instance only because its value is (#996).- Parameters:
map- the map acted onkey- the key acted onvalue- the value put,nullfor a removeremove- whether the act was aremove- Since:
- 1.13.2
-
analyze
Analyses what has been recorded about the observation and builds the report for it.- Returns:
- the findings this detector collected during the run
-