From 2014f388b1f5eef66c8f787dc6889b03ae461d08 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 12 May 2016 21:54:42 +0200 Subject: [PATCH] interrupt XMPPConnection Thread in some cases the the DNS query might take too long (even though we specified a timeout) if that happens we need a secondary solution (besides killing the socket) to stop the thread --- .../java/eu/siacs/conversations/utils/DNSHelper.java | 7 ++++++- .../java/eu/siacs/conversations/xmpp/XmppConnection.java | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/utils/DNSHelper.java b/src/main/java/eu/siacs/conversations/utils/DNSHelper.java index b96d3c25..2f588f49 100644 --- a/src/main/java/eu/siacs/conversations/utils/DNSHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/DNSHelper.java @@ -51,14 +51,19 @@ public class DNSHelper { final String host = jid.getDomainpart(); final List servers = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDnsServers(context) : getDnsServersPreLollipop(); Bundle b = new Bundle(); + boolean interrupted = false; for(InetAddress server : servers) { + if (Thread.currentThread().isInterrupted()) { + interrupted = true; + break; + } b = queryDNS(host, server); if (b.containsKey("values")) { return b; } } if (!b.containsKey("values")) { - Log.d(Config.LOGTAG,"all dns queries failed. provide fallback A record"); + Log.d(Config.LOGTAG,(interrupted ? "Thread interrupted during DNS query" :"all dns queries failed") + ". provide fallback A record"); ArrayList values = new ArrayList<>(); values.add(createNamePortBundle(host, 5222, false)); b.putParcelableArrayList("values",values); diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 823fa0ce..3740097e 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -280,6 +280,10 @@ public class XmppConnection implements Runnable { final Bundle result = DNSHelper.getSRVRecord(account.getServer(), mXmppConnectionService); final ArrayListvalues = result.getParcelableArrayList("values"); for(Iterator iterator = values.iterator(); iterator.hasNext();) { + if (Thread.currentThread().isInterrupted()) { + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": Thread was interrupted"); + return; + } final Bundle namePort = (Bundle) iterator.next(); try { String srvRecordServer; @@ -1334,7 +1338,12 @@ public class XmppConnection implements Runnable { } } + public void interrupt() { + Thread.currentThread().interrupt(); + } + public void disconnect(final boolean force) { + interrupt(); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force="+Boolean.valueOf(force)); if (force) { forceCloseSocket();