diff --git a/src/main/java/io/nats/client/impl/MessageQueue.java b/src/main/java/io/nats/client/impl/MessageQueue.java index 0fc33e997..e1eedc78d 100644 --- a/src/main/java/io/nats/client/impl/MessageQueue.java +++ b/src/main/java/io/nats/client/impl/MessageQueue.java @@ -141,6 +141,7 @@ boolean push(NatsMessage msg) { boolean push(NatsMessage msg, boolean internal) { long start = System.currentTimeMillis(); + boolean lockWasSuccessful = false; try { /* This was essentially a Head-Of-Line blocking problem. @@ -166,6 +167,8 @@ boolean push(NatsMessage msg, boolean internal) { throw new IllegalStateException(OUTPUT_QUEUE_IS_FULL + queue.size()); } + lockWasSuccessful = true; + if (!internal && this.discardWhenFull) { return this.queue.offer(msg); } @@ -184,7 +187,9 @@ boolean push(NatsMessage msg, boolean internal) { return false; } finally { - editLock.unlock(); + if (lockWasSuccessful) { + editLock.unlock(); + } } }