Skip to content

[noup] zephyr: fix socket and memory leak when remove interface#148

Open
GaofengZhangNXP wants to merge 3 commits into
zephyrproject-rtos:mainfrom
nxp-upstream:wifi_intf_add_remove
Open

[noup] zephyr: fix socket and memory leak when remove interface#148
GaofengZhangNXP wants to merge 3 commits into
zephyrproject-rtos:mainfrom
nxp-upstream:wifi_intf_add_remove

Conversation

@GaofengZhangNXP

@GaofengZhangNXP GaofengZhangNXP commented Jul 8, 2026

Copy link
Copy Markdown
Contributor
  1. When the interface is removed, the hostapd control socket is not closed,
    resulting in a hostapd control socket leak.
  2. hostapd_deinit can't get zephyr driver dev_ops,
    and can't free hostapd memory during hostapd_init

@GaofengZhangNXP GaofengZhangNXP changed the title 123 [noup] zephyr wpa_supplicant and hostapd support add and remove interface when wlan-reset 2 Jul 8, 2026
@GaofengZhangNXP GaofengZhangNXP changed the title [noup] zephyr wpa_supplicant and hostapd support add and remove interface when wlan-reset 2 [noup] support add and remove supplicant interface when wifi reset Jul 8, 2026
Comment thread hostapd/hostapd_cli_zephyr.c Outdated
@MaochenWang1

Copy link
Copy Markdown
Collaborator

should not mention wlan-reset 2 in the commit message.

Comment thread src/drivers/driver_zephyr.c
@GaofengZhangNXP
GaofengZhangNXP force-pushed the wifi_intf_add_remove branch 2 times, most recently from 63a323a to cc9e4de Compare July 13, 2026 08:57
@GaofengZhangNXP GaofengZhangNXP changed the title [noup] support add and remove supplicant interface when wifi reset [noup] fix socket and memory leak when wifi remove interface Jul 13, 2026
@krish2718
krish2718 requested a review from Copilot July 13, 2026 12:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR targets Zephyr-specific cleanup issues during Wi-Fi reset/interface removal by improving teardown paths in the Zephyr driver and hostapd control interface so resources (control sockets/fds and allocated context) don’t leak.

Changes:

  • Update Zephyr hostapd driver deinit to retrieve dev_ops via get_dev_ops(if_ctx->dev_ctx) and add NULL safety.
  • Add zephyr_hostapd_ctrl_deinit() API and implement deferred fd cleanup in the eloop thread.
  • Simplify hostapd CLI connection close logic (removing unnecessary detach for a connection that was never attached).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/drivers/driver_zephyr.c Adjusts hostapd deinit to fetch dev_ops from the device context and avoid NULL dereference.
hostapd/hostapd_cli_zephyr.h Exposes the new Zephyr hostapd control deinit function.
hostapd/hostapd_cli_zephyr.c Implements control deinit and fd cleanup logic to prevent control fd leaks on interface removal.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/drivers/driver_zephyr.c
Comment thread hostapd/hostapd_cli_zephyr.c Outdated
Comment thread hostapd/hostapd_cli_zephyr.c
@GaofengZhangNXP
GaofengZhangNXP force-pushed the wifi_intf_add_remove branch 2 times, most recently from 35ddc60 to 5064f5c Compare July 14, 2026 08:16
@krish2718

Copy link
Copy Markdown
Collaborator

You can run git clang-format HEAD~1 to format, I see some newline or spacing issues, this would take care of it and then you can amend the changes.

@GaofengZhangNXP

Copy link
Copy Markdown
Contributor Author

should not mention wlan-reset 2 in the commit message.

update it

@GaofengZhangNXP
GaofengZhangNXP force-pushed the wifi_intf_add_remove branch 2 times, most recently from 22f6700 to 883db12 Compare July 14, 2026 10:31
@GaofengZhangNXP

Copy link
Copy Markdown
Contributor Author

You can run git clang-format HEAD~1 to format, I see some newline or spacing issues, this would take care of it and then you can amend the changes.

I executed git clang-format HEAD~1, but it reformatted the code to use spaces for indentation at the beginning of each line instead of tabs. According to the Zephyr coding style, tabs should be used for indentation. My original changes were all indented with tabs, so the formatting applied by git clang-format appears to be inconsistent with the expected Zephyr style.

@jukkar

jukkar commented Jul 14, 2026

Copy link
Copy Markdown
Member

I executed git clang-format HEAD~1, but it reformatted the code to use spaces for indentation at the beginning of each line instead of tabs.

I cannot recommend to use clang-format for anything, it usually does bad job as you have noticed.

@krish2718

Copy link
Copy Markdown
Collaborator

I executed git clang-format HEAD~1, but it reformatted the code to use spaces for indentation at the beginning of each line instead of tabs.

I cannot recommend to use clang-format for anything, it usually does bad job as you have noticed.

:). Probably because this repo doesn't have .clang-format file, there's one here https://github.com/zephyrproject-rtos/hostap/blob/main/wpa_supplicant/binder/.clang-format and here https://github.com/zephyrproject-rtos/zephyr/blob/main/.clang-format. Anyways that was just a suggestion to automate styling, up to you.

@GaofengZhangNXP

Copy link
Copy Markdown
Contributor Author

git clang-format HEAD~1

thank your suggestion, I can use the .clang-format to format hostapd repo and zephyr repo PR, and update it


dev_ops->hapd_deinit(if_ctx->dev_priv);

out:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warrants a separate commit as its fixing a different leak.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, this fix is a separate commit, you can check it

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still part of the original commit to fix the control socket leak.

@GaofengZhangNXP

Copy link
Copy Markdown
Contributor Author

should not mention wlan-reset 2 in the commit message.

update it

@jukkar jukkar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please adjust the commit messages a bit to make it clear that this is a zephyr specific change, so

[noup] zephyr: ....

Comment thread src/drivers/driver_zephyr.c Outdated
@GaofengZhangNXP
GaofengZhangNXP force-pushed the wifi_intf_add_remove branch 2 times, most recently from 8ada1d6 to 883effa Compare July 16, 2026 08:03
Comment thread src/drivers/driver_zephyr.c Outdated
@GaofengZhangNXP
GaofengZhangNXP force-pushed the wifi_intf_add_remove branch 3 times, most recently from a281f73 to 9981f90 Compare July 16, 2026 09:09
When the interface is removed, the hostapd control socket
is not closed, resulting in a hostapd control socket leak.

Signed-off-by: Gaofeng Zhang <gaofeng.zhang@nxp.com>
hostapd_deinit can't get zephyr driver dev_ops,
and can't free hostapd memory when hostapd_init

Signed-off-by: Gaofeng Zhang <gaofeng.zhang@nxp.com>
@GaofengZhangNXP GaofengZhangNXP changed the title [noup] fix socket and memory leak when wifi remove interface [noup] zephyr: fix socket and memory leak when remove interface Jul 16, 2026
@GaofengZhangNXP

Copy link
Copy Markdown
Contributor Author

Please adjust the commit messages a bit to make it clear that this is a zephyr specific change, so

[noup] zephyr: ....

update it

when hostapd and supplicant interface is removed,
zep_drv_if_ctx is free in hostapd and supplicant driver deinit.
so need to check zep_drv_if_ctx if function parameteris zep_drv_if_ctx

Signed-off-by: Gaofeng Zhang <gaofeng.zhang@nxp.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment on lines 1619 to +1622
if_ctx = priv;

dev_ops = (struct zep_wpa_supp_dev_ops *)if_ctx->dev_ops;
if (!dev_ops->hapd_deinit) {
dev_ops = get_dev_ops(if_ctx->dev_ctx);
if (!dev_ops || !dev_ops->hapd_deinit) {
Comment on lines +359 to +381
/* Close ctrl_conn (just frees wpa_ctrl struct, no fd close) */
hostapd_cli_close_connection(hapd);
ctx = os_zalloc(sizeof(*ctx));
if (!ctx) {
/* Best-effort cleanup even on OOM to avoid leaking fds/socks */
if (hapd->recv_sock >= 0) {
eloop_unregister_read_sock(hapd->recv_sock);
close(hapd->recv_sock);
hapd->recv_sock = -1;
}
if (hapd->send_sock >= 0) {
close(hapd->send_sock);
hapd->send_sock = -1;
}
return;
}
ctx->recv_sock = hapd->recv_sock;
ctx->send_sock = hapd->send_sock;
hapd->recv_sock = -1;
hapd->send_sock = -1;
/* timeout=0: execute in next eloop iteration, in eloop thread */
if (eloop_register_timeout(0, 0, hapd_ctrl_close_fds, ctx, NULL) < 0)
hapd_ctrl_close_fds(ctx, NULL);
Comment on lines 22 to 24
int zephyr_hostapd_ctrl_init(void *ctx);
void zephyr_hostapd_ctrl_deinit(void *hapd);
int zephyr_hostapd_ctrl_zephyr_cmd(struct wpa_ctrl *ctrl, int argc, const char *argv[]);
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.

6 participants