JDK-8226806 : [macOS 10.14] Methods of Java Robot should be called from appropriate thread
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 8u202,11,12,13,14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • Submitted: 2019-06-24
  • Updated: 2021-09-14
  • Resolved: 2020-03-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 15 JDK 8 Other
11.0.8-oracleFixed 13.0.4Fixed 15 b16Fixed 8u291Fixed openjdk8u322Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
MacBook Pro, Intel Core i7 (64 bit) 
macOS Mojave 10.14.5
jdk-12.0.1.jdk / jdk-11.0.3.jdk / jdk1.8.0_212.jdk

A DESCRIPTION OF THE PROBLEM :
On recent macOS updates, like Mojave, running Java apps which uses Robot logs the following message to the Java error output:
yyyy-mm-dd hh:mm:ss.NNN java[NNN:NNN] pid(NNN)/euid(NNN) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!

It looks like currently on macOS Java Robot methods (jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m) work in some background thread, but it should be performed on Main Thread. 

So correctness of apps using Java Robot may possibly be affected.

For example, such message appears in Java error output during the following JDK jtreg tests run on macOS Mojave:

java/awt/event/KeyEvent/SwallowKeyEvents/SwallowKeyEvents.java
java/awt/KeyboardFocusmanager/ConsumeNextMnemonicKeyTypedTest/ConsumeForModalDialogTest/ConsumeForModalDialogTest.java
java/awt/KeyboardFocusmanager/TypeAhead/EnqueueWithDialogButtonTest/EnqueueWithDialogButtonTest.java

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run simple reproducer - NonMainThreadTest.java:

<javac> NonMainThreadTest.java
<java> NonMainThreadTest

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Empty java error output. 
Specifically, no following message in the java error output:

yyyy-mm-dd hh:mm:ss.NNN java[NNN:NNN] pid(NNN)/euid(NNN) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
ACTUAL -
---

> jdk1.8.0_202.jdk/Contents/Home/bin/java -showversion  NonMainThreadTest 

java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

Test start
2019-06-24 17:42:45.886 java[1950:42575] pid(1950)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2019-06-24 17:42:45.886 java[1950:42575] pid(1950)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2019-06-24 17:42:45.886 java[1950:42575] pid(1950)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2019-06-24 17:42:45.886 java[1950:42575] pid(1950)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
Test end

---

> jdk-11.0.3.jdk/Contents/Home/bin/java -showversion  NonMainThreadTest

java version "11.0.3" 2019-04-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.3+12-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.3+12-LTS, mixed mode)
Test start
2019-06-24 17:43:12.137 java[1952:42798] pid(1952)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2019-06-24 17:43:12.137 java[1952:42798] pid(1952)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2019-06-24 17:43:12.138 java[1952:42798] pid(1952)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2019-06-24 17:43:12.138 java[1952:42798] pid(1952)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
Test end

---

> jdk-12.0.1.jdk/Contents/Home/bin/java -showversion  NonMainThreadTest

java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
Test start
2019-06-24 17:43:27.710 java[1953:42931] pid(1953)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2019-06-24 17:43:27.710 java[1953:42931] pid(1953)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
...
2019-06-24 17:43:27.712 java[1953:42931] pid(1953)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
Test end

---

---------- BEGIN SOURCE ----------
---

import java.awt.Frame;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class NonMainThreadTest {

    public static void main(String[] args) throws Exception {
        System.out.println("Test start");

        Frame frame = new Frame();
        frame.setSize(100, 100);
        frame.setVisible(true);

        Robot robot = new Robot();
        robot.setAutoDelay(100);
        int anyKey = KeyEvent.VK_ESCAPE;
        robot.keyPress(anyKey);
        robot.keyRelease(anyKey);
        robot.waitForIdle();

        frame.dispose();

        System.out.println("Test end");
    }
}

---
---------- END SOURCE ----------

FREQUENCY : always



Comments
Fix Request (8u) I would like to backport this to jdk8u for parity with oracle's, also this fix is needed after recent addition of newer xcode support. Didn't apply clean, review thread: https://mail.openjdk.java.net/pipermail/jdk8u-dev/2021-August/014159.html https://mail.openjdk.java.net/pipermail/jdk8u-dev/2021-September/014270.html
10-09-2021

Fix request (13u) Requesting backport to 13u for parity with 11u, applies cleanly.
09-06-2020

Fix request (11u) -- will label after testing completed. I would like to downport this for parity with 11.0.8-oracle. Applies clean.
21-04-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/23dc143cca50 User: psadhukhan Date: 2020-03-22 11:49:29 +0000
22-03-2020

URL: https://hg.openjdk.java.net/jdk/client/rev/23dc143cca50 User: serb Date: 2020-03-17 02:48:42 +0000
18-03-2020

As per description, macOS Java Robot methods (jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m) work in background thread, but it should be performed on Main Thread. The issue seems to be restricted to macOS Mojave as it worked fine with macOS High Sierra. This still need to be checked with respective OS version 10.14. Attached test case to verify.
26-06-2019