make sure tag writer thread shuts down

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/xml/TagWriter.java       | 16 ++--
src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java |  3 
2 files changed, 10 insertions(+), 9 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/xml/TagWriter.java 🔗

@@ -13,15 +13,15 @@ import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
 public class TagWriter {
 
 	private OutputStreamWriter outputStream;
-	private boolean finshed = false;
+	private boolean finished = false;
 	private LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>();
+
 	private Thread asyncStanzaWriter = new Thread() {
-		private boolean shouldStop = false;
 
 		@Override
 		public void run() {
-			while (!shouldStop) {
-				if ((finshed) && (writeQueue.size() == 0)) {
+			while (!isInterrupted()) {
+				if (finished && writeQueue.size() == 0) {
 					return;
 				}
 				try {
@@ -29,7 +29,7 @@ public class TagWriter {
 					outputStream.write(output.toString());
 					outputStream.flush();
 				} catch (Exception e) {
-					shouldStop = true;
+					return;
 				}
 			}
 		}
@@ -73,7 +73,7 @@ public class TagWriter {
 	}
 
 	public TagWriter writeStanzaAsync(AbstractStanza stanza) {
-		if (finshed) {
+		if (finished) {
 			Log.d(Config.LOGTAG,"attempting to write stanza to finished TagWriter");
 			return this;
 		} else {
@@ -90,7 +90,7 @@ public class TagWriter {
 	}
 
 	public void finish() {
-		this.finshed = true;
+		this.finished = true;
 	}
 
 	public boolean finished() {
@@ -102,7 +102,7 @@ public class TagWriter {
 	}
 
 	public synchronized void forceClose() {
-		finish();
+		asyncStanzaWriter.interrupt();
 		if (outputStream != null) {
 			try {
 				outputStream.close();

src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java 🔗

@@ -219,6 +219,7 @@ public class XmppConnection implements Runnable {
 	protected synchronized void changeStatus(final Account.State nextStatus) {
 		if (Thread.currentThread().isInterrupted()) {
 			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": not changing status to "+nextStatus+" because thread was interrupted");
+			return;
 		}
 		if (account.getStatus() != nextStatus) {
 			if ((nextStatus == Account.State.OFFLINE)
@@ -454,7 +455,7 @@ public class XmppConnection implements Runnable {
 	 * Starts xmpp protocol, call after connecting to socket
 	 * @return true if server returns with valid xmpp, false otherwise
      */
-	private boolean startXmpp(Socket socket) throws Exception {
+	private synchronized boolean startXmpp(Socket socket) throws Exception {
 		if (Thread.currentThread().isInterrupted()) {
 			throw new InterruptedException();
 		}