Skip to content

Conversation

pchilano
Copy link
Contributor

@pchilano pchilano commented Oct 1, 2025

Please review the following fix. When blocking in ObjectMonitor::enter_internal we currently use timed-park for pinned virtual threads. This is done to alleviate some potential deadlocks cases where the successor is an unmounted virtual thread that cannot run. In particular this could happen during class loading/initialization if all other carriers are blocked waiting for the same class to be loaded/initialized.
This mechanism should be extended to cover ObjectMonitor::reenter_internal used in Object.wait (notification case). Also, the criteria to decide whether to do a timed-park should be based on whether there are unmounted vthreads already in the _entry_list, and not just if this is a pinned virtual thread. This covers mixed usages of the same ObjectMonitor between virtual threads and platform threads. This will become more relevant once we bring the changes currently in the fibers branch to preempt virtual threads during klass initialization.

These changes have been running in the loom pipeline for a couple of months already. I also added a new test case to test/jdk/java/lang/Thread/virtual/MonitorWaitNotify.java which deadlocks without these changes.

Thanks,
Patricio


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8369019: Improve timed-park mechanism in ObjectMonitor for virtual thread support (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27597/head:pull/27597
$ git checkout pull/27597

Update a local copy of the PR:
$ git checkout pull/27597
$ git pull https://git.openjdk.org/jdk.git pull/27597/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27597

View PR using the GUI difftool:
$ git pr show -t 27597

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27597.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 1, 2025

👋 Welcome back pchilanomate! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 1, 2025

@pchilano This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8369019: Improve timed-park mechanism in ObjectMonitor for virtual thread support

Reviewed-by: dholmes, alanb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 167 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Oct 1, 2025

@pchilano The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@pchilano
Copy link
Contributor Author

pchilano commented Oct 1, 2025

/label remove core-libs

@openjdk
Copy link

openjdk bot commented Oct 1, 2025

@pchilano
The core-libs label was successfully removed.

@pchilano pchilano marked this pull request as ready for review October 1, 2025 23:33
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 1, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 1, 2025

Webrevs

// all other carriers have a vthread pinned to it waiting for said class
// to be loaded/initialized.
// If there are unmounted virtual threads ahead in the _entry_list we want
// to do a timed-park instead to alleviate some deadlocks cases where one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// to do a timed-park instead to alleviate some deadlocks cases where one
// to do a timed-park instead to alleviate some deadlock cases where one

assert_mark_word_consistency();

// If there are unmounted virtual threads ahead in the _entry_list we want
// to do a timed-park instead to alleviate some deadlocks cases where one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// to do a timed-park instead to alleviate some deadlocks cases where one
// to do a timed-park instead to alleviate some deadlock cases where one

// _entry_list uses Atomic::cmpxchg() which already provides a fence that
// prevents this load from floating up previous store.
// Note that we can have false positives where timed-park is not necessary.
bool do_timed_parked = has_unmounted_vthreads();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we still only need the timed-park if the current thread is a pinned vthread?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, except if the monitor is also used in the context of a carrier thread. Currently there are only very few such cases and we disable preemption for them (e.g. interruptLock), so it’s likely not needed. With the upcoming changes to preempt on klass initialization, we could also have this situation if a class can be initialized both in the context of a carrier and a vthread. Since code executed in the context of the carriers is limited to library code there will also be very few cases of this, but I’ve seen at least one such case with LockSupport.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are saying the current code is insufficient and could still deadlock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there’s a monitor that is used both in the context of a virtual thread and the carriers then potentially yes. But this should almost never happen, and for the very few special cases we identified we currently disable preemption. So this is mostly to cover for the upcoming changes in case there is a class that can be initialized in the context of both a virtual thread and the carriers. Again this should also be a rare case but I’ve seen at least one case.

// the notifier in notify_internal.
// Note that we can have false positives where timed-park is not necessary.
bool do_timed_parked = has_unmounted_vthreads();
static int MAX_RECHECK_INTERVAL = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a constant? This is the same as the enter case, should there be only one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I moved it to a global static.

* paths and notification is done using notifyAll.
*/
@Test
void testMixedPinnedUnmounted() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think of testing timed-wait too? Some of the other tests are paramerized with a value source and some time values to test both untimed and timed waits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


/**
* Test no deadlock happens when Object.wait is called from a mix of pinned and non-pinned
* paths and notification is done using notifyAll.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point then maybe we should combine this with RetryMonitorEnterWhenPinned. I'm not suggesting we do this now but some of the expanded description might be useful to include here as a passing reader might not immediately know what this test is doing.

Copy link
Contributor Author

@pchilano pchilano Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn’t sure where to put the extra test and missed RetryMonitorEnterWhenPinned.java. I agree it makes more sense to have it there. Moved now, let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for moving to RetryMonitorEnterWhenPinned as it is similar to the test we had for enter. The update, and conversion to JUnit look good.

@openjdk
Copy link

openjdk bot commented Oct 6, 2025

@pchilano core-libs has been added to this pull request based on files touched in new commit(s).

// _entry_list uses Atomic::cmpxchg() which already provides a fence that
// prevents this load from floating up previous store.
// Note that we can have false positives where timed-park is not necessary.
bool do_timed_parked = has_unmounted_vthreads();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are saying the current code is insufficient and could still deadlock?

}

inline void ObjectMonitor::inc_unmounted_vthreads() {
assert(_unmounted_vthreads >= 0, "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(_unmounted_vthreads >= 0, "");
assert(_unmounted_vthreads >= 0, "invariant");

Here and below - thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay - thanks for clarifying.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 8, 2025
@pchilano
Copy link
Contributor Author

Thanks for the reviews and comments @dholmes-ora, @AlanBateman and @coleenp!
/integrate

@openjdk
Copy link

openjdk bot commented Oct 13, 2025

Going to push as commit 9feb8f2.
Since your change was applied there have been 178 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 13, 2025
@openjdk openjdk bot closed this Oct 13, 2025
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Oct 13, 2025
@openjdk openjdk bot removed the rfr Pull request is ready for review label Oct 13, 2025
@openjdk
Copy link

openjdk bot commented Oct 13, 2025

@pchilano Pushed as commit 9feb8f2.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants