shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

[bug] Error on disconnect 'autoReconnectInProgress' was called on null #210

Closed robertsLando closed 4 years ago

robertsLando commented 4 years ago

When I call client.disconnect I always get this error: Unhandled Exception: NoSuchMethodError: The getter 'autoReconnectInProgress' was called on null;

Code:

void disconnect() {
    log.i('Disconnecting...');
    if (connectionState != MqttCurrentConnectionState.DISCONNECTED) {
      client.disconnect();
    }
  }

void _onDisconnected() {
    log.i('DISCONNECTED callback');
    connectionState = MqttCurrentConnectionState.DISCONNECTED;
  }
I/flutter (26707): 💡  MqttClientWrapper - Disconnecting...
I/flutter (26707): 💡  MqttClientWrapper - DISCONNECTED callback
E/flutter (26707): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'autoReconnectInProgress' was called on null.
E/flutter (26707): Receiver: null
E/flutter (26707): Tried calling: autoReconnectInProgress
E/flutter (26707): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
E/flutter (26707): #1      MqttClient.internalDisconnect 
package:mqtt_client/src/mqtt_client.dart:327
E/flutter (26707): #2      MqttServerWsConnection.onDone 
robertsLando commented 4 years ago

If I disable autoReconnect before calling disconnect it works but I noticed that I receive 2 onDisconnect callbacks

shamblett commented 4 years ago

Yes, I'm looking at this on #209 at the moment, I'll fix this in that work.

robertsLando commented 4 years ago

@shamblett ANy progress/news on this?

shamblett commented 4 years ago

Yes, please follow this on #209, nearly there may be out this week.

shamblett commented 4 years ago

OK, I've put a fix in for this, I'm just waiting for any more comments to come back on #209 before I re publish the package.

If you want to test this in the mean time the fix is on the development branch of the repo, update your pubscpec yaml accordingly.

robertsLando commented 4 years ago

@shamblett Thanks! Will test it asap

robertsLando commented 4 years ago

Seems a little bit better but there are still some problems

Also I noticed that after the app is in background for a long time the mqtt disconnects and never comes back even when the app is open again, like it's stucked

robertsLando commented 4 years ago

With the dev branch the autoReconnect doens't work at all, when client goes offline I see autoreconnect in progress but it never ome back online. With the 7.3.0 version at least it was reconnecting correctly, just subscriptions aren't restored correctly

shamblett commented 4 years ago

What do you mean by 'when the client goes offline'? How are you doing this?

Please supply a full log.

robertsLando commented 4 years ago

What do you mean by 'when the client goes offline'? How are you doing this?

If I use the dev branch: client connects > force close the broker > the client goes offline > onAutoreconnect callback triggers > broker is restarted > client never reconnects :negative_squared_cross_mark:

With 7.3.0 : client connects > force close the broker > the client goes offline > onAutoreconnect callback triggers > broker is restarted > client reconnects > subscriptions not restored :negative_squared_cross_mark:

shamblett commented 4 years ago

Ok, still need the full log so I can see whats going on.

robertsLando commented 4 years ago

This are the logs after closing the broker:

WIth dev:

I/flutter (29946): 2020-09-04 13:59:10.986103 -- MqttWsConnection::_onDone - calling disconnected callback
I/flutter (29946): 2020-09-04 13:59:10.990270 -- MqttConnectionHandlerBase::autoReconnect entered
I/flutter (29946): 2020-09-04 13:59:10.991826 -- MqttConnectionHandlerBase::autoReconnect - attempting reconnection
I/flutter (29946): 2020-09-04 13:59:10.992150 -- MqttConnectionHandlerBase::connect - server wss://xxx.xxx.xxx, port 443
I/flutter (29946): 2020-09-04 13:59:10.992290 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (29946): 2020-09-04 13:59:10.992400 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress true

With 7.3.0:

I/flutter (30169): 2020-09-04 14:02:34.484056 -- MqttWsConnection::_onDone - calling disconnected callback
I/flutter (30169): 2020-09-04 14:02:34.489195 -- MqttConnectionHandlerBase::autoReconnect - attempting reconnection
I/flutter (30169): 2020-09-04 14:02:34.489481 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (30169): 2020-09-04 14:02:34.489543 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (30169): 2020-09-04 14:02:34.489582 -- SynchronousMqttServerConnectionHandler::internalConnect - websocket selected
I/flutter (30169): 2020-09-04 14:02:34.489694 -- MqttWsConnection:: WS URL is wss://xxx.xxx.xxx:443, protocols are [mqtt, mqttv3.1, mqttv3.11]
I/flutter (30169): 2020-09-04 14:02:34.543587 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter (30169): 2020-09-04 14:02:34.543918 -- MqttClient::internalDisconnect - not invoking auto connect, already in progress
I/flutter (30169): 2020-09-04 14:02:34.552567 -- SynchronousMqttServerConnectionHandler::internalConnect exception thrown during auto reconnect - ignoring
I/flutter (30169): 2020-09-04 14:02:34.552744 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (30169): 2020-09-04 14:02:34.552812 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (30169): 2020-09-04 14:02:34.552916 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (30169): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 194
I/flutter (30169): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (30169): MqttConnectPayload - client identifier is :
I/flutter (30169): 2020-09-04 14:02:34.553390 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:39.561468 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:39.562303 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 1
I/flutter (30169): 2020-09-04 14:02:39.562464 -- SynchronousMqttServerConnectionHandler::internalConnect - websocket selected
I/flutter (30169): 2020-09-04 14:02:39.562806 -- MqttWsConnection:: WS URL is wss://xxx.xxx.xxx:443, protocols are [mqtt, mqttv3.1, mqttv3.11]
I/flutter (30169): 2020-09-04 14:02:39.624347 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter (30169): 2020-09-04 14:02:39.624562 -- MqttClient::internalDisconnect - not invoking auto connect, already in progress
I/flutter (30169): 2020-09-04 14:02:39.625301 -- SynchronousMqttServerConnectionHandler::internalConnect exception thrown during auto reconnect - ignoring
I/flutter (30169): 2020-09-04 14:02:39.625750 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (30169): 2020-09-04 14:02:39.625995 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (30169): 2020-09-04 14:02:39.626235 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (30169): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 194
I/flutter (30169): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (30169): MqttConnectPayload - client identifier is :
I/flutter (30169): 2020-09-04 14:02:39.627530 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:44.635860 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:44.636156 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 2
I/flutter (30169): 2020-09-04 14:02:44.636288 -- SynchronousMqttServerConnectionHandler::internalConnect - websocket selected
I/flutter (30169): 2020-09-04 14:02:44.636567 -- MqttWsConnection:: WS URL is wss://xxx.xxx.xxx:443, protocols are [mqtt, mqttv3.1, mqttv3.11]
I/flutter (30169): 2020-09-04 14:02:44.700252 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter (30169): 2020-09-04 14:02:44.700569 -- MqttClient::internalDisconnect - not invoking auto connect, already in progress
I/flutter (30169): 2020-09-04 14:02:44.701725 -- SynchronousMqttServerConnectionHandler::internalConnect exception thrown during auto reconnect - ignoring
I/flutter (30169): 2020-09-04 14:02:44.701961 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (30169): 2020-09-04 14:02:44.702170 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (30169): 2020-09-04 14:02:44.702553 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (30169): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 194
I/flutter (30169): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (30169): MqttConnectPayload - client identifier is :
I/flutter (30169): 2020-09-04 14:02:44.703438 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:49.710145 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:49.710435 -- SynchronousMqttServerConnectionHandler::internalConnect exited with state Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:49.710637 -- MqttConnectionHandlerBase::autoReconnect - attempting reconnection
I/flutter (30169): 2020-09-04 14:02:49.710818 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (30169): 2020-09-04 14:02:49.710945 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (30169): 2020-09-04 14:02:49.711025 -- SynchronousMqttServerConnectionHandler::internalConnect - websocket selected
I/flutter (30169): 2020-09-04 14:02:49.711263 -- MqttWsConnection:: WS URL is wss://xxx.xxx.xxx:443, protocols are [mqtt, mqttv3.1, mqttv3.11]
I/flutter (30169): 2020-09-04 14:02:49.745227 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter (30169): 2020-09-04 14:02:49.745378 -- MqttClient::internalDisconnect - not invoking auto connect, already in progress
I/flutter (30169): 2020-09-04 14:02:49.745779 -- SynchronousMqttServerConnectionHandler::internalConnect exception thrown during auto reconnect - ignoring
I/flutter (30169): 2020-09-04 14:02:49.746017 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (30169): 2020-09-04 14:02:49.746109 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (30169): 2020-09-04 14:02:49.746221 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (30169): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 194
I/flutter (30169): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (30169): MqttConnectPayload - client identifier is :
I/flutter (30169): 2020-09-04 14:02:49.746718 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:54.756601 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:54.757459 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 1
I/flutter (30169): 2020-09-04 14:02:54.757600 -- SynchronousMqttServerConnectionHandler::internalConnect - websocket selected
I/flutter (30169): 2020-09-04 14:02:54.758671 -- MqttWsConnection:: WS URL is wss://xxx.xxx.xxx:443, protocols are [mqtt, mqttv3.1, mqttv3.11]
I/flutter (30169): 2020-09-04 14:02:55.037345 -- MqttServerConnection::_startListening
I/flutter (30169): 2020-09-04 14:02:55.037930 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (30169): 2020-09-04 14:02:55.038109 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (30169): 2020-09-04 14:02:55.039753 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (30169): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 194
I/flutter (30169): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (30169): MqttConnectPayload - client identifier is :
I/flutter (30169): 2020-09-04 14:02:55.043361 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:55.142877 -- MqttConnection::_onData
I/flutter (30169): 2020-09-04 14:02:55.143804 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck
I/flutter (30169): Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2
I/flutter (30169): Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.connectionAccepted}
I/flutter (30169): 2020-09-04 14:02:55.144309 -- MqttServerConnection::_onData - message processed
I/flutter (30169): 2020-09-04 14:02:55.144807 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor
I/flutter (30169): 2020-09-04 14:02:55.145107 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor - state = connected
I/flutter (30169): 2020-09-04 14:02:55.147942 -- SynchronousMqttServerConnectionHandler:: cancelling connect timer
I/flutter (30169): 2020-09-04 14:02:55.148487 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code of connectionAccepted and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:55.148684 -- SynchronousMqttServerConnectionHandler::internalConnect exited with state Connection status is connected with return code of connectionAccepted and a disconnection origin of none
I/flutter (30169): 2020-09-04 14:02:55.149533 -- MqttConnectionHandler::autoReconnect - auto reconnect complete
I/flutter (30169): 2020-09-04 14:03:20.193472 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.pingRequest
I/flutter (30169): Header: MessageType = MqttMessageType.pingRequest, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (30169): 2020-09-04 14:03:20.259948 -- MqttConnection::_onData
I/flutter (30169): 2020-09-04 14:03:20.262025 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.pingResponse
I/flutter (30169): Header: MessageType = MqttMessageType.pingResponse, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (30169): 2020-09-04 14:03:20.262451 -- MqttServerConnection::_onData - message processed
I/flutter (30169): 2020-09-04 14:04:20.202074 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.pingRequest
I/flutter (30169): Header: MessageType = MqttMessageType.pingRequest, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (30169): 2020-09-04 14:04:20.259811 -- MqttConnection::_onData
I/flutter (30169): 2020-09-04 14:04:20.260347 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.pingResponse
I/flutter (30169): Header: MessageType = MqttMessageType.pingResponse, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (30169): 2020-09-04 14:04:20.260616 -- MqttServerConnection::_onData - message processed
I/flutter (30169): 2020-09-04 14:05:20.219957 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.pingRequest
I/flutter (30169): Header: MessageType = MqttMessageType.pingRequest, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (30169): 2020-09-04 14:05:20.286423 -- MqttConnection::_onData
I/flutter (30169): 2020-09-04 14:05:20.287001 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.pingResponse
I/flutter (30169): Header: MessageType = MqttMessageType.pingResponse, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (30169): 2020-09-04 14:05:20.287435 -- MqttServerConnection::_onData - message processed
shamblett commented 4 years ago

OK thanks, I'm not seeing any errors in my auto reconnect testing against mosquitto for either ws or wss connections, could you just confirm that the dev log supplied above is complete i.e. doe sit just stop on the last line and hang?

robertsLando commented 4 years ago

doe sit just stop on the last line and hang?

I confirm that it stops and hangs there.

I'm using Aedes mqtt broker (but I don't think it matters)

shamblett commented 4 years ago

For some reason you are not getting this line -

'MqttWsConnection::connectAuto - WS URL is $uriString, protocols are $protocols');

its as though its not being called. Your broker doesn't matter, not for this anyway, I test against hive and mosquitto.

I've added extra debug to the 8.0.0 dev branch, if you get chance could you try this again.

robertsLando commented 4 years ago

Unfortunally not now, and I will be on holiday next week, will take a look at this after next week

shamblett commented 4 years ago

Ok thanks for your time. Ill try and recreate your issue but Ill probably have to republish by then as other people are wating for some of this functionality I will.leave this open though.

robertsLando commented 4 years ago

Thanks to you for this super useful package :smiley:

shamblett commented 4 years ago

Client re-published at 8.0.0, please use this version for any further testing.

robertsLando commented 4 years ago

@shamblett Just tested, seems much better!