Bug: ClientMessageQueueInterceptor breaks the broker due to QoS levels
See original GitHub issueDescribe the bug
If a Client A subscribes a topic with QoS 0 and a client B publishes with QoS 1 AND a ClientMessageQueueInterceptor is used, the broker throws an exception.
Which project is your bug related to?
- Server
To Reproduce
Steps to reproduce the behavior:
- Using this version of MQTTnet ‘3.0.12’.
- Add a
ClientMessageQueueInterceptor. - Subscribe with a Client to a topic “A” and QoS 0
- Publish with a Client to topic “A” and QoS 1
- Exception is thrown (
MqttCommunicationException)
Expected behavior
The usage of a ClientMessageQueueInterceptor should work correctly.
Additional context / logging
The following lines lead to the exception:
- QoS is set to the
publishPacketcorrectly fromqueuedApplicationMessage.QualityOfServiceLevel(i.e. QoS from the subscription) https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Server/MqttClientConnection.cs#L395 - The
MqttClientMessageQueueInterceptorContextis created and passed to the handler. The informationqueuedApplicationMessage.QualityOfServiceLevelisn’t available on the context. https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Server/MqttClientConnection.cs#L407 - The
publishPacket.QualityOfServiceLevelis set again BUT fromApplicationMessage.QualityOfServiceLevelwhich overwrites the correct one fromqueuedApplicationMessage.QualityOfServiceLevel(i.e. with the QoS from the published message) https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Server/MqttClientConnection.cs#L424:
The thrown exceptions:
Message was published with QoS 1 but client subscribed with QoS 0:
MQTT Trace: Sending publish packet failed: Communication exception (ClientId: xy).
MQTTnet.Exceptions.MqttCommunicationException: Packet identifier must be empty if QoS == 0 [MQTT-2.3.1-5]. ---> MQTTnet.Exceptions.MqttProtocolViolationException: Packet identifier must be empty if QoS == 0 [MQTT-2.3.1-5].
at MQTTnet.Formatter.V3.MqttV310PacketFormatter.EncodePublishPacket(MqttPublishPacket packet, IMqttPacketWriter packetWriter)
at MQTTnet.Formatter.V3.MqttV310PacketFormatter.Encode(MqttBasePacket packet)
at MQTTnet.Adapter.MqttChannelAdapter.<SendPacketAsync>d__33.MoveNext()
--- End of inner exception stack trace ---
at MQTTnet.Adapter.MqttChannelAdapter.WrapException(Exception exception)
at MQTTnet.Adapter.MqttChannelAdapter.<SendPacketAsync>d__33.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Server.MqttClientConnection.<SendAsync>d__46.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Server.MqttClientConnection.<SendPendingPacketsAsync>d__45.MoveNext()
Message was published with QoS 0 but client subscribed with QoS 1:
MQTT Trace: Sending publish packet failed: Communication exception (ClientId: xy).
MQTTnet.Exceptions.MqttCommunicationException: Publish packet has no packet identifier. ---> MQTTnet.Exceptions.MqttProtocolViolationException: Publish packet has no packet identifier.
at MQTTnet.Formatter.V3.MqttV310PacketFormatter.EncodePublishPacket(MqttPublishPacket packet, IMqttPacketWriter packetWriter)
at MQTTnet.Formatter.V3.MqttV310PacketFormatter.Encode(MqttBasePacket packet)
at MQTTnet.Adapter.MqttChannelAdapter.<SendPacketAsync>d__33.MoveNext()
--- End of inner exception stack trace ---
at MQTTnet.Adapter.MqttChannelAdapter.WrapException(Exception exception)
at MQTTnet.Adapter.MqttChannelAdapter.<SendPacketAsync>d__33.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Server.MqttClientConnection.<SendAsync>d__46.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Server.MqttClientConnection.<SendPendingPacketsAsync>d__45.MoveNext()
Discussion
This is a bug which is very urgent for us and we would like to submit a Pull Request. We see the following possibilities:
- Don’t allow the interceptor to change the QoS level at all (means, just delete the line)
- Pass and the correct QoS level (means, if the interceptor doesn’t change something, the correct level as before is set)
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
ClientMessageQueueInterceptor breaks the broker due to ...
Publish with a Client to topic "A" and QoS 1; Exception is thrown ( MqttCommunicationException ). Expected behavior. The usage of a ...
Read more >What is MQTT Quality of Service (QoS) 0,1, & 2?
If the subscribing client defines a lower QoS level than the publishing client, the broker will transmit the message with the lower QoS...
Read more >Broker Issues (Oracle GlassFish Message Queue 4.4.2 ...
This “bad version” warning indicates that the client should reconnect to the broker at a lower protocol level. When using a JDBC data...
Read more >Understanding MQTT QOS Levels- Part 1
The QOS levels are a way of guaranteeing message delivery and they refer to the connection between a broker and a client.
Read more >MQTT QoS Guide - Quality of Service 0, 1, 2 Explained
MQTT QoS is a level of service that serves as a consensus between a publisher and a broker on one hand, and the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@msallin I have no plans yet for a new release but I will release an RC soon so you can already make use of the latest changes.
https://github.com/chkr1011/MQTTnet/releases/tag/v3.0.13