RFR: 8346952 : GetGraphicsStressTest.java fails: Native resources unavailable [v2]

Anass Baya abaya at openjdk.org
Mon Jun 9 20:33:49 UTC 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

Anass Baya has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains six additional commits since the last revision:

 - Merge branch 'openjdk:master' into JDK-8346952
 - Handle white spaces
 - remove extra-space
 - Update copyright
 - Return the default device if the peer is beeing disposed
 - Add Test for JDK-8346952

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/25619/files
  - new: https://git.openjdk.org/jdk/pull/25619/files/268e79c1..78319571

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=25619&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=25619&range=00-01

  Stats: 52807 lines in 939 files changed: 27588 ins; 15699 del; 9520 mod
  Patch: https://git.openjdk.org/jdk/pull/25619.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/25619/head:pull/25619

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


More information about the client-libs-dev mailing list