From ed48367f5f1524ca9fa0833e51b1899a1866c2e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebastian=20K=C3=BCrten?= <sebastian@mobanisto.de>
Date: Sun, 12 Feb 2023 17:36:04 +0100
Subject: [PATCH] Avoid deadlock with MailboxClientManager

Solve the more general problem where a task already running on the event
executor schedules another task on the same executor and expects that to
finish asynchronously. With the single-threaded event executor this does
not work unless such tasks as immediately executed instead of being put
into the queue.
---
 .../briarproject/briar/desktop/DesktopCoreModule.kt  | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt
index c69e24bafe..5464f843ee 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/DesktopCoreModule.kt
@@ -79,6 +79,7 @@ import java.io.File
 import java.nio.file.Path
 import java.util.concurrent.Executor
 import javax.inject.Singleton
+import javax.swing.SwingUtilities.isEventDispatchThread
 
 // corresponding Briar Android class in
 // briar/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java
@@ -149,7 +150,16 @@ internal class DesktopCoreModule(
     @Provides
     @Singleton
     @UiExecutor
-    fun provideUiExecutor(): Executor = Dispatchers.Swing.asExecutor()
+    fun provideUiExecutor(): Executor {
+        val swingExecutor = Dispatchers.Swing.asExecutor()
+        return Executor { command ->
+            if (isEventDispatchThread()) {
+                command.run()
+            } else {
+                swingExecutor.execute(command)
+            }
+        }
+    }
 
     @Provides
     @Singleton
-- 
GitLab