From beb5269bffaf73fa0853fa53da8b55fc069ab5be Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 17 Sep 2026 14:40:02 +0000 Subject: [PATCH] Synchronize timer --- .../java/io/xpipe/app/util/GlobalTimer.java | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/util/GlobalTimer.java b/app/src/main/java/io/xpipe/app/util/GlobalTimer.java index 17a1ee484..1626bfce2 100644 --- a/app/src/main/java/io/xpipe/app/util/GlobalTimer.java +++ b/app/src/main/java/io/xpipe/app/util/GlobalTimer.java @@ -15,7 +15,7 @@ public class GlobalTimer { TIMER = new Timer("global-timer", true); } - public static void reset() { + public static synchronized void reset() { if (TIMER == null) { return; } @@ -30,10 +30,16 @@ public class GlobalTimer { public void run() { try { if (!s.get()) { - // Use this approach instead of scheduleAtFixedRate - // to prevent it from being run rapidly in case the timer is trying - // to catch up. For example with system hibernation - TIMER.schedule(createDelayedTask(interval, s), interval.toMillis()); + synchronized (GlobalTimer.class) { + if (TIMER == null) { + return; + } + + // Use this approach instead of scheduleAtFixedRate + // to prevent it from being run rapidly in case the timer is trying + // to catch up. For example with system hibernation + TIMER.schedule(createDelayedTask(interval, s), interval.toMillis()); + } } } catch (IllegalStateException e) { // The timer might be shutdown already @@ -46,23 +52,23 @@ public class GlobalTimer { } private static void schedule(TimerTask task, long delay) { - if (TIMER == null) { - return; - } - try { - TIMER.schedule( - new TimerTask() { - @Override - public void run() { - try { - task.run(); - } catch (Throwable t) { - ErrorEventFactory.fromThrowable(t).handle(); - } + synchronized (GlobalTimer.class) { + if (TIMER == null) { + return; + } + + TIMER.schedule(new TimerTask() { + @Override + public void run() { + try { + task.run(); + } catch (Throwable t) { + ErrorEventFactory.fromThrowable(t).handle(); } - }, - delay); + } + }, delay); + } } catch (IllegalStateException e) { // The timer might be shutdown already ErrorEventFactory.fromThrowable(e).omit().expected().handle();