Skip to content

Commit

Permalink
Improve error handling around thread attribute calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmah888 committed Nov 8, 2023
1 parent a360f70 commit 0b1be25
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/iperf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ enum {
IEPTHREADCREATE=150, // Unable to create thread (check perror)
IEPTHREADCANCEL=151, // Unable to cancel thread (check perror)
IEPTHREADJOIN=152, // Unable to join thread (check perror)
IEPTHREADATTRINIT=153, // Unable to initialize thread attribute (check perror)
IEPTHREADATTRDESTROY=154, // Unable to destroy thread attribute (check perror)
/* Stream errors */
IECREATESTREAM = 200, // Unable to create a new stream (check herror/perror)
IEINITSTREAM = 201, // Unable to initialize stream (check herror/perror)
Expand Down
10 changes: 8 additions & 2 deletions src/iperf_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,10 @@ iperf_run_client(struct iperf_test * test)

/* Create and spin up threads */
pthread_attr_t attr;
pthread_attr_init(&attr);
if (pthread_attr_init(&attr) != 0) {
i_errno = IEPTHREADATTRINIT;
goto cleanup_and_fail;
}

SLIST_FOREACH(sp, &test->streams, streams) {
if (pthread_create(&(sp->thr), &attr, &iperf_client_worker_start, sp) != 0) {
Expand All @@ -650,7 +653,10 @@ iperf_run_client(struct iperf_test * test)
if (test->debug_level >= DEBUG_LEVEL_INFO) {
iperf_printf(test, "All threads created\n");
}
pthread_attr_destroy(&attr);
if (pthread_attr_destroy(&attr) != 0) {
i_errno = IEPTHREADATTRDESTROY;
goto cleanup_and_fail;
}

// Set non-blocking for non-UDP tests
if (test->protocol->id != Pudp) {
Expand Down
8 changes: 8 additions & 0 deletions src/iperf_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ iperf_strerror(int int_errno)
snprintf(errstr, len, "unable to join thread");
perr = 1;
break;
case IEPTHREADATTRINIT:
snprintf(errstr, len, "unable to create thread attributes");
perr = 1;
break;
case IEPTHREADATTRDESTROY:
snprintf(errstr, len, "unable to destroy thread attributes");
perr = 1;
break;
default:
snprintf(errstr, len, "int_errno=%d", int_errno);
perr = 1;
Expand Down
10 changes: 8 additions & 2 deletions src/iperf_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,10 @@ iperf_run_server(struct iperf_test *test)

/* Create and spin up threads */
pthread_attr_t attr;
pthread_attr_init(&attr);
if (pthread_attr_init(&attr) != 0) {
i_errno = IEPTHREADATTRINIT;
cleanup_server(test);
};

SLIST_FOREACH(sp, &test->streams, streams) {
if (pthread_create(&(sp->thr), &attr, &iperf_server_worker_start, sp) != 0) {
Expand All @@ -855,7 +858,10 @@ iperf_run_server(struct iperf_test *test)
if (test->debug_level >= DEBUG_LEVEL_INFO) {
iperf_printf(test, "All threads created\n");
}
pthread_attr_destroy(&attr);
if (pthread_attr_destroy(&attr) != 0) {
i_errno = IEPTHREADATTRDESTROY;
cleanup_server(test);
};
}
}

Expand Down

0 comments on commit 0b1be25

Please sign in to comment.