Code Monkey home page Code Monkey logo

Comments (12)

davidepianca98 avatar davidepianca98 commented on May 24, 2024 2

Thanks for letting me know, hopefully during the weekend

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 24, 2024

Hello, error 9 bad file descriptor, basically the socket has been closed. Are you still using the client after calling disconnect?

from kmqtt.

KirkBushman avatar KirkBushman commented on May 24, 2024

No I'm setting the client to null and cancelling the coroutines job right after that.

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 24, 2024

Can you please send a code snippet so that I can try to reproduce it?

from kmqtt.

KirkBushman avatar KirkBushman commented on May 24, 2024

@davidepianca98 Sure, I have an interface wrapping the client that has 2 methods: subscribe() / unsubscribe()

internal interface MyMqttClient {
    suspend fun subscribe(onReceive: (String) -> Unit)
    suspend fun unsubscribe()
}

here below is the rough implementation:

internal class MyMqttClientImpl {

    private var job: Job? = null
    private var client: MQTTClient? = null

    override suspend fun subscribe(onReceive: (String) -> Unit) {
        client = createMqttClient(onReceive)
        client?.let { client ->
            
            job = scope.launch {
                 try {
                     client.subscribe(listOf(Subscription("my_topic", SubscriptionOptions(Qos.EXACTLY_ONCE))))
                     client.run()
                 } catch (ex: Exception) {
                     // handle exception
                 }
           }
           job!!.start()
        }
    }
    override suspend fun unsubscribe() {
        client?.let { client ->
            client.unsubscribe(listOf("my_topic"))
            client.disconnect(ReasonCode.SUCCESS) // crashes here, doesn't crash without this line
        }

        job?.cancel()
        job = null

        client = null
    }

    private fun createMqttClient(onReceive: (String) -> Unit) {
        return MQTTClient(
            mqttVersion = MQTTVersion.MQTT5,
            address = "my_address",
            port = 1, // my port 
            clientId = "my_client_id",
            userName = "my_username",
            password = "my_password",
            tls = null,
        ) { publish ->
            // handle incoming events
        }
    }
}

After calling subscribe() and receiving some events, I just call unsubscribe() and get the crash.
Again, I wanna remind you that this above case works just fine on Android, but crashes on iOS

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 24, 2024

I was not able to reproduce the issue, but I think I have found what potentially can trigger the problem and should be fixed in commit 48cc990.
If you cannot wait for a new build to go out, you may try to either build yourself or do this modification to your code:

override suspend fun unsubscribe() {
      job?.cancel()
      job = null
      client?.let { client ->
          client.unsubscribe(listOf("my_topic"))
          client.disconnect(ReasonCode.SUCCESS) // crashes here, doesn't crash without this line
      }

      client = null
}

from kmqtt.

KirkBushman avatar KirkBushman commented on May 24, 2024

@davidepianca98 Thanks a lot 😃 we can wait for a build.

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 24, 2024

Also please let met know if it works without errors with the code change I sent in the last message

from kmqtt.

KirkBushman avatar KirkBushman commented on May 24, 2024

@davidepianca98 Once the release is out, I'll report back

from kmqtt.

KirkBushman avatar KirkBushman commented on May 24, 2024

@davidepianca98 We've tried the fix above and we still get the same issue :(
Do you have an ETA as to when you're going to publish a new release?

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 24, 2024

Please let me know if the new release is working for you

from kmqtt.

KirkBushman avatar KirkBushman commented on May 24, 2024

@davidepianca98 It seems it's working now on iOS :) thanks

from kmqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.