-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
8297923: java.awt.ScrollPane broken after multiple scroll up/down #14338
Conversation
👋 Welcome back aivanov! A progress list of the required criteria for merging this PR into |
@aivanov-jdk The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
@@ -701,7 +701,7 @@ Java_sun_awt_windows_WScrollPanePeer_getOffset(JNIEnv *env, jobject self, | |||
gos->scrollpane = env->NewGlobalRef(self); | |||
gos->orient = orient; | |||
|
|||
return static_cast<jint>(reinterpret_cast<INT_PTR>(AwtToolkit::GetInstance().SyncCall( | |||
return static_cast<jint>(reinterpret_cast<INT_PTR>(AwtToolkit::GetInstance().InvokeFunction( |
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.
So this needs to block until the toolkit thread can process this and return, but since its
directly called from Java (ie we are in a JNI method) I think this is likely fine.
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.
That's right.
In addition to that, WScrollPanePeer.getScrollOffset
is unused, so it's never called.
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.
If it is unused can we delete it?
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.
If it is unused can we delete it?
It's possible. Yet I'd rather leave it for another time.
There's also a specific WM_AWT_SET_SCROLL_INFO
message to call ::SetScrollInfo
. This message is never used, it can also be removed.
@@ -742,7 +742,7 @@ Java_sun_awt_windows_WScrollPanePeer_setScrollPosition(JNIEnv *env, | |||
ssps->x = x; | |||
ssps->y = y; | |||
|
|||
AwtToolkit::GetInstance().SyncCall(AwtScrollPane::_SetScrollPos, ssps); | |||
AwtToolkit::GetInstance().InvokeFunctionLater(AwtScrollPane::_SetScrollPos, ssps); |
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.
Whereas in the _GetOffset case above, you clearly need to wait until the result is returned - I guess
you didn't see a need to block here ? Or the case below ?
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 had a similar clarification as to why it was InvokeFunction
for _getOffset
and InvokeFunctionLater
for _SetScrollPos
and _SetSpans
. The above discussion makes it clear.
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.
No, there's no need to wait, and no result is returned.
Java has updated the position of the child component based on the new position of the scroll bar. Now the position of the non-client scroll bars of the scroll pane window need to be updated.
The _SetSpans
recalculates the position and range of the scroll bars if the size of the scroll pane or its child component changes. The changes are already handled on the Java side, these changes need to be reflected in the scroll bars.
I have found no scenario where we need to block EDT.
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.
The screenshot of the test case after the problem is reproduced:
If you scroll up or down, the left side of the frame gets filled with the light-green color of the canvas which is the child component inside the scroll pane.
Test works as expected with the fix.
Without the fix, OutOfMemory exception is thrown as described.
When running the test without the fix, I had to introduce a small delay after the frame is maximized again to see the light-green block outside the canvas.
You can use the test case attached to JDK-8297923, |
@aivanov-jdk 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 46 new commits pushed to the
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 |
I wonder if we have any other leaks due to similar thread mis-usage ? |
Probably we should update the Java_sun_awt_windows_WScrollbarPeer_setValues as well? it calls the SetScrollInfo |
You're right. It needs to be updated too. However, I'd rather do it separately. |
That is up2you, but it looks strange when the patch updates the code which is never executed and skips the code which is executed and has the bug |
It has taken a very long time till the root cause for this bug was identified. I admit I was too focused on the code for As such, the updated code is limited to the native implementation of Thank you for finding a similar problem in |
/integrate |
Going to push as commit ea41907.
Your commit was automatically rebased without conflicts. |
@aivanov-jdk Pushed as commit ea41907. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
I have submitted the follow-up bugs.
JDK-8315690: Call ::SetScrollInfo on toolkit thread in awt.ScrollBar
JDK-8315691: Remove unused WScrollPanePeer.getScrollOffset method
JDK-8315693: Remove WM_AWT_SET_SCROLL_INFO message |
@prrace Potentially, all the usages of jdk/src/java.desktop/windows/native/libawt/windows/awt_Toolkit.cpp Lines 1870 to 1878 in ed2b467
It does not even synchronise the threads. So, there could be other leaks or issues with memory visibility. I think this scenario led to leaks because it also modified the position of the horizontal scroll bar, which started animation provided by visual styles. The animation never got a chance to complete before another update came. The issue wasn't reproducible if there was only vertical scroll bar which was being dragged. |
Problem description
If you grab the thumb of the scroll bar of
ScrollPane
and drag it slowly and continuously up and down, you'll notice the UI stops rendering correctly: the child component of the scroll pane will render on the left of the frame itself, the inside of the scroll pane will be filled with the background color of its child component.Root cause
AWT calls the
::SetScrollInfo
function on EDT when AWT processes scroll bar tracking events. This Windows API is not thread-safe, calling this function on an incorrect thread leads to leaking GDI objects.When the process reaches the limit on the number of GDI objects, it cannot create new GDI objects, which results in rendering issues.
Fix
To resolve the problem, I modified the code so that
::SetScrollInfo
and::GetScrollInfo
are always called on the toolkit thread, all AWT components are created on the toolkit thread.An automatic test is provided. The test scrolls the vertical scroll bar up and down. Then the test takes a screenshot. When the bug is reproduced, the robot cannot create new GDI objects to capture the screenshot and it throws
OutOfMemoryError
.Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/14338/head:pull/14338
$ git checkout pull/14338
Update a local copy of the PR:
$ git checkout pull/14338
$ git pull https://git.openjdk.org/jdk.git pull/14338/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 14338
View PR using the GUI difftool:
$ git pr show -t 14338
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/14338.diff
Webrev
Link to Webrev Comment