stan.java icon indicating copy to clipboard operation
stan.java copied to clipboard

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

Open ambientlabs opened this issue 4 years ago • 0 comments

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()
        }

ambientlabs avatar Jun 08 '20 20:06 ambientlabs