Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalMonitorStateException if thread is interrupted during NatsConnection.publish() #1250

Open
davidmcote opened this issue Nov 7, 2024 · 0 comments
Labels
defect Suspected defect such as a bug or regression

Comments

@davidmcote
Copy link
Contributor

davidmcote commented Nov 7, 2024

Observed behavior

My application uses thread interruption to signal that work should be aborted. When a thread is interrupted before making a call to anything that tries to publish a message to a NATS Connection, the call to publish throws an IllegalMonitorStateException.

Here's an example exception stack from a call to KeyValue.create().

java.lang.IllegalMonitorStateException(null)
java.lang.IllegalMonitorStateException
    at java.base/java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:175)
    at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1007)
    at java.base/java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:494)
    at io.nats.client.impl.MessageQueue.push(MessageQueue.java:187)
    at io.nats.client.impl.MessageQueue.push(MessageQueue.java:139)
    at io.nats.client.impl.NatsConnectionWriter.queue(NatsConnectionWriter.java:233)
    at io.nats.client.impl.NatsConnection.queueOutgoing(NatsConnection.java:1678)
    at io.nats.client.impl.NatsConnection.publishInternal(NatsConnection.java:990)
    at io.nats.client.impl.NatsConnection.requestFutureInternal(NatsConnection.java:1339)
    at io.nats.client.impl.NatsConnection.requestInternal(NatsConnection.java:1229)
    at io.nats.client.impl.NatsJetStreamImpl.makeInternalRequestResponseRequired(NatsJetStreamImpl.java:242)
    at io.nats.client.impl.NatsJetStream.publishSyncInternal(NatsJetStream.java:155)
    at io.nats.client.impl.NatsJetStream.publish(NatsJetStream.java:83)
    at io.nats.client.impl.NatsKeyValue._write(NatsKeyValue.java:238)
    at io.nats.client.impl.NatsKeyValue.update(NatsKeyValue.java:189)
    at io.nats.client.impl.NatsKeyValue.create(NatsKeyValue.java:168)

Expected behavior

Calls to NATS should either:

  • (Strongly Preferred) Store the message into queueOutgoing and return successfully. Make no fuss about the calling thread being interrupted.
  • Propagate a different exception out and restore the thread's interrupted flag.
  • Propagate an InterruptedException out

Server and client version

Nats Server: 2.10.20
Nats Java client: 2.20.2

Host environment

Amazon Linux 2 within docker on c5a.2xlarge EC2 instance. Container limited to 2GiB RAM and 3vCPU.

Steps to reproduce

    @Test
    void testNatsIllegalStateMonitor() throws Exception {
        try (final Connection nc = Nats.connect()) {
            nc.publish("test", "test".getBytes()); // Successful

            Thread.currentThread().interrupt();

            nc.publish("test", "test".getBytes()); // throws java.lang.IllegalMonitorStateException
        }
    }
@davidmcote davidmcote added the defect Suspected defect such as a bug or regression label Nov 7, 2024
@davidmcote davidmcote changed the title Intermittent java.lang.IllegalMonitorStateException during NatsJetStream.publish() java.lang.IllegalMonitorStateException if thread is interrupted during NatsConnection.publish() Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Suspected defect such as a bug or regression
Projects
None yet
Development

No branches or pull requests

1 participant