Skip to content

8346952 : GetGraphicsStressTest.java fails: Native resources unavailable #25619

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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

anass-baya
Copy link
Contributor

@anass-baya anass-baya commented Jun 3, 2025

Analysis:

We are encountering a race condition in the native code. While retrieving the screen number by calling _getScreenImOn(), the window is disposed. As a result, the AWT-Windows event loop processes the Dispose() call, which triggers UnlinkObjects().
The race condition between the execution paths of these two native methods sometimes causes an exception

Proposed Fix:

While it's possible to introduce a synchronization mechanism, it would not offer any real benefit. The window will be disposed regardless, and we’ll fall back to the default screen. This behavior is already handled in WWindowPeer.java, where a workaround is in place to use the default device when getScreenImOn() returns a non-existent screen number

public void updateGC() {

    int scrn = getScreenImOn();

    if (screenLog.isLoggable(PlatformLogger.Level.FINER)) {
        log.finer("Screen number: " + scrn);
    }

    // get current GD
    Win32GraphicsDevice oldDev = winGraphicsConfig.getDevice();

    Win32GraphicsDevice newDev;
    GraphicsDevice[] devs = GraphicsEnvironment
        .getLocalGraphicsEnvironment()
        .getScreenDevices();

    // Occasionally during device addition/removal getScreenImOn can return
    // a non-existing screen number. Use the default device in this case.
    if (scrn >= devs.length) {
        newDev = (Win32GraphicsDevice) GraphicsEnvironment
            .getLocalGraphicsEnvironment().getDefaultScreenDevice();
    } else {
        newDev = (Win32GraphicsDevice) devs[scrn];
    }
}

Therefore, I propose modifying the native method getScreenImOn to return the default device if the peer is being disposed :

jint AwtWindow::_GetScreenImOn(void *param)
{
...
    jboolean destroyed = JNI_GET_DESTROYED(self);
    if (destroyed == JNI_TRUE){
        env->DeleteGlobalRef(self);
        return AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
    }
...

Tests Summary:

GetGraphicsStressTest (existing test):

    Fails intermittently without the fix

    Consistently passes with the fix

NotifyStressTest (newly added test):

     Consistently fails without the fix

     Consistently passes with the fix

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-8346952: GetGraphicsStressTest.java fails: Native resources unavailable (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25619

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 3, 2025

👋 Welcome back abaya! 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 Jun 3, 2025

@anass-baya 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:

8346952: GetGraphicsStressTest.java fails: Native resources unavailable

Reviewed-by: serb

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 229 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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@mrserb) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk
Copy link

openjdk bot commented Jun 3, 2025

@anass-baya The following label will be automatically applied to this pull request:

  • client

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.

@anass-baya anass-baya changed the title Draft 8346952 : GetGraphicsStressTest.java fails: Native resources unavailable 8346952 : GetGraphicsStressTest.java fails: Native resources unavailable Jun 3, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 3, 2025
@mlbridge
Copy link

mlbridge bot commented Jun 3, 2025

@anass-baya anass-baya requested review from prrace and mrserb June 10, 2025 22:14
return 0;
}
// Return the default screen.
JNI_CHECK_PEER_GOTO(self, ret);
Copy link
Member

@mrserb mrserb Jun 10, 2025

Choose a reason for hiding this comment

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

I suggest reordering it slightly - this pattern is commonly used in most cases where JNI_CHECK_PEER_GOTO is used:

    result...;
    AwtWindow *window = NULL;

    PDATA pData;
    JNI_CHECK_PEER_GOTO(self, ret);
    window = (AwtWindow *)pData;

Copy link
Member

Choose a reason for hiding this comment

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

I still suggest this order.

Also, please add the new bug ID to the GetGraphicsStressTest.
btw I'm still looking into the new test - it always passes for me. Can we tweak it to reproduce the bug more reliably?

Copy link
Contributor Author

@anass-baya anass-baya Jun 11, 2025

Choose a reason for hiding this comment

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

I cant reproduce it locally.
I can see it only on CI 20/20 with the new test and 2/20 with the old one

Copy link
Member

Choose a reason for hiding this comment

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

what is the stack trace when the new test fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the same stack as the old test. However let me try to enhance it and come back to you

Copy link
Contributor Author

@anass-baya anass-baya Jun 17, 2025

Choose a reason for hiding this comment

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

Hello @mrserb,
I've enhanced the new test. It now reproduces the issue consistently (5/5) locally, but only intermittently (6/20) on CI.
Can we proceed like that ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @mrserb,
What do you think about the test? Does it look good to you?
KR,

Copy link
Member

Choose a reason for hiding this comment

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

looking into it

Copy link
Member

Choose a reason for hiding this comment

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

It is unclear from the diff, but does the PDATA pData have additional space at the beginning?

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. it's handled now

@anass-baya anass-baya requested a review from mrserb June 11, 2025 00:09
@openjdk openjdk bot removed the rfr Pull request is ready for review label Jun 11, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 12, 2025
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 10; i++) {
f.removeNotify();
f.addNotify();
Copy link
Member

Choose a reason for hiding this comment

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

Note that we call addNotify last, so the frame will not be disposed at the end of the test.
btw, I cannot reproduce the bug even with the updated test. If you look at the stack trace, it seems the error comes from displayChanged, which is not triggered by the test itself

Copy link
Member

Choose a reason for hiding this comment

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

you can use this test:

/*
 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.Window;

/**
 * @test
 * @bug 8346952
 * @key headful
 */
public final class BogusFocusableWindowState {

    public static void main(String[] args) {
        Window frame = new Window(null) {
            @Override
            public boolean getFocusableWindowState() {
                removeNotify();
                return true;
            }
        };
        try {
            frame.pack();
            frame.setVisible(true);
        } finally {
            frame.dispose();
        }
    }
}

* @key headful
*/

public final class BogusFocusableWindowState {
Copy link
Member

Choose a reason for hiding this comment

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

please split the long line to 80 chars, also the empty line above is not really necessary.

Copy link
Contributor Author

@anass-baya anass-baya Jun 26, 2025

Choose a reason for hiding this comment

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

hello @mrserb,
Of course, it's done. Thank you.
For my proposed test, it does trigger the same exception call stack.
However, yours is better. The reproduction frequency with it is 20/20.

@openjdk openjdk bot removed the rfr Pull request is ready for review label Jun 26, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 26, 2025
@anass-baya anass-baya requested a review from mrserb June 26, 2025 22:06
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 30, 2025
@anass-baya
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jun 30, 2025
@openjdk
Copy link

openjdk bot commented Jun 30, 2025

@anass-baya
Your change (at version e458e6e) is now ready to be sponsored by a Committer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client [email protected] ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored
Development

Successfully merging this pull request may close these issues.

3 participants