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
In a weak network environment, the MQTT Broker is unable to detect disconnections #1883
Comments
Update Description, the infinite loop of Ping is from another team's client. |
We may need to add an additional check here. Right now the "Receive" call on socket level does not get cancelled because there is no timeout check. We may need to insert an additional timeout here so that received a message is allowed to take more time than the keep alive interval. But this would require users to use higher keep alive values if they want to transfer larger payloads on slower connections. I create a branch for this ticket. Then we can test. |
I added a new setting in the server options to alter the handling of keep alive stuff. |
The MQTT 5 spec says:
And the disconnect reason may be:
My interpretation is, that clients must begin sending another packet within the keep-alive interval with the intent that the packet is completely received on the server side no later than 1.5 times the keep-alive timeout. That would mean that being in the process of receiving a packet is not enough to keep the client alive. The current more tolerant behavior for large packets probably makes life easier for clients, but such clients would also have the option to request a longer keep alive timeout. So to me, it looks like that enabling the new Maybe that be could be expressed in the naming of the option somehow, but I'm not sure what that could be. |
@chkr1011 Sorry for my delayed response. Before today, to veriy my idea is workable. I build a test env then keep it running for serval days. -- |
@chkr1011 After five days long-running experiment. |
@gmiloveyou Thanks for testing this. I would stick to @logicaloud opinion and remove the code entirely so that it matches the MQTT spec. |
Describe the bug
1.unable to detect disconnections in a weak network environment
Which component is your bug related to?
-Server
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Disconnect Client Event shuld fired
Additional context / logging
We add smoe extra log-point.
The message stops here.
MQTTnet\Server\Internal\MqttClient.cs
Latest Updated: 2023/11/20
by more logging point
MQTTnet.Adapter > public sealed class MqttChannelAdapter> private async Task ReceiveAsync
We discover that the code never reach "finally" part, stuck in "do while"
So the parameter 'IsReadingPacket" always be true
image
In that case:
MQTTnet.Server>public sealed class MqttServerKeepAliveMonitor>TryProcessClient
the function will always return when check "IsReadingPacket"
that makes the broker never raised the disconnected event
image
But we still do not known
1.the reason that makes this situtation exactly
2.how to fixed.
We will try to figure out continuously .
If anyone can help. that will be grateful.
--
2023/11/22 Update
With additional logging points,
Finally, we found out the code stuck in
MQTTnet-master\Source\MQTTnet\Implementations\MqttTcpChannel.cs
-> public async Task ReadAsync( byte[] buffer, int offset, int count, CancellationToken cancellationToken )
await stream.ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false);
Our guess is that:
CancellToken do not apply any timeout setting.
So mqtt client always waits for tcp packets forever.
We will take the modify as describe in the link below.
https://stackoverflow.com/questions/12421989/networkstream-readasync-with-a-cancellation-token-never-cancels
Now, We had already started an new experimnet to verify it.
Hope this will work.
The text was updated successfully, but these errors were encountered: