Skip to content
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

After the device system sleeps and wakes up again, the PING request is not sent #2760

Open
liangliang-zhll opened this issue Feb 25, 2023 · 2 comments

Comments

@liangliang-zhll
Copy link

hi:
I have a problem here, please help me guide, thank you!
I am using libmosquito to develop mqtt client applications;using mosquitto_connecte () to connect the mqtt broker, and set keepalive time is 3 minutes. When a PINGREQ request is sent and the PINGRESP response message is received, I trigger to let my device system sleep for 2 minutes, and then wakes up the device system. After waiting for 1 minute, the PINGREQ request is not sent;

I found mosquito_ loop_ misc()--> mosquitto__ check_ keepalive() --> mosquitto_ time() --> clock_ gettime(CLOCK_MONOTONIC, &tp); The current time obtained is the system running time. When the system sleeps, it will not be counted. So when the system wakes up, the two minutes of the system sleep will not be counted, so waiting for one minute will not send a PINGREQ request;

Excuse me: 1) Does mosquito not support the keepalive function of the system sleep scenario? 2) Is there any good solution to this problem?
(Can we use clock_gettime (CLOCK_BOOTTIME,&tp) to solve the above problem?)

Please help to answer it, thank you!

@Daedaluz
Copy link
Contributor

Daedaluz commented Feb 27, 2023

Interesting edge-case...

While i think using CLOCK_BOOTTIME when possible is generally a good idea and would probably solve this specific test in your case, I would not rely on the connection being preserved over sleeps.

There are other layers of timeout implementations that also needs to be considered (like TCP and TLS for example).

If this is a system where you are using sleep periodically to preserve battery or something similar,
I would instead of trying to keep a specific connection alive over sleeps, just make use of persistent clients and do new connections when required and disconnect over sleep.

The only thing this change would do is maybe let the client use the same connection after a sleep, provided that the server hasn't already cut the connection while the client were sleeping.

Also: this suggests that the issue is gone in linux 4.17 and later.
Edit: My bad, it was reverted.

  1. I'm not sure if this is about mosquitto supporting system sleep or not, given that that mosquitto should do reconnects (if configured to do so) when the connection is lost (for whatever reason) and mosquitto supporting QoS 1, 2 should prevent any messages having qos > 0 beeing lost during temporary uplink issues.
  2. Sure, using CLOCK_BOOTTIME should fix the issue (or using linux 4.17 or later)

Just to clarify, I'm not arguing against such change and it certainly isn't a big one, maybe just curious where exactly the problem came from or "what prompted testing".

@liangliang-zhll
Copy link
Author

liangliang-zhll commented Mar 2, 2023

Thank you for your answer

First, we have this application scenario:

  1. After the device successfully connects to the server, if there is no control message interacting with the server, mosquito will send the PINGREQ packet. After receiving the PINGRESP packet, we will request the device to sleep state; In the sleep state, it is necessary to wake up regularly (keep alive until) and send PING packets to maintain the mqtt link connection;
  2. If the device does not connect to the server successfully, it does not need to send PING packets in the sleep state;

In addition, in the sleep state, the client and server maintain the link connection, mainly to improve the timeliness of message processing, so that it does not take too much time to connect to the server

when mosquitto_connect (), We set the keepalive time. Even if there is no control message to communicate with the server, the server will send a PING packet to maintain the link connection when the keepalive time is up. If the server does not receive the keepalive packet or other control messages within 1.5 times of the keepalive time, the server will disconnect; If the PING packet is sent within 1.5 times of the keepalive time, the link connection will always be maintained; And when the device is not in sleep state, we have no problem testing the activation function.

Please help me to have a look again, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants