From f9114f780ddb10ec88d120591b62f2cf967de676 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 9 Mar 2021 13:10:40 +0100 Subject: [PATCH] [tcp] Ignore exceptions in reader thread if writer was terminated If we do not ignore the exception, then users may receive an exception via connectionClosedOnError() on connection termination. Those exceptions are typically unwanted if they are caused e.g. because the server does not send a closing stream tag. We previously ignored exceptions in this case already, but that behavior was changed with [1: 57961a8cc1f2]. This commit re-adds the behavior. 1: 57961a8cc1f2df6ecc1afa8c4f8460794d8d2dce Remove SynchronizationPoint --- .../smack/tcp/XMPPTCPConnection.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index aeb9d109b..c9884a3d0 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -1141,16 +1141,26 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { } } catch (Exception e) { - // The exception can be ignored if the the connection is 'done' - // or if the it was caused because the socket got closed. - if (!done) { - // Set running to false since this thread will exit here and notifyConnectionError() will wait until - // the reader and writer thread's 'running' value is false. - running = false; - // Close the connection and notify connection listeners of the - // error. - notifyConnectionError(e); + // Set running to false since this thread will exit here and notifyConnectionError() will wait until + // the reader and writer thread's 'running' value is false. + running = false; + + String ignoreReasonThread = null; + + boolean writerThreadWasShutDown = packetWriter.queue.isShutdown(); + if (writerThreadWasShutDown) { + ignoreReasonThread = "writer"; + } else if (done) { + ignoreReasonThread = "reader"; } + + if (ignoreReasonThread != null) { + LOGGER.log(Level.FINER, "Ignoring " + e + " as " + ignoreReasonThread + " was already shut down"); + return; + } + + // Close the connection and notify connection listeners of the error. + notifyConnectionError(e); } } }