Why is it dangerous that StampedLock is non-reentrant, and what bug does this commonly cause?
answer
- Reentrant = same thread can re-lock; StampedLock isn't
- Re-acquire by holder → waits for itself → self-deadlock
- Hits recursion, callbacks, refactored helpers
- Keep sections flat; don't call out while locked
- Need re-entry? use ReentrantReadWriteLock
basics
~20 sStampedLock does not recognize its holder: a thread holding the write lock that calls writeLock() or readLock() again, or a reader that calls writeLock(), blocks waiting for its own release. A write-locked method calling a helper that also locks therefore freezes.
solid answer
~50 sReentrancy is the property that a thread already holding a lock can re-acquire it without blocking; synchronized and ReentrantReadWriteLock are reentrant. StampedLock is deliberately not — it tracks no per-thread hold count. The danger: if a thread holding the write lock calls another method, directly or indirectly, that calls writeLock() or readLock() on the same StampedLock, or a read-lock holder calls writeLock(), that call blocks waiting for the release the thread itself was due to make, and unless other code releases the lock for it (StampedLock allows that), the wait never ends. The thread deadlocks against itself. This bites with recursion, with overridable/callback methods invoked while locked, and when refactoring splits a locked operation into helper methods that each lock. Mitigations: keep critical sections flat and self-contained (never call out while holding the lock), or use ReentrantReadWriteLock if re-entry is genuinely needed.
go deeper
Knows 'reentrant' means a thread can lock something it already holds, and that StampedLock cannot.
Can describe the self-deadlock that results when a thread re-acquires a StampedLock it holds, and names recursion as a trigger.
Explains why it's non-reentrant (no ownership or hold count), the call-out/refactor triggers, and the mitigations including choosing ReentrantReadWriteLock when re-entry is needed.
Sets team guidelines (no call-outs under StampedLock, flat critical sections), reasons about the broader lock-discipline risks of an owner-less lock, and chooses the lock primitive per access pattern.
### What 'reentrant' means A lock is **reentrant** (also called *recursive*) if a thread that **already holds** it can **acquire it again** without blocking, and must then release it the same number of times before it is truly free. Java's `synchronized` is reentrant: a synchronized method can call another synchronized method on the same object on the same thread and it just works. `ReentrantLock` and `ReentrantReadWriteLock` are reentrant by design (the name says so) — internally they keep a *hold count* per owning thread. ### What StampedLock does instead `StampedLock` keeps **no per-thread ownership or hold count**. A stamp is just a version token, not "thread X owns this." Consequently, the lock has **no idea** that the thread now requesting it is the same thread that already holds it. From the lock's point of view, a second acquire from an already-holding thread looks exactly like a *different* thread wanting in — so a blocking acquire **waits** until the lock is released. But that release was this thread's own job, and the thread is now blocked. StampedLock would let another thread release the lock (with the stamp, or with `tryUnlockWrite()`), but code written to the usual lock/try/finally pattern never does, so the wait never ends. The thread is **deadlocked against itself** — a *self-deadlock*. That covers `writeLock()` or `readLock()` while the thread holds the write lock, and `writeLock()` while it holds a read lock; the untimed `tryWriteLock()`, `tryReadLock()` and `tryOptimisticRead()` return 0 at once instead, and the timed forms give up at their timeout. A second `readLock()` while holding a read lock usually goes through, because read mode is shared, but the Javadoc says the scheduling policy does not consistently prefer readers over writers, so never rely on it. ### The classic bug shapes 1. **Recursion under the lock.** ```java void process(Node n) { long stamp = lock.writeLock(); try { // ... mutate n ... if (n.child != null) process(n.child); // re-acquires writeLock -> SELF-DEADLOCK } finally { lock.unlockWrite(stamp); } } ``` The recursive call tries to take the write lock again and hangs forever. 2. **Calling out to code that re-locks.** A method holding the lock calls a helper, a listener, or an overridden method that (perhaps far down the call chain) acquires the same StampedLock. With a reentrant lock this is merely *inadvisable* (you can call out while locked and risk other deadlocks); with a non-reentrant lock it is an **immediate** self-deadlock if that path calls `writeLock()` or `readLock()` while the caller holds the write lock, or `writeLock()` while the caller holds a read lock. 3. **Refactoring that splits a locked block.** Extracting part of a critical section into a helper that "also locks to be safe" introduces a second acquire on the same thread. ### Why StampedLock is non-reentrant Reentrancy needs an owner: the lock must recognize that the thread asking is the one already holding it. `StampedLock` has **no notion of ownership** — its Javadoc says locks acquired in one thread can be released or converted in another — and its state is just a version and a mode, so there is no holder to count re-acquisitions against. That is what lets a stamp be passed to another method to use, convert or release. The Javadoc presents StampedLocks as internal utilities for building thread-safe components, whose authors know what runs under the lock. The trade is that **the programmer must guarantee a thread never tries to re-enter**. ### How to avoid the bug - **Keep critical sections flat and short.** Acquire, do the minimal work, release. Do not call arbitrary or recursive code while holding a StampedLock. - **Never call out (callbacks, virtual methods, listeners) while holding the lock.** Compute under the lock, release, then call out with the result. - **If you genuinely need re-entry** (recursive data-structure traversal under one lock, etc.), use **`ReentrantReadWriteLock`** or `synchronized` instead — pick the right tool. - In code review, treat "locks a StampedLock and then calls a method" as a smell to inspect. ### Related gotchas Because it tracks no owner, a lock acquired in one thread may legally be released or converted in another, so the lock cannot tell you which thread forgot to release; it checks only that a stamp matches its current state, and `unlockRead`/`unlockWrite` throw `IllegalMonitorStateException` when it does not — correctness depends on disciplined stamp handling. And it has **no `Condition`** support, so wait/await coordination must use other primitives.
- Why is StampedLock non-reentrant by design?StampedLock has no notion of ownership: a lock taken in one thread can be released or converted in another, and its state is only a version and a mode, so there is no holder to count re-acquisitions for. That keeps stamps transferable between methods; the Javadoc presents StampedLocks as internal utilities for building thread-safe components, which leaves the no-re-entry burden on their authors.
- How would you fix code that recursively traverses a tree while holding a StampedLock write lock?Either acquire the lock once at the top level and have the recursive helper assume it is already held (passing the protected state, not re-locking), or switch to ReentrantReadWriteLock / synchronized which permit re-entry. The key is exactly one acquire per thread for the whole operation.
- Believing a thread can safely re-acquire a StampedLock it already holds
- Calling recursive or overridable methods while holding a StampedLock
- Assuming StampedLock can replace ReentrantReadWriteLock unchanged in reentrant code
- Expecting the lock to detect or report the self-deadlock (it just hangs)
- StampedLock
- Java 1.8
context
- StampedLock (Java SE 27 & JDK 27)docs.oracle.com
- ReentrantReadWriteLock (Java SE 27 & JDK 27)docs.oracle.com