From 53d645d8057d1aefd55f2b984b494d319f5313ad Mon Sep 17 00:00:00 2001 From: DavidBar-On Date: Thu, 21 Nov 2024 12:31:02 +0200 Subject: [PATCH 1/2] Do not try to cancell NULL thread --- src/iperf.h | 1 + src/iperf_api.c | 1 + src/iperf_client_api.c | 102 ++++++++++++++++++++++------------------- src/iperf_server_api.c | 32 +++++++------ 4 files changed, 76 insertions(+), 60 deletions(-) diff --git a/src/iperf.h b/src/iperf.h index 7d14a3453..b541d2c8c 100644 --- a/src/iperf.h +++ b/src/iperf.h @@ -193,6 +193,7 @@ struct iperf_stream struct iperf_test* test; pthread_t thr; + int thread_created; int done; /* configurable members */ diff --git a/src/iperf_api.c b/src/iperf_api.c index 34f08bc81..7b69fe0c3 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -4498,6 +4498,7 @@ iperf_new_stream(struct iperf_test *test, int s, int sender) memset(sp, 0, sizeof(struct iperf_stream)); + sp->thread_created = 0; sp->sender = sender; sp->test = test; sp->settings = test->settings; diff --git a/src/iperf_client_api.c b/src/iperf_client_api.c index 67b05cc69..8807541ff 100644 --- a/src/iperf_client_api.c +++ b/src/iperf_client_api.c @@ -715,6 +715,7 @@ iperf_run_client(struct iperf_test * test) i_errno = IEPTHREADCREATE; goto cleanup_and_fail; } + sp->thread_created = 1; if (test->debug_level >= DEBUG_LEVEL_INFO) { iperf_printf(test, "Thread FD %d created\n", sp->socket); } @@ -753,22 +754,25 @@ iperf_run_client(struct iperf_test * test) if (sp->sender) { int rc; sp->done = 1; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "sender cancel in pthread_cancel - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "sender cancel in pthread_join - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "sender cancel in pthread_cancel - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "sender cancel in pthread_join - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } } @@ -791,22 +795,25 @@ iperf_run_client(struct iperf_test * test) if (!sp->sender) { int rc; sp->done = 1; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "receiver cancel in pthread_cancel - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "receiver cancel in pthread_join - %s", iperf_strerror(i_errno)); - goto cleanup_and_fail; - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "receiver cancel in pthread_cancel - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "receiver cancel in pthread_join - %s", iperf_strerror(i_errno)); + goto cleanup_and_fail; + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } } @@ -835,20 +842,23 @@ iperf_run_client(struct iperf_test * test) } sp->done = 1; int rc; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "cleanup_and_fail in pthread_cancel - %s", iperf_strerror(i_errno)); - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "cleanup_and_fail in pthread_join - %s", iperf_strerror(i_errno)); - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "cleanup_and_fail in pthread_cancel - %s", iperf_strerror(i_errno)); + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "cleanup_and_fail in pthread_join - %s", iperf_strerror(i_errno)); + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } if (test->debug_level >= DEBUG_LEVEL_INFO) { diff --git a/src/iperf_server_api.c b/src/iperf_server_api.c index 9727cdddb..aa601c3ec 100644 --- a/src/iperf_server_api.c +++ b/src/iperf_server_api.c @@ -448,20 +448,23 @@ cleanup_server(struct iperf_test *test) SLIST_FOREACH(sp, &test->streams, streams) { int rc; sp->done = 1; - rc = pthread_cancel(sp->thr); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADCANCEL; - errno = rc; - iperf_err(test, "cleanup_server in pthread_cancel - %s", iperf_strerror(i_errno)); - } - rc = pthread_join(sp->thr, NULL); - if (rc != 0 && rc != ESRCH) { - i_errno = IEPTHREADJOIN; - errno = rc; - iperf_err(test, "cleanup_server in pthread_join - %s", iperf_strerror(i_errno)); - } - if (test->debug_level >= DEBUG_LEVEL_INFO) { - iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + if (sp->thread_created == 1) { + rc = pthread_cancel(sp->thr); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADCANCEL; + errno = rc; + iperf_err(test, "cleanup_server in pthread_cancel - %s", iperf_strerror(i_errno)); + } + rc = pthread_join(sp->thr, NULL); + if (rc != 0 && rc != ESRCH) { + i_errno = IEPTHREADJOIN; + errno = rc; + iperf_err(test, "cleanup_server in pthread_join - %s", iperf_strerror(i_errno)); + } + if (test->debug_level >= DEBUG_LEVEL_INFO) { + iperf_printf(test, "Thread FD %d stopped\n", sp->socket); + } + sp->thread_created = 0; } } i_errno = i_errno_save; @@ -909,6 +912,7 @@ iperf_run_server(struct iperf_test *test) cleanup_server(test); return -1; } + sp->thread_created = 1; if (test->debug_level >= DEBUG_LEVEL_INFO) { iperf_printf(test, "Thread FD %d created\n", sp->socket); } From fa183fd11762266b6bbdbbbeb5a27aa2a4196fb2 Mon Sep 17 00:00:00 2001 From: DavidBar-On Date: Fri, 22 Nov 2024 15:42:10 +0200 Subject: [PATCH 2/2] Remove init line per reviewer comment --- src/iperf_api.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 7b69fe0c3..34f08bc81 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -4498,7 +4498,6 @@ iperf_new_stream(struct iperf_test *test, int s, int sender) memset(sp, 0, sizeof(struct iperf_stream)); - sp->thread_created = 0; sp->sender = sender; sp->test = test; sp->settings = test->settings;