Skip to content
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

Java + Windows RDP disconnect causes Java Headless Exception #1203

Open
1 task done
michael-newsrx opened this issue Dec 9, 2024 · 7 comments
Open
1 task done

Java + Windows RDP disconnect causes Java Headless Exception #1203

michael-newsrx opened this issue Dec 9, 2024 · 7 comments
Labels
bug Something isn't working jbs:reported Someone from our org has reported it to OpenJDK jbs-needs-backport-to-21

Comments

@michael-newsrx
Copy link

Please provide a brief summary of the bug

I've also posted this at stackoverflow in case someone there can provide a short term work around. https://stackoverflow.com/q/79265762/1341731

Starting with Java 17.0.12 and 21.0.4 Java fails with a Headless exception when running a full GUI app via RDP and the RDP session is disconnected. The remote desktop is running and can be reconnected with. All non-Java apps are still running and updating their UI's just fine. It is only Java GUI apps that are crashing.

If the Java program attempts to do GUI operations such as create a modal progress bar while the RDP session is disconnected, a Headless exception is thrown, even though the environment is not headless, just disconnected.

This seems to be related to https://bugs.openjdk.org/browse/JDK-8340992 and https://bugs.openjdk.org/browse/JDK-8336862 but there are no workarounds provided.

Is there a workaround that can be done runtime?

The only workarounds we can come up with are:

  1. Rewrite everything in another language. Not a small undertaking.
  2. Downgrade all Java/JDK instances to 21.0.3 and not apply any security updates.
  3. Lose all work and start over for any RDP disconnects caused by intentional user disconnect, internet issues, etc. This is where we are at now and it is causing production issues.

This would also seem to be breaking of the "contract" where minor point releases are not supposed to cause breaking changes.

We mostly use simple apps composed of JFrames, JTextComponents, and ProgressMonitor with a couple of self-hosted JediTerm applications.

Did you test with the latest update version?

  • Yes

Please provide steps to reproduce where possible

Create an app with opens and closes windows with time delays.
Start the app on Windows in an RDP session.
Disconnect from the RDP session.
Wait till the app should have opened and closed any test windows.
Reconnect to the RDP session.

Expected Results

The app to create UI components in the disconnected RDP session, just like all other non-Java apps are capable of.

Actual Results

GUI apps should keep running when a remote desktop session is disconnected. They should not suddenly fail reporting that GUI operations are not possible.

What Java Version are you using?

Picked up JAVA_TOOL_OPTIONS: -Xmx6144m -Dlog4j2.formatMsgNoLookups=true openjdk version "21.0.5" 2024-10-15 LTS OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode, sharing) PS C:\Users\admin.NEWSRX>

What is your operating system and platform?

Windows specifications:

  • Edition: Windows 10 Pro
  • Version: 22H2
  • Installed on: 2021-04-23
  • OS build: 19045.5131

How did you install Java?

  • AdoptOpenJDK

https://github.com/tushev/aojdk-updatewatcher/wiki

Did it work before?

Works with version 21.0.3

Did you test with other Java versions?

Every point release after 21.0.3 that AdoptOpenJDK reported in need of updating.

Relevant log output

No response

@michael-newsrx michael-newsrx added the bug Something isn't working label Dec 9, 2024
@michael-newsrx
Copy link
Author

Can this issue be monkey-patched?

@michael-newsrx
Copy link
Author

Code demonstrating issue:

import javax.swing.*;
import java.awt.*;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) throws InterruptedException {
        var delay = 10;
        var isHeadless = GraphicsEnvironment.isHeadless();
        System.out.printf("1) Is headless: %s.%n", isHeadless);
        System.out.printf("Sleeping %,d seconds.%n", delay);
        System.out.printf("""
                DISCONNECT YOUR REMOTE DESKTOP CONNECTION NOW!
                RECONNECT AFTER %,d SECONDS.
                """, delay);
        Thread.sleep(delay * 1_000);
        System.out.println("Drawing progress bar");
        ProgressMonitor p = new ProgressMonitor(null, "Test progressbar", "My note", 0, 100);
        for (int i = 0; i < 100; i++) {
            p.setProgress(i);
            Thread.sleep(250);
        }
        isHeadless = GraphicsEnvironment.isHeadless();
        System.out.printf("2) Is headless: %s.%n", isHeadless);

        //TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
        // to see how IntelliJ IDEA suggests fixing it.
        System.out.printf("Hello and welcome!");

        for (int i = 1; i <= 5; i++) {
            //TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
            // for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
            System.out.println("i = " + i);
        }
    }
}

Error log:

"C:\Program Files\Eclipse Adoptium\jdk-21.0.5.11-hotspot\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.1\lib\idea_rt.jar=59072:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\admin.NEWSRX\git\rdp-crashes\out\production\rdp-crashes Main
Picked up JAVA_TOOL_OPTIONS: -Xmx6144m -Dlog4j2.formatMsgNoLookups=true
1) Is headless: false.
Sleeping 10 seconds.
DISCONNECT YOUR REMOTE DESKTOP CONNECTION NOW!
RECONNECT AFTER 10 SECONDS.
Drawing progress bar
Exception in thread "main" java.awt.AWTError: no screen devices
	at java.desktop/sun.awt.Win32GraphicsEnvironment.getDefaultScreenDevice(Win32GraphicsEnvironment.java:101)
	at java.desktop/sun.font.FontDesignMetrics.getDefaultFrc(FontDesignMetrics.java:157)
	at java.desktop/sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:274)
	at java.desktop/sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1242)
	at java.desktop/javax.swing.JComponent.getFontMetrics(JComponent.java:1700)
	at java.desktop/javax.swing.plaf.basic.BasicGraphicsUtils.getPreferredButtonSize(BasicGraphicsUtils.java:360)
	at java.desktop/javax.swing.plaf.basic.BasicButtonUI.getPreferredSize(BasicButtonUI.java:542)
	at java.desktop/javax.swing.plaf.basic.BasicButtonUI.getMinimumSize(BasicButtonUI.java:532)
	at java.desktop/javax.swing.JComponent.getMinimumSize(JComponent.java:1814)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.addButtonComponents(BasicOptionPaneUI.java:818)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.createButtonArea(BasicOptionPaneUI.java:751)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.installComponents(BasicOptionPaneUI.java:206)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.installUI(BasicOptionPaneUI.java:160)
	at java.desktop/javax.swing.JComponent.setUI(JComponent.java:740)
	at java.desktop/javax.swing.JOptionPane.setUI(JOptionPane.java:1858)
	at java.desktop/javax.swing.JOptionPane.updateUI(JOptionPane.java:1880)
	at java.desktop/javax.swing.JOptionPane.<init>(JOptionPane.java:1845)
	at java.desktop/javax.swing.ProgressMonitor$ProgressOptionPane.<init>(ProgressMonitor.java:161)
	at java.desktop/javax.swing.ProgressMonitor.setProgress(ProgressMonitor.java:291)
	at Main.main(Main.java:20)

Process finished with exit code 1

@jerboaa
Copy link

jerboaa commented Dec 9, 2024

Looks to be https://bugs.openjdk.org/browse/JDK-8336862

Does setting -Djava.awt.headless=false help?

@michael-newsrx
Copy link
Author

No this does not help.

"C:\Program Files\Eclipse Adoptium\jdk-21.0.5.11-hotspot\bin\java.exe" -Djava.awt.headless=false "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.1\lib\idea_rt.jar=59410:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\admin.NEWSRX\git\rdp-crashes\out\production\rdp-crashes Main
Picked up JAVA_TOOL_OPTIONS: -Xmx6144m -Dlog4j2.formatMsgNoLookups=true
1) Is headless: false.
Sleeping 10 seconds.
DISCONNECT YOUR REMOTE DESKTOP CONNECTION NOW!
RECONNECT AFTER 10 SECONDS.
Drawing progress bar
Exception in thread "main" java.awt.AWTError: no screen devices
	at java.desktop/sun.awt.Win32GraphicsEnvironment.getDefaultScreenDevice(Win32GraphicsEnvironment.java:101)
	at java.desktop/sun.font.FontDesignMetrics.getDefaultFrc(FontDesignMetrics.java:157)
	at java.desktop/sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:274)
	at java.desktop/sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1242)
	at java.desktop/javax.swing.JComponent.getFontMetrics(JComponent.java:1700)
	at java.desktop/javax.swing.plaf.basic.BasicGraphicsUtils.getPreferredButtonSize(BasicGraphicsUtils.java:360)
	at java.desktop/javax.swing.plaf.basic.BasicButtonUI.getPreferredSize(BasicButtonUI.java:542)
	at java.desktop/javax.swing.plaf.basic.BasicButtonUI.getMinimumSize(BasicButtonUI.java:532)
	at java.desktop/javax.swing.JComponent.getMinimumSize(JComponent.java:1814)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.addButtonComponents(BasicOptionPaneUI.java:818)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.createButtonArea(BasicOptionPaneUI.java:751)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.installComponents(BasicOptionPaneUI.java:206)
	at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.installUI(BasicOptionPaneUI.java:160)
	at java.desktop/javax.swing.JComponent.setUI(JComponent.java:740)
	at java.desktop/javax.swing.JOptionPane.setUI(JOptionPane.java:1858)
	at java.desktop/javax.swing.JOptionPane.updateUI(JOptionPane.java:1880)
	at java.desktop/javax.swing.JOptionPane.<init>(JOptionPane.java:1845)
	at java.desktop/javax.swing.ProgressMonitor$ProgressOptionPane.<init>(ProgressMonitor.java:161)
	at java.desktop/javax.swing.ProgressMonitor.setProgress(ProgressMonitor.java:291)
	at Main.main(Main.java:20)

Process finished with exit code 1

@michael-newsrx
Copy link
Author

Hrmmmm... just tried using JetBrains JDK in Intellij version 21.0.5 and the problem does not occur.

@michael-newsrx
Copy link
Author

michael-newsrx commented Dec 13, 2024

OK

So we've downgraded all the physical desktop systems to 21.0.3 and RDP disconnects no longer cause Java to crash.

As we use AdoptOpenJDK, we told it to skip the current 21.0.5 release for updating.

-Mike/NewsRx

@mjschwaiger
Copy link

mjschwaiger commented Dec 16, 2024

Hrmmmm... just tried using JetBrains JDK in Intellij version 21.0.5 and the problem does not occur.

The reason why this works in JetBrains Runtime (JBR) is because the problem has been "solved" directly in JBR.
The "fix" was to undo the changes from JDK-8336862. See JBR-7683 and commit bb54739.
Seems to work again with JBR 21.0.5 b709 and later.

However, there are no changes and no proposals for the OpenJDK at the present time.

Currently, the only known workaround is to stick with Java 21.0.3 (Java 17.0.11). However, as it does not provide fixes for new vulnerabilities and other issues, it is not a long-term solution.

@karianna karianna added jbs:reported Someone from our org has reported it to OpenJDK jbs-needs-backport-to-21 labels Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working jbs:reported Someone from our org has reported it to OpenJDK jbs-needs-backport-to-21
Projects
None yet
Development

No branches or pull requests

4 participants