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

Fix infinite loop in mqttsimple #406

Closed
wants to merge 1 commit into from

Conversation

nmanne-anlgov
Copy link

If the connection is closed, recv returns 0 and gets stuck in an infinite loop.

If the connection is closed, recv returns 0 and gets stuck in an infinite loop
@wolfSSL-Bot
Copy link

Can one of the admins verify this patch?

@embhorn embhorn self-assigned this Nov 11, 2024
@embhorn
Copy link
Member

embhorn commented Nov 11, 2024

Hi @nmanne-anlgov

Thanks for contacting wolfSSL Support. The socket timeout should handle the condition you described:

    /* Setup timeout */
    setup_timeout(&tv, timeout_ms);
    (void)setsockopt(*pSockFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
            sizeof(tv));

/* Setup timeout */
setup_timeout(&tv, timeout_ms);
(void)setsockopt(*pSockFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
sizeof(tv));
/* Loop until buf_len has been read, error or timeout */
while (bytes < buf_len) {
rc = (int)recv(*pSockFd, &buf[bytes], buf_len - bytes, 0);
if (rc < 0) {
rc = socket_get_error(*pSockFd);
if (rc == 0)
break; /* timeout */
PRINTF("NetRead: Error %d", rc);
return MQTT_CODE_ERROR_NETWORK;
}
bytes += rc; /* Data */
}
if (bytes == 0) {
return MQTT_CODE_ERROR_TIMEOUT;
}

Is the timeout not working with your setup?

Could you tell us a bit about your project using wolfMQTT?

Kind regards,
@embhorn - wolfSSL Support

@embhorn
Copy link
Member

embhorn commented Nov 11, 2024

I was able to reproduce this issue by killing the broker task while the client was waiting for a ping response. I've added a fix here:
#407

Let me know if that resolves the issue for you, also.

Thanks,
@embhorn

@nmanne-anlgov
Copy link
Author

Hi @embhorn,

Thanks for contacting wolfSSL Support. The socket timeout should handle the condition you described:

Is the timeout not working with your setup?

I think that timeout is for an individual recv call. If the other side doesn't send any message within the timeout, the recv call returns -1 and it exits the loop.

Upon successful completion, recv() returns the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recv() returns 0. Otherwise, -1 is returned and errno is set to indicate the error.

But if the connection somehow gets closed, recv returns 0 immediately and it doesn't trigger the timeout or any of the other errors, causing this loop to go into a 100% CPU infinite loop.

In our project, we are using wolfMQTT on an embedded linux system to connect to a local broker from multiple processes running on it. These processes use MQTT to communicate among each other.
We had used the mqttsimple example as a base for implementing this communication in all the applications. After running for a while, some of these processes get stuck in a 100% CPU loop.
Using some debug printing, I realized its happening in mqtt_net_read, if they somehow disconnect from the broker. Adding this line solved that issue and I'm not seeing any more instances of 100% CPU usage.

Regards,
Nithin

@nmanne-anlgov
Copy link
Author

I was able to reproduce this issue by killing the broker task while the client was waiting for a ping response. I've added a fix here: #407

Let me know if that resolves the issue for you, also.

Thanks, @embhorn

Thanks for the fix, yeah I think this should also solve the issue.

@embhorn
Copy link
Member

embhorn commented Nov 11, 2024

Great! I'll close this PR in favor of the fix in #407

@embhorn embhorn closed this Nov 11, 2024
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

Successfully merging this pull request may close these issues.

3 participants