From bfff4121126d9a945ee8e8d50dd5891b68c0e731 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 7 Apr 2020 23:10:35 +0200 Subject: [PATCH] tcp: increase unack'ed stanza queue size, decrease ack request limit To reduce the chances of a deadlock between read and writer if SM unacked stanza queue is full. See SMACK-881. --- .../org/jivesoftware/smack/tcp/XMPPTCPConnection.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 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 068dc867d..47452a11a 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 @@ -1152,6 +1152,8 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { protected class PacketWriter { public static final int QUEUE_SIZE = XMPPTCPConnection.QUEUE_SIZE; + public static final int UNACKKNOWLEDGED_STANZAS_QUEUE_SIZE = 1024; + public static final int UNACKKNOWLEDGED_STANZAS_QUEUE_SIZE_HIGH_WATER_MARK = (int) (0.3 * UNACKKNOWLEDGED_STANZAS_QUEUE_SIZE); private final String threadName = "Smack Writer (" + getConnectionCounter() + ')'; @@ -1337,7 +1339,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { // The client needs to add messages to the unacknowledged stanzas queue // right after it sent 'enabled'. Stanza will be added once // unacknowledgedStanzas is not null. - unacknowledgedStanzas = new ArrayBlockingQueue<>(QUEUE_SIZE); + unacknowledgedStanzas = new ArrayBlockingQueue<>(UNACKKNOWLEDGED_STANZAS_QUEUE_SIZE); } maybeAddToUnacknowledgedStanzas(packet); @@ -1439,9 +1441,9 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { // packet order is not stable at this point (sendStanzaInternal() can be // called concurrently). if (unacknowledgedStanzas != null && stanza != null) { - // If the unacknowledgedStanza queue is nearly full, request an new ack + // If the unacknowledgedStanza queue reaching its high water mark, request an new ack // from the server in order to drain it - if (unacknowledgedStanzas.size() == 0.8 * XMPPTCPConnection.QUEUE_SIZE) { + if (unacknowledgedStanzas.size() == UNACKKNOWLEDGED_STANZAS_QUEUE_SIZE_HIGH_WATER_MARK) { writer.write(AckRequest.INSTANCE.toXML().toString()); writer.flush(); }