Skip to content

net: lwm2m: Stop the LwM2M engine in case of fatal network error #92041

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rlubos
Copy link
Contributor

@rlubos rlubos commented Jun 23, 2025

In case of fatal network error (i.e. when the LwM2M client runs out of retries), call lwm2m_engine_stop() to cleanup any allocated resources for the client. The engine is dead at that point anyway so the application needs to recover.

If this isn't done, it is theoretically possible to restart the LwM2M client (with lwm2m_rd_client_start() which does not report an error in such case), which in turn could lead to resource leaks (like for example the observer list is reinitialized) if the application didn't call lwm2m_rd_client_stop() first. Calling lwm2m_engine_stop() ensures that all resources are freed even if the application doesn't call stop before restarting.

Potentially fixes #91383

@github-actions github-actions bot added size: XS A PR changing only a single line of code area: LWM2M labels Jun 23, 2025
@github-actions github-actions bot requested a review from SeppoTakalo June 23, 2025 14:41
@rlubos rlubos requested a review from jukkar June 23, 2025 14:49
SeppoTakalo
SeppoTakalo previously approved these changes Jun 24, 2025
jukkar
jukkar previously approved these changes Jun 24, 2025
In case of fatal network error (i.e. when the LwM2M client runs out of
retries), call lwm2m_engine_stop() to cleanup any allocated resources
for the client. The engine is dead at that point anyway so the
application needs to recover.

If this isn't done, it is theoretically possible to restart the LwM2M
client (with lwm2m_rd_client_start() which does not report an error in
such case), which in turn could lead to resource leaks (like for
example the observer list is reinitialized) if the application
didn't call lwm2m_rd_client_stop() first. Calling lwm2m_engine_stop()
ensures that all resources are freed even if the application doesn't
call stop before restarting.

Stopping the engine is also needed when the network error occurs when
de-registering - in such case the engine goes straight into the IDLE
state, where it's possible to restart the client and thus reinitialize
the observer lists.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
@rlubos rlubos dismissed stale reviews from jukkar and SeppoTakalo via a6a6736 June 24, 2025 14:48
@rlubos rlubos force-pushed the net/lwm2m-observer-leak branch from c42d2d9 to a6a6736 Compare June 24, 2025 14:48
@rlubos
Copy link
Contributor Author

rlubos commented Jun 24, 2025

I've updated the PR as I've identified one more problematic place where we could leak observers - if the network error happens while deregistering (which isn't that difficult, quite easy to reproduce with net/lwm2m shell, just put the network interface down and stop the client) we could leak observers too.

Copy link

@rlubos rlubos requested review from SeppoTakalo and jukkar July 24, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: LWM2M size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lwm2m: unexpected ENOMEM when adding observer
3 participants