This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit ca26789991573390d9d5a5577537d23eb76d5c96 Author: zhangkai25 <[email protected]> AuthorDate: Tue Aug 18 12:10:00 2026 +0800 iperf: release resources when interrupted by control-C Control-C left the port bound and the instance on g_iperf_ctrl_list. The default SIGINT action kills the task on the spot, so iperf_start() never reached its sq_rem(), and the traffic thread never reached close() on the listening socket. A second "iperf -s" then failed with EADDRINUSE, and the node left behind pointed into a stack frame already released. Handle SIGINT instead and let the existing exit paths do the work: the handler asks the instance to stop, the traffic thread unwinds and closes its socket, pthread_join() returns, and the sq_rem() already there runs. Setting the finish flag alone is not enough, since a thread parked in accept() does not re-test it until that call returns, which for a server with no client never happens. What ends the call is the delivery of the signal, and only the one group member chosen to receive it gets its blocking call completed with EINTR, so delivery must land on the traffic thread. The signal mask arranges that: iperf_start() blocks the set before creating the thread, which inherits the mask and unblocks it for itself, leaving it the only member not blocking these signals. iperf_start_report() blocks across its own pthread_create() for the same reason. Unblocking in the thread also delivers a signal that arrived in between, which was left pending on the group rather than dropped. iperf_start() installs the dispositions as well, beside the mask that decides which thread the signals reach, since the two only work together. It restores them on every path out because they belong to the task group, shared with whatever runs on this console next: "cu" installs its own SIGINT handler to know when to quit, and losing it leaves the terminal unable to exit. The instance is published before the thread starts, so a control-C arriving while the thread is already blocked can still find it. iperf_stop() now only sets the flag; unlinking is left to iperf_start(), which owns the stack frame the node lives in and does it after joining anyway. Taking the non-recursive list mutex from a handler is safe here: iperf_start() holds it only with these signals blocked, and neither the traffic thread the handler runs on nor the report thread takes it. The udp server joins the report thread only if one was started, since it is created when the first packet arrives. Interrupted socket calls still report their error, so stopping a server prints one "Interrupted system call" line. Left alone to keep this change to what the leak needs. This does not help "iperf -a", which runs as a separate task: the mask steers delivery only within the traffic thread's own task group, so an abort from elsewhere sets the flag but cannot end the blocking call. A server in accept() is still unstoppable that way; the TODO above accept4() asking for non-blocking mode remains the real fix. Verified on tc489_bmp_b0 by reproducing the original sequence -- server parked in accept, then a console switch: the instance is released, the list returns to empty, and a second run no longer fails with EADDRINUSE. Signed-off-by: zhangkai25 <[email protected]> Assisted-by: Kiro:claude-opus-4 --- netutils/iperf/Kconfig | 2 +- netutils/iperf/iperf.c | 132 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 117 insertions(+), 17 deletions(-) diff --git a/netutils/iperf/Kconfig b/netutils/iperf/Kconfig index 69165bc14..65b791fbc 100644 --- a/netutils/iperf/Kconfig +++ b/netutils/iperf/Kconfig @@ -27,7 +27,7 @@ config NETUTILS_IPERF bool "iperf example" default n - depends on NET + depends on NET && !DISABLE_SIGNALS select NETUTILS_NETLIB select LIBC_FLOATINGPOINT ---help--- diff --git a/netutils/iperf/iperf.c b/netutils/iperf/iperf.c index 94f91d297..370caf23e 100644 --- a/netutils/iperf/iperf.c +++ b/netutils/iperf/iperf.c @@ -31,6 +31,7 @@ #include <netpacket/rpmsg.h> #include <pthread.h> #include <sched.h> +#include <signal.h> #include <stdbool.h> #include <sys/prctl.h> #include <sys/socket.h> @@ -69,6 +70,7 @@ struct iperf_ctrl_t FAR struct iperf_ctrl_t *flink; struct iperf_cfg_t cfg; pthread_t report_thread; + pthread_t traffic_thread; bool finish; uintmax_t total_len; uint32_t buffer_len; @@ -106,6 +108,8 @@ inline static bool iperf_is_udp_client(FAR struct iperf_ctrl_t *ctrl); inline static bool iperf_is_udp_server(FAR struct iperf_ctrl_t *ctrl); inline static bool iperf_is_tcp_client(FAR struct iperf_ctrl_t *ctrl); inline static bool iperf_is_tcp_server(FAR struct iperf_ctrl_t *ctrl); +static void iperf_wakeup_handler(int signo); +static void iperf_stop_handler(int signo); static int iperf_get_socket_error_code(int sockfd); static int iperf_show_socket_error_reason(FAR const char *str, int sockfd); static void iperf_report_task(FAR void *arg); @@ -177,6 +181,71 @@ inline static bool iperf_is_tcp_server(FAR struct iperf_ctrl_t *ctrl) && (ctrl->cfg.flag & IPERF_FLAG_TCP)); } +/**************************************************************************** + * Name: iperf_wakeup_handler + * + * Description: + * Empty handler for the signal used to wake the traffic thread. + * + * The body is intentionally empty and must stay that way: what releases + * the traffic thread from accept() or recv() is the delivery of the + * signal itself, which completes the call with EINTR, not anything done + * here. The handler exists only so the signal has a disposition that + * does not terminate the task; SIG_IGN would not do, since an ignored + * signal does not interrupt a blocking call. + * + ****************************************************************************/ + +static void iperf_wakeup_handler(int signo) +{ + UNUSED(signo); +} + +/**************************************************************************** + * Name: iperf_stop_handler + * + * Description: + * Ask every running instance to stop: set the finish flag and wake its + * traffic thread out of any blocking socket call. iperf_stop() reuses it + * from ordinary context under the list mutex. + * + * Where the default action terminates the task, it does so on the spot: + * iperf_start() never reaches the code that unlinks the instance, and the + * traffic thread never closes its listening socket, so the port stays + * bound and a later run fails with EADDRINUSE. Requesting a stop instead + * lets the existing exit paths run to completion. + * + * Only async-signal-safe work is done here: the finish flag is a single + * word, and pthread_kill() is on the list of functions a handler may + * call. The heavier teardown -- unlinking, close(), free() -- is left to + * the threads themselves once they unwind. + * + * The kill is what actually ends the test. Setting finish alone is not + * enough for a traffic thread parked in accept() or recv(), which does + * not re-test the flag until its current call returns; a server with no + * client would never return. Directing the signal at that thread + * completes its blocking call with EINTR regardless of which group member + * the kernel happens to run this handler on. + * + ****************************************************************************/ + +static void iperf_stop_handler(int signo) +{ + FAR sq_entry_t *p; + + sq_for_every(&g_iperf_ctrl_list, p) + { + FAR struct iperf_ctrl_t *ctrl = (FAR struct iperf_ctrl_t *)p; + + ctrl->finish = true; + + if (ctrl->traffic_thread != 0) + { + pthread_kill(ctrl->traffic_thread, SIGUSR1); + } + } +} + /**************************************************************************** * Name: iperf_get_socket_error_code * @@ -974,8 +1043,12 @@ int iperf_start(FAR struct iperf_cfg_t *cfg) { struct iperf_ctrl_t ctrl; struct sched_param param; + struct sigaction act; + struct sigaction oldint; + struct sigaction oldterm; + struct sigaction oldpipe; + struct sigaction oldusr1; pthread_attr_t attr; - pthread_t thread; FAR void *retval; int ret; @@ -1000,27 +1073,65 @@ int iperf_start(FAR struct iperf_cfg_t *cfg) param.sched_priority = IPERF_TRAFFIC_TASK_PRIORITY; pthread_attr_setschedparam(&attr, ¶m); pthread_attr_setstacksize(&attr, IPERF_TRAFFIC_TASK_STACK); - ret = pthread_create(&thread, &attr, (FAR void *)iperf_task_traffic, - &ctrl); + /* Claim the signals that end a test, for as long as it runs. + * + * SIGINT is control-C and SIGTERM is an explicit kill. SIGPIPE belongs + * here too: it is raised when writing to a socket whose peer has gone + * away, which is an ordinary way for a test to end. SIGUSR1 is the + * private wake-up the handler sends to the traffic thread; it needs a + * disposition that does not terminate the task. + * + * Dispositions belong to the task group, which is shared with whatever + * else is running on this console, so the previous ones are put back + * before returning: "cu", for one, installs its own SIGINT handler to + * learn when to quit, and losing it leaves the terminal unable to exit. + */ + + memset(&act, 0, sizeof(act)); + sigemptyset(&act.sa_mask); + act.sa_handler = iperf_stop_handler; + sigaction(SIGINT, &act, &oldint); + sigaction(SIGTERM, &act, &oldterm); + sigaction(SIGPIPE, &act, &oldpipe); + + act.sa_handler = iperf_wakeup_handler; + sigaction(SIGUSR1, &act, &oldusr1); + + ret = pthread_create(&ctrl.traffic_thread, &attr, + (FAR void *)iperf_task_traffic, &ctrl); if (ret != 0) { printf("iperf_task_traffic: create task failed: %d\n", ret); + + sigaction(SIGINT, &oldint, NULL); + sigaction(SIGTERM, &oldterm, NULL); + sigaction(SIGPIPE, &oldpipe, NULL); + sigaction(SIGUSR1, &oldusr1, NULL); free(ctrl.buffer); ctrl.buffer = NULL; return -1; } + /* Publish the instance before waiting for the worker to finish, so the + * stop path can find it. + */ + pthread_mutex_lock(&g_iperf_ctrl_mutex); sq_addlast((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list); pthread_mutex_unlock(&g_iperf_ctrl_mutex); - pthread_join(thread, &retval); + pthread_join(ctrl.traffic_thread, &retval); pthread_mutex_lock(&g_iperf_ctrl_mutex); sq_rem((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list); pthread_mutex_unlock(&g_iperf_ctrl_mutex); + sigaction(SIGINT, &oldint, NULL); + sigaction(SIGTERM, &oldterm, NULL); + sigaction(SIGPIPE, &oldpipe, NULL); + sigaction(SIGUSR1, &oldusr1, NULL); + return 0; } @@ -1034,19 +1145,8 @@ int iperf_start(FAR struct iperf_cfg_t *cfg) int iperf_stop(void) { - FAR struct iperf_ctrl_t *ctrl; - FAR sq_entry_t *tmp; - FAR sq_entry_t *p; - pthread_mutex_lock(&g_iperf_ctrl_mutex); - - sq_for_every_safe(&g_iperf_ctrl_list, p, tmp) - { - ctrl = (FAR struct iperf_ctrl_t *)p; - ctrl->finish = true; - sq_rem(p, &g_iperf_ctrl_list); - } - + iperf_stop_handler(0); pthread_mutex_unlock(&g_iperf_ctrl_mutex); return 0;
