From 71b1ae9fa448fb28212b4b46affa4012158cc3eb Mon Sep 17 00:00:00 2001 From: DavidBar-On Date: Thu, 28 Nov 2024 17:03:45 +0200 Subject: [PATCH] Rebase and add PR 1801 changes to preveng sigfault when test start fail --- src/iperf.h | 1 + src/iperf_client_api.c | 102 ++++++++++++++++++++++------------------- src/iperf_server_api.c | 32 +++++++------ 3 files changed, 75 insertions(+), 60 deletions(-) diff --git a/src/iperf.h b/src/iperf.h index 17b88cf77..a10f76db3 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_client_api.c b/src/iperf_client_api.c index 77f2256c3..3533cd367 100644 --- a/src/iperf_client_api.c +++ b/src/iperf_client_api.c @@ -727,6 +727,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); } @@ -765,22 +766,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; } } } @@ -803,22 +807,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; } } } @@ -847,20 +854,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 2bbdb5ba7..f8b1ec65a 100644 --- a/src/iperf_server_api.c +++ b/src/iperf_server_api.c @@ -451,20 +451,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; @@ -915,6 +918,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); }