Skip to content
This repository was archived by the owner on Dec 31, 2025. It is now read-only.

NPE fix for closing with pending AckClosure(s) on Android#152

Open
ambientlabs wants to merge 1 commit intonats-io:masterfrom
ambientlabs:StreamingConnectionImplfix
Open

NPE fix for closing with pending AckClosure(s) on Android#152
ambientlabs wants to merge 1 commit intonats-io:masterfrom
ambientlabs:StreamingConnectionImplfix

Conversation

@ambientlabs
Copy link

Calling publish on an instance of StreamingConnection with an AckHandler can result in NPEs if the connection is closed before ACKs have been sent. The result should be an "java.lang.IllegalStateException: stan: connection closed"; however, the result in an NPE.

Note: I am working on Android using Kotlin.

  val opts: Options? = Options.Builder().natsUrl("nats://10.0.2.2:4222").clientId("android-client").clusterId("test-cluster").build()
        val cf = StreamingConnectionFactory(opts)
        val sc = cf.createConnection()

        // Note: Proving an AckHandler can result in NPEs when closing the StreamingConnection
        sc.publish("foo", "Hello World".toByteArray(), AckHandler { nuid, ex ->
            Log.i("Test", "Ack for nuid $nuid and ex $ex")
        })

        uiScope.launch {
            val r = Random()
            var count = 10
            while (count-- > 0) {
                var message = "Value: ${r.nextInt(10000)}"
                // Note: Proving an AckHandler can result in NPEs when closing the StreamingConnection
                sc.publish("foo", "Message $count: ${message}".toByteArray(), AckHandler { nuid, ex ->
                    Log.i("Test", "Ack for nuid $nuid and ex $ex")
                })

                delay(100)
                // Introduce an early close to generate an NPE rather than an IllegalStateException
                sc.close()
            }
            sc.close()
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant