-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8323782: Race: Thread::interrupt vs. AbstractInterruptibleChannel.begin #17444
8323782: Race: Thread::interrupt vs. AbstractInterruptibleChannel.begin #17444
Conversation
👋 Welcome back rrich! A progress list of the required criteria for merging this PR into |
Thanks for the test @AlanBateman |
@reinrich |
The new test LotsOfInterrupts.java hangs after a few repetitions (using jtreg's REPEAT_COUNT). With the fix it always terminates successfully. |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for finding this issue. It duplicates on JDK 8, and looking at the ancient history, it looks like it's been there since JDK 1.4 but just not noticed or diagnosed.
Reading the nioBlocker after setting the interrupt status is good.
There are about 20 tests for async interrupt of I/O ops in tier2. I see you've run tier1-4 so you've run them.
// Write interrupted before reading nioBlocker for correct synchronization. | ||
interrupted = true; | ||
interrupt0(); // inform VM of interrupt | ||
if (this != Thread.currentThread()) { | ||
// thread may be blocked in an I/O operation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the existing comment "thread may be blocked in an I/O operation" can move to before the if
statement so that it's a bit clearer than this is code for I/O operations.
@@ -1702,24 +1702,23 @@ public final void stop() { | |||
public void interrupt() { | |||
if (this != Thread.currentThread()) { | |||
checkAccess(); | |||
|
|||
} | |||
// Write interrupted before reading nioBlocker for correct synchronization. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd prefer if this said that it sets the interrupt status, and must be done before reading the nioBlocker.
@reinrich 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:
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 3 new commits pushed to the
Please see this link for an up-to-date comparison between the source branch of this pull request and the ➡️ To integrate this PR with the above commit message to the |
Thanks for your help Alan. |
@@ -0,0 +1,91 @@ | |||
/* | |||
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you should credit SAP here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alan wrote the test. I added him as contributor.
interrupted = true; | ||
interrupt0(); // inform VM of interrupt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is really safe/correct to move this outside the synchronized block? I know things have changed a bit with loom but we've "always" held a lock when doing the actual interrupt. I'd have to check the VM logic to be sure it can be called concurrently from multiple threads for the same target thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is really safe/correct to move this outside the synchronized block? I know things have changed a bit with loom but we've "always" held a lock when doing the actual interrupt. I'd have to check the VM logic to be sure it can be called concurrently from multiple threads for the same target thread.
This hasn't changed. The interruptLock is used to coordinate the add/remove of the nioBlocker. When there is no nioBlocker set then the interrupt status and unparking (as in JavaThread::interrupt) has always executed without the interruptLock (named "blockerLock" in the past).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that interrupting is just asynchronous to some extent.
E.g. a thread polls its interrupt status clearing it thereby (without lock) before calling nio. A concurrent interrupt can be lost then even if the lock is acquired.
(Maybe clearing should not be done by a public method)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep my bad on the VM side of things - no change there. But in the nioBlocker case doesn't this inherently make things more racy? Now maybe those races are allowed, but this might lead to a change in behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep my bad on the VM side of things - no change there. But in the nioBlocker case doesn't this inherently make things more racy? Now maybe those races are allowed, but this might lead to a change in behaviour.
I/O threads always check their interrupt status after installing the nioBlocker. The interrupter and the I/O thread (can be several) may race calling postInterrupt but that is okay.
I noticed that VirtualThread overrides jdk/src/java.base/share/classes/java/lang/VirtualThread.java Lines 902 to 905 in 05dad67
with just the same implementation as Thread has: jdk/src/java.base/share/classes/java/lang/Thread.java Lines 1741 to 1751 in 05dad67
This potentially hinders performance. Is there a reason to have this override? |
It was necessary at one point but no reason to now except to keep it close at the source level with the other methods that access the interrupt status. |
Do you want to keep it then or should I open an RFE to remove it? |
Maybe leave it for now as there is significant churn in this code right now, with more accumulating in the loom repo in advance of the monitors work. |
…actInterruptibleChannel_begin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for leaving this hanging. Change looks good.
Thanks
…actInterruptibleChannel_begin
Thanks for helping and reviewing. |
Going to push as commit 4018b2b.
Your commit was automatically rebased without conflicts. |
Set
interrupted
inThread::interrupt
before readingnioBlocker
for correct (Dekker scheme) synchronization with concurrent execution ofAbstractInterruptibleChannel::begin
.The change passed our CI functional testing: JTReg tests: tier1-4 of hotspot and jdk. All of Langtools and jaxp. SPECjvm2008, SPECjbb2015, Renaissance Suite, and SAP specific tests.
Testing was done with fastdebug and release builds on the main platforms and also on Linux/PPC64le and AIX.
Progress
Issue
Reviewers
Contributors
<alanb@openjdk.org>
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/17444/head:pull/17444
$ git checkout pull/17444
Update a local copy of the PR:
$ git checkout pull/17444
$ git pull https://git.openjdk.org/jdk.git pull/17444/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 17444
View PR using the GUI difftool:
$ git pr show -t 17444
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/17444.diff
Webrev
Link to Webrev Comment