shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
50 stars 26 forks source link

InvalidHeaderException (Header must be at least 2 bytes long) on Processing of PublishAck / Publish Received #41

Closed cladelpino closed 1 year ago

cladelpino commented 1 year ago

After publishing with QOS 1 or 2 from a flutter app, the library throws this exception.

Looking at the code, I get the feeling that there is an extra call to _onData which calls isMessageAvailable() and erroneously doesn't return false, causing it to try to read from an empty byte buffer.

I am using the library together with the bloc package, so, it is the case that the code which publishes the message is ran from a different "zone" than the code suscribing to the topic. This is the only difference I can think of from the examples provided. Also, I am not sleeping after publishing, but I tried that and nothing changed.

Log example for QOS1

II/flutter (18997): 2023-01-09 19:48:59.410625 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>> I/flutter (18997): 2023-01-09 19:48:59.552287 -- MqttServerConnection::_onData - Message Received Started <<< I/flutter (18997): 2023-01-09 19:48:59.552645 -- MqttServerConnection::_ondata - adding incoming data, data length is 6, message stream length is 0, message stream position is 0 I/flutter (18997): 2023-01-09 19:48:59.557413 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.publishReceived I/flutter (18997): MessageType = MqttMessageType.publishReceived Duplicate = false Retain = false Qos = atMostOnce Size = 4 I/flutter (18997): Message Identifier = 2 I/flutter (18997): Reason Code = success I/flutter (18997): Properties = No properties set I/flutter (18997): I/flutter (18997): 2023-01-09 19:48:59.567799 -- MqttServerConnection::_onData - message available event fired E/flutter (18997): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: mqtt-client::InvalidHeaderException: The supplied header is invalid. Header must be at least 2 bytes long. I/flutter (18997): 2023-01-09 19:49:04.505405 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.publishReceived I/flutter (18997): 2023-01-09 19:49:04.506545 -- MqttPublishingManager::handlePublishReceived - entered I/flutter (18997): 2023-01-09 19:49:04.511134 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publishRelease I/flutter (18997): MessageType = MqttMessageType.publishRelease Duplicate = false Retain = false Qos = atLeastOnce Size = 0 I/flutter (18997): Message Identifier = 2 I/flutter (18997): Reason Code = success I/flutter (18997): Properties = No properties set I/flutter (18997): I/flutter (18997): 2023-01-09 19:49:04.513729 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>> I/flutter (18997): 2023-01-09 19:49:04.520093 -- MqttServerConnection::_onData - Message Received Started <<< I/flutter (18997): 2023-01-09 19:49:04.524466 -- MqttServerConnection::_ondata - adding incoming data, data length is 45, message stream length is 1, message stream position is 0 I/flutter (18997): 2023-01-09 19:49:04.525270 -- Available bytes(46) is less than the message size 52 I/flutter (18997): 2023-01-09 19:49:04.526407 -- MqttServerConnection::_onData - Message Received Ended <<<

Log example with QOS2:

I/flutter (18559): 2023-01-09 19:32:15.325602 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>> I/flutter (18559): 2023-01-09 19:32:15.513071 -- MqttServerConnection::_onData - Message Received Started <<< I/flutter (18559): 2023-01-09 19:32:15.515605 -- MqttServerConnection::_ondata - adding incoming data, data length is 6, message stream length is 0, message stream position is 0 I/flutter (18559): 2023-01-09 19:32:15.521690 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.publishReceived I/flutter (18559): MessageType = MqttMessageType.publishReceived Duplicate = false Retain = false Qos = atMostOnce Size = 4 I/flutter (18559): Message Identifier = 2 I/flutter (18559): Reason Code = success I/flutter (18559): Properties = No properties set I/flutter (18559): I/flutter (18559): 2023-01-09 19:32:15.526831 -- MqttServerConnection::_onData - message available event fired E/flutter (18559): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: mqtt-client::InvalidHeaderException: The supplied header is invalid. Header must be at least 2 bytes long. I/flutter (18559): 2023-01-09 19:32:38.712619 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publishRelease I/flutter (18559): MessageType = MqttMessageType.publishRelease Duplicate = false Retain = false Qos = atLeastOnce Size = 0 I/flutter (18559): Message Identifier = 2 I/flutter (18559): Reason Code = success I/flutter (18559): Properties = No properties set I/flutter (18559): I/flutter (18559): 2023-01-09 19:32:38.720054 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>> I/flutter (18559): 2023-01-09 19:32:38.723103 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.pingRequest I/flutter (18559): MessageType = MqttMessageType.pingRequest Duplicate = false Retain = false Qos = atMostOnce Size = 0 I/flutter (18559): 2023-01-09 19:32:38.724901 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>> I/flutter (18559): 2023-01-09 19:32:38.727776 -- MqttServerConnection::_onData - Message Received Started <<< I/flutter (18559): 2023-01-09 19:32:38.730001 -- MqttServerConnection::_ondata - adding incoming data, data length is 45, message stream length is 1, message stream position is 0 I/flutter (18559): 2023-01-09 19:32:38.730910 -- Available bytes(46) is less than the message size 52 I/flutter (18559): 2023-01-09 19:32:38.732243 -- MqttServerConnection::_onData - Message Received Ended <<<

shamblett commented 1 year ago

Yep, OK, I'll look at this,

cladelpino commented 1 year ago

Thanks! Let me know if there's any help needed.. Sometime in the future I will try to make a minimal reproducible example.

MattBrittan commented 1 year ago

A question has been asked on stack overflow which appears to be the same issue.

shamblett commented 1 year ago

OK, if I run the QOS examples from the examples directory (mqtt_client5_publish_qos1.dart and mqtt_client5_publish_qos2.dart) they work fine so whatever is happening seems to be in your flutter environment so I'm going to need a reproducible sample from you to get any further. Do you have keepalive set, i.e. are also sending keepalive pings to the broker?

shamblett commented 1 year ago

May have fixed this please try version 3.3.5

shamblett commented 1 year ago

Version 3.3.6 released, please check if this fixes the problem.

shamblett commented 1 year ago

Package re published at version 3.4.0, believe this is now fixed.