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

Disconnect an mqtt client from an isolate #4

Closed
Fianax opened this issue Sep 9, 2021 · 12 comments
Closed

Disconnect an mqtt client from an isolate #4

Fianax opened this issue Sep 9, 2021 · 12 comments

Comments

@Fianax
Copy link

Fianax commented Sep 9, 2021

Hi, I have created a client on an isolate. When I do worker.dispose (), the client keeps receiving data from the mqtt server until it disconnects (when receiving a lot of data, even though I kill the worker, I keep receiving the data that has been queued, so when I kill the worker I keep receiving data).

But I would like to be able to disconnect him, without waiting for him to disconnect. Can I send a message to the worker to disconnect it? How does the worker work when I send it several messages, I mean, does the isolate method run again?

Sorry for the inconvenience.

@lubritto
Copy link
Owner

lubritto commented Sep 9, 2021

Hey @Fianax, are you using the latest version? The Mqtt is being instantiated and used inside the isolate handler or on the main thread?

@lubritto
Copy link
Owner

lubritto commented Sep 9, 2021

@Fianax I'm currently using the MQTT client inside the worker without any problem, but maybe the isolate is not being killed because it is still processing a message. I added an immediate option to the dispose method that will kill the isolate without necessarily awaiting the current operation finish. Can you try using the dispose(immediate: true)? This option is present in the version 1.3.0

@Fianax
Copy link
Author

Fianax commented Sep 10, 2021

Hola @Fianax , ¿estás usando la última versión? ¿Se está creando una instancia del Mqtt y se está utilizando dentro del controlador de aislamiento o en el hilo principal?

The mqtt client created it inside the isolate because as the isolates do not share memory with the main thread and I cannot pass the instance that it had created in another screen. That is why I create it within the isolate itself and I pass it the ip of the server and the port.

@Fianax
Copy link
Author

Fianax commented Sep 10, 2021

Sorry but nothing has changed. The problem is that even if the worker dies (because I check that he is dead), the mqtt client is still connected to the server (I see that he is exactly).

But when I close the application or exit it and go back in, then it is already disconnected.

The mqtt client works perfectly in the isolate but it is rare that it does not disconnect from the server when I kill the worker but when changing the screen it does not disconnect either.

@lubritto
Copy link
Owner

Can you create an example project reproducing this bug? I would like to investigate more @Fianax

@Fianax
Copy link
Author

Fianax commented Sep 10, 2021

¿Puede crear un proyecto de ejemplo que reproduzca este error? Me gustaría investigar más @Fianax

Of course

@Fianax
Copy link
Author

Fianax commented Sep 10, 2021

I've been doing more testing and I think it fails when I get a large amount of information per second. Stop entering the client.updates method? .Listen ((List <MqttReceivedMessage > c) async {} but it does not disconnect.

But when information stops arriving, if I kill the worker, then the mqtt client does disconnect.
I think the problem is in the large amount of information that comes from the mqtt server.

PS: I am doing the example and I will also try to put it without isolate to see if it also fails when the amount of data is very large.

@Fianax
Copy link
Author

Fianax commented Sep 10, 2021

Example.txt

@lubritto
Copy link
Owner

Hey @Fianax, did you try using the main thread? The behavior that you are facing seems to be related to a huge number of events being processed. Can you try to reproduce on the main thread and see if closing the client, and canceling the StreamSubscription, the events stop being processed at the same time?

@Fianax
Copy link
Author

Fianax commented Sep 15, 2021

Yes, when I do it in the main thread the data flow stops. But the interface crashes due to the large amount of data, that's why I do it in an isolate.

@Fianax
Copy link
Author

Fianax commented Sep 15, 2021

I have been doing more tests and, after a while connected, data stops arriving. client.update.listen stops running but the client is still connected.

The data keeps coming in but it is no longer launched in client.update.listen and therefore, I no longer have any more data to update. And I don't know if it's the isolate. If I do everything in the main, nothing bad happens but the screen crashes.

@Fianax
Copy link
Author

Fianax commented Sep 21, 2021

In the end, using some mqtt-client properties, I get that when it stops receiving data, only the client disconnects and from the 'mainMessageHandle' I send a message to the worker so that the client connects again.

I'm sorry to have bothered you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants