RFR: 8366659: ObjectMonitor::wait() can deadlock with a suspension request

Anton Artemov duke at openjdk.org
Mon Oct 27 13:49:01 UTC 2025


Hi, please consider the following changes:

If suspension is allowed when a thread is re-entering an object monitor (OM), then a deadlock is possible. There are two places where it can happen:

1) The waiting thread is made to be a successor and is unparked. Upon a suspension request, the thread will suspend itself whilst clearing the successor. The OM will be left unlocked (not grabbed by any thread), while the other threads are parked until a thread grabs the OM and the exits it. The suspended thread is on the entry-list and can be selected as a successor again. None of other threads can be woken up to grab the OM until the suspended thread has been resumed and successfully releases the OM.

2) The race between suspension and retry: the thread could reacquire the OM and complete the wait() code in full, but then on return to Java it will be suspended while holding the OM.

The issues are addressed by not allowing suspension in case 1, and by handling the suspension request at a later stage, after the thread has grabbed the OM in `reenter_internal()` in case 2. In case of a suspension request, the thread exits the OM and enters it again once resumed. 

The JVMTI `waited` event posting (2nd one) is postponed until the suspended thread is resumed and has entered the OM again.  The `enter` to the OM (in case `ExitOnSuspend` did exit) is done without posting any events.

Tests are added for both scenarios. 

Tested in tiers 1 - 7.

-------------

Commit messages:
 - Merge remote-tracking branch 'origin/master' into JDK-8366659-OM-wait-suspend-deadlock
 - 8366659: Fixed new lines.
 - Merge remote-tracking branch 'origin/master' into JDK-8366659-OM-wait-suspend-deadlock
 - 8366659: Removed incorrect assert,
 - 8366659: Fixed merge conflict
 - 8366659: Fixed whitespace.
 - 8366659: Disabled posting JVMTI events in reenter-etner path of wait. Postponed waited event.
 - Merge remote-tracking branch 'origin/master' into JDK-8366659-OM-wait-suspend-deadlock
 - Merge remote-tracking branch 'origin/master' into JDK-8366659-OM-wait-suspend-deadlock
 - 8366659: Fixed whitespace.
 - ... and 5 more: https://git.openjdk.org/jdk/compare/7bb490c4...8b7d3f60

Changes: https://git.openjdk.org/jdk/pull/27040/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=27040&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8366659
  Stats: 374 lines in 3 files changed: 310 ins; 44 del; 20 mod
  Patch: https://git.openjdk.org/jdk/pull/27040.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/27040/head:pull/27040

PR: https://git.openjdk.org/jdk/pull/27040


More information about the hotspot-runtime-dev mailing list