-
Notifications
You must be signed in to change notification settings - Fork 8.3k
net: sockets: Cleanup socket properly if POSIX API is enabled #80232
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
net: sockets: Cleanup socket properly if POSIX API is enabled #80232
Conversation
|
Using the |
3484b2f to
335aa81
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, this seems like it should be unnecessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the extra flag, but this can be super confusing to a future developer looking at this code.
What I mean by this is that some of the networking code is now using close(void*) callback variant and some (currently sockets_inet.c, sockets_tls.c and sockets_packet.c) are using the close2(void*, int) callback variant in order to fix the resource leak. In both cases the fdtable will call the close2(void*, int) if IFSOCK is set but the 2nd parameter is ignored most of the cases (I would have guessed compiler to give a warning in this case). For a future self looking this code in couple of years, this might probably look like a bug rather than a feature 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there will be a fair bit of refactorization here between now and a couple of years from now, mainly because FS also needs to get a bump in the near term for PSE52.
So consumers of close() can be upgraded to close2(), and the older version can be deprecated. Additionally, that would be a good time to introduce something like a k_off_t type and deprecate the function signatures that use ssize_t and off_t.
335aa81 to
8cc4e8c
Compare
The sock_obj_core_dealloc() was not called if close() is called instead of zsock_close(). This happens if POSIX API is enabled. Fix this by calling zvfs_close() from zsock_close() and then pass the socket number to zsock_close_ctx() so that the cleanup can be done properly. Reported-by: Andreas Ålgård <[email protected]> Signed-off-by: Jukka Rissanen <[email protected]>
8cc4e8c to
fe7b1f9
Compare
|
@icsys-aal I tested the code in this PR and did not spot any leaks, but if you have time, please try the PR to verify that your use case works too. |
|
LGTM - thanks for this @jukkar |
Hello, I apologize for not getting back to you sooner. Your fix looks good, however in our use case we are using a strange offloaded device, which seems to return a nonzero return code on offload_put, which means this never runs and the leak persists. Should we call |
@icsys-aal what offloaded driver is that? Perhaps there is a bug in it. |
The driver in question is the winc1500 driver. |
I briefly looked the Atmel HAL and its socket close implementation in zephyr/drivers/wifi/winc1500/wifi_winc1500.c Line 588 in d4b7bf9
we just return the value from the HAL. I think we should do the cleanup in Zephyr socket implementation only if offloaded/native code is also ok with that (so the net_context_put returns 0). The offloaded driver could decide to return 0 if the HAL is giving some bogus value if we want to fix this properly.
|
|
@mmahadevan108 / @dkalowsk Could we get this merged to 4.0? |
The sock_obj_core_dealloc() was not called if close() is called instead of zsock_close(). This happens if POSIX API is enabled.
Fix this by calling zvfs_close() from zsock_close() and then pass the socket number to zsock_close_ctx() so that the cleanup can be done properly.
Reported-by: Andreas Ålgård [email protected]
Fixes #80997