Disconnected event not raised after stopping and restarting ManagedClient
Hi, I am currently trying to implement my own protocol fallback strategy where the websocket connection is used if the connection fails over mqtt. To do so, I start the client with the first protocol and if the ConnectionFailed event is raised, I stop the client and restart with new options. (In between the stop and start, I have had to put a delay because once StopAsync is called, the client is not yet stopped and it can't be started again.)
However, once the client has been stopped and restarted, a loss of connection will not raise the disconnected event. The logs show the Ping TX >>> then Stopped receiving packets and that is all. I have tried the same thing on the first protocol (so no stopping of the client) and the event is raised. I have also tried raising the delay after calling StopAsync up to 5 seconds and the same happens.
This bug is happening on both MQTT and WSS connections and I have tried with v1.8.4 and v1.8.5
Steps to reproduce:
StartAsyncwith MQTT options- On connection fail,
StopAsyncand wait ~50ms StartAsyncwith WSS options- Unplug Internet cable and wait for next ping request
- The disconnected event is not fired
I'm considering factoring out managed client at this point because of this.
Hi, thank you for reporting this issue. Is this also happening with the regular client?. The managed client is not intended to be used in this way. But I will have a look because the event should be fired in all cases.
Best regards Christian
Hi Christian, thanks for the quick answer.
I will look at the regular client tomorrow, but I have done a little testing and the issue does not seem to be happening if the client options do not change. When passing from MQTT to WSS it happens, but if I reproduce the same steps while connection with MQTT the second time also, it does not happen.
@cquesnel when you do your testing... since you are setup for it faster than I am... can you run the workflow through for wss options exclusively and see if the disconnected event fires? I read your OP wrong and thought that you were using exclusive wss connections.
@dealproc I've just tested and using only wss connection leads to the same problem as using both protocols, after using StopAsync the Disconnected event is not raised anymore.
I've also tried reversing the order and using wss followed by mqtt and it also happens.
In summary: mqtt -> StopAsync() -> mqtt ---> OK wss -> StopAsync() -> wss ---> No event mqtt -> StopAsync() -> wss ---> No event wss -> StopAsync() -> mqtt ---> No event
However, when I directly call StopAsync, the problem does not seem to be happening. It is only when StopAsync is called from within the handler for ConnectingFailed
so i want to say that it sounds like an issue with the wss protocol, but with you showing wss > stop > mqtt > no event it seems like it's a framework issue... but what's the diff for mqtt > stop > mqtt > ok Makes no sense. I'll have to read the code base to understand it more.
If you factored back to a straight MqttConnection (non-managed), did the summary change, or are you still seeing the same issues overall? I'm asking partially for my own edification, plus i think it'll help give more details to @chkr1011 when he goes to debug the potential issue(s).
I'd really like to know how you'd go about building an integration test for something like this. Maybe that's what is missing at this point... we need to boot a mqtt server/client integration test?
I am also interested if this works with the core client (not managed one).
Is this happening also with the final version of 2.8.5 or only with some RC version?
I just finished writing a little program allowing me to test with the managed and unmanaged clients without all of the extra code I had.
It works as expected with the core client. If I call a ConnectAsync in the handler of Disconnected the Disconnected event is still raised even after many reconnections.
In the managed client, mqtt > stop > mqtt has the same behavior as wss > stop > wss.
In the tests, I found out that the Disconnected event is not raised, only if the connection has been previously established. So lets say I start with mqtt, it fails then I try with wss and it also fails, the event is raised. However if wss fails, but wss succeeds and I then block port 443, the event is not raised.
This is all beeing tested in the final version of 2.8.5.
Here is the code i'm using to test. The ConnectWss() and ConnectMqtt() methods build the client options and then call StartAsync(). To test it, I block port 8883 (mqtt) and once the wss connection has been established, i block port 443 to force a deconnection.
public async Task RunTest()
{
var mqttFactory = new MqttFactory();
_managedClient = mqttFactory.CreateManagedMqttClient();
_managedClient.ConnectingFailed += ManagedClientOnConnectingFailed;
_managedClient.Disconnected += ManagedClientOnDisconnected;
await ConnectMqtt();
}
private void ManagedClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e)
{
Console.WriteLine("################# Client is disconnected ################");
}
private async void ManagedClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e)
{
Console.WriteLine("################# Stopping client ################");
await _managedClient.StopAsync();
await Task.Delay(10);
await ConnectWss();
}
Does this apply to the latest version, too?
I assume this is no longer relevant since there is no reply.