Skip to content

Commit

Permalink
improve thread cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 21, 2024
1 parent bed158e commit e82a227
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions neb_module_naemon/result_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ void *get_results( gearman_job_st *job, __attribute__((__unused__)) void *contex
double now_f, core_starttime_f, starttime_f, finishtime_f, exec_time, latency;
size_t wsize = 0;

// disable thread cancellation while working on the job
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, NULL);

/* for calculating real latency */
gettimeofday(&now,NULL);

Expand All @@ -147,6 +150,7 @@ void *get_results( gearman_job_st *job, __attribute__((__unused__)) void *contex
workload = (const char *)gearman_job_workload(job);
if(workload == NULL) {
*ret_ptr = GEARMAN_WORK_FAIL;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // restore thread cancellation
return NULL;
}
gm_log( GM_LOG_TRACE, "got result %s\n", gearman_job_handle(job));
Expand Down Expand Up @@ -176,11 +180,13 @@ void *get_results( gearman_job_st *job, __attribute__((__unused__)) void *contex
total_submit_jobs,
total_submit_errors
);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // restore thread cancellation
return((void*)result);
}

if(decrypted_data == NULL) {
*ret_ptr = GEARMAN_WORK_FAIL;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // restore thread cancellation
return NULL;
}
gm_log( GM_LOG_TRACE, "%zu --->\n%s\n<---\n", strlen(decrypted_data), decrypted_data );
Expand All @@ -206,6 +212,7 @@ void *get_results( gearman_job_st *job, __attribute__((__unused__)) void *contex
if ( ( chk_result = ( check_result * )gm_malloc( sizeof *chk_result ) ) == 0 ) {
*ret_ptr = GEARMAN_WORK_FAIL;
gm_free(decrypted_data_c);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // restore thread cancellation
return NULL;
}
init_check_result(chk_result);
Expand Down Expand Up @@ -275,6 +282,7 @@ void *get_results( gearman_job_st *job, __attribute__((__unused__)) void *contex
if ( chk_result->host_name == NULL || chk_result->output == NULL ) {
*ret_ptr= GEARMAN_WORK_FAIL;
gm_log( GM_LOG_ERROR, "discarded invalid job (%s), check your encryption settings\n", gearman_job_handle( job ) );
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // restore thread cancellation
return NULL;
}

Expand Down Expand Up @@ -333,6 +341,7 @@ void *get_results( gearman_job_st *job, __attribute__((__unused__)) void *contex

gm_free(decrypted_data_c);

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // restore thread cancellation
return NULL;
}

Expand Down

0 comments on commit e82a227

Please sign in to comment.