From f232a6f4c9219cab431005ebacc7bda3cdcd64fb Mon Sep 17 00:00:00 2001 From: Diego Romar <18450339+doromaraujo@users.noreply.github.com> Date: Mon, 22 Sep 2025 08:58:41 -0300 Subject: [PATCH] Add check on current looper when adding dns to VPNService.builder --- .../java/io/netbird/client/tool/IFace.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/tool/src/main/java/io/netbird/client/tool/IFace.java b/tool/src/main/java/io/netbird/client/tool/IFace.java index 918d2ac..e41a280 100644 --- a/tool/src/main/java/io/netbird/client/tool/IFace.java +++ b/tool/src/main/java/io/netbird/client/tool/IFace.java @@ -104,25 +104,33 @@ class IFace implements TunAdapter { return; } - CountDownLatch latch = new CountDownLatch(1); + if (Looper.myLooper() != Looper.getMainLooper()) { + CountDownLatch latch = new CountDownLatch(1); - // ConnectivityManager must to run on the main thread instead of a Go routine - new Handler(Looper.getMainLooper()).post(() -> { - DNSWatch dnsWatch = new DNSWatch(vpnService); + // ConnectivityManager must to run on the main thread instead of a Go routine + new Handler(Looper.getMainLooper()).post(() -> { + addDnsServer(builder, dns); - if (!dnsWatch.isPrivateDnsActive()) { - builder.addDnsServer(dns); - } else { - Log.d(LOGTAG, "ignore DNS because private dns is active"); + latch.countDown(); + }); + + try { + latch.await(); // Will block the current thread until countDown() is called + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } + } else { + addDnsServer(builder, dns); + } + } - latch.countDown(); - }); + private void addDnsServer(VpnService.Builder builder, String dns) { + DNSWatch dnsWatch = new DNSWatch(vpnService); - try { - latch.await(); // Will block the current thread until countDown() is called - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + if (!dnsWatch.isPrivateDnsActive()) { + builder.addDnsServer(dns); + } else { + Log.d(LOGTAG, "ignore DNS because private dns is active"); } }