[libbluray-devel] [Git][videolan/libbluray][master] BD-J: cancel leftover java.util.Timer threads when stopping an Xlet

Petri Hintukainen (@hpi) gitlab at videolan.org
Mon Nov 16 22:01:50 UTC 2026



Petri Hintukainen pushed to branch master at VideoLAN / libbluray


Commits:
46135354 by Martin Penkava at 2026-11-16T09:03:11+01:00
BD-J: cancel leftover java.util.Timer threads when stopping an Xlet

Some discs never cancel a java.util.Timer they create. Its thread ignores
interrupt(), so BDJThreadGroup.stopAll() waits out its timeout and, on
Java 20+ where ThreadGroup.stop() is unsupported, the 500 ms wait after
the failed kill as well.

Cancel such threads the way Timer.cancel() does before waiting.

- - - - -


3 changed files:

- src/libbluray/bdj/java-j2me/org/videolan/PortingHelper.java
- src/libbluray/bdj/java-j2se/org/videolan/PortingHelper.java
- src/libbluray/bdj/java/org/videolan/BDJThreadGroup.java


Changes:

=====================================
src/libbluray/bdj/java-j2me/org/videolan/PortingHelper.java
=====================================
@@ -27,6 +27,10 @@ public class PortingHelper {
     public static void stopThreadGroup(ThreadGroup t) {
     }
 
+    public static boolean cancelTimerThread(Thread t) {
+        return false;
+    }
+
     public static String dumpStack(Thread t) {
         return "";
     }


=====================================
src/libbluray/bdj/java-j2se/org/videolan/PortingHelper.java
=====================================
@@ -19,7 +19,9 @@
 
 package org.videolan;
 
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 
 public class PortingHelper {
 
@@ -47,6 +49,30 @@ public class PortingHelper {
         }
     }
 
+    /* java.util.Timer threads ignore interrupt(). Cancel like Timer.cancel(). */
+    public static boolean cancelTimerThread(Thread t) {
+        if (!"java.util.TimerThread".equals(t.getClass().getName())) {
+            return false;
+        }
+        try {
+            Field queueField = t.getClass().getDeclaredField("queue");
+            Field newTasksField = t.getClass().getDeclaredField("newTasksMayBeScheduled");
+            queueField.setAccessible(true);
+            newTasksField.setAccessible(true);
+            Object queue = queueField.get(t);
+            Method clear = queue.getClass().getDeclaredMethod("clear", new Class[0]);
+            clear.setAccessible(true);
+            synchronized (queue) {
+                newTasksField.setBoolean(t, false);
+                clear.invoke(queue, new Object[0]);
+                queue.notify();
+            }
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
     public static String dumpStack(Thread t) {
         StackTraceElement e[] = t.getStackTrace();
         if (e != null) {


=====================================
src/libbluray/bdj/java/org/videolan/BDJThreadGroup.java
=====================================
@@ -80,6 +80,7 @@ class BDJThreadGroup extends ThreadGroup {
     protected void stopAll(int timeout) {
 
         interrupt();
+        cancelTimers();
         waitForShutdown(timeout, 0);
 
         if (activeCount() > 0) {
@@ -102,6 +103,16 @@ class BDJThreadGroup extends ThreadGroup {
         }
     }
 
+    private void cancelTimers() {
+        Thread[] threads = new Thread[activeCount() + 1];
+        int count = enumerate(threads, true);
+        for (int i = 0; i < count; i++) {
+            if (PortingHelper.cancelTimerThread(threads[i])) {
+                logger.info("cancelled timer thread " + threads[i]);
+            }
+        }
+    }
+
     public void dumpThreads() {
         logger.info("Active threads in " + this + ":");
         Thread[] threads = new Thread[activeCount() + 1];



View it on GitLab: https://code.videolan.org/videolan/libbluray/-/commit/4613535451af402c6d72665e8cd88be1b29c9158

-- 
View it on GitLab: https://code.videolan.org/videolan/libbluray/-/commit/4613535451af402c6d72665e8cd88be1b29c9158
You're receiving this email because of your account on code.videolan.org. Manage all notifications: https://code.videolan.org/-/profile/notifications | Help: https://code.videolan.org/help




More information about the libbluray-devel mailing list