srowen commented on a change in pull request #33947:
URL: https://github.com/apache/spark/pull/33947#discussion_r706104756



##########
File path: common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java
##########
@@ -77,28 +67,52 @@
       cleanerClassName = "jdk.internal.ref.Cleaner";
     }
     try {
-      Class<?> cleanerClass = Class.forName(cleanerClassName);
-      Method createMethod = cleanerClass.getMethod("create", Object.class, 
Runnable.class);
-      // Accessing jdk.internal.ref.Cleaner should actually fail by default in 
JDK 9+,
-      // unfortunately, unless the user has allowed access with something like
-      // --add-opens java.base/java.lang=ALL-UNNAMED  If not, we can't really 
use the Cleaner
-      // hack below. It doesn't break, just means the user might run into the 
default JVM limit
-      // on off-heap memory and increase it or set the flag above. This tests 
whether it's
-      // available:
+      Class<?> cls = Class.forName("java.nio.DirectByteBuffer");
+      Constructor<?> constructor = cls.getDeclaredConstructor(Long.TYPE, 
Integer.TYPE);
+      Field cleanerField = cls.getDeclaredField("cleaner");
       try {
-        createMethod.invoke(null, null, null);
-      } catch (IllegalAccessException e) {
-        // Don't throw an exception, but can't log here?
-        createMethod = null;
-      } catch (InvocationTargetException ite) {
-        // shouldn't happen; report it
-        throw new IllegalStateException(ite);
+        constructor.setAccessible(true);
+        cleanerField.setAccessible(true);
+      } catch (RuntimeException re) {
+        // This is a Java 9+ exception, so needs to be handled without 
importing it
+        if 
("InaccessibleObjectException".equals(re.getClass().getSimpleName())) {
+          // Continue, but the constructor/field are not available
+          // See comment below for more context
+          constructor = null;
+          cleanerField = null;

Review comment:
       Ugh, yeah I made a basic error here - needs to be 'else throw re'. 
   I should also say I don't know how this arises, so can't test for it. It 
came up on the list and seems therefore possible, but also didn't come up 
before in testing




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to