Skip to content

Commit

Permalink
shutdown threads earlier
Browse files Browse the repository at this point in the history
naemon unloads modules at the end of the shutdown process, so we
need to shutdown threads earlier already when receiving the end of
eventloop event. After that event, we must not access objects anymore.
  • Loading branch information
sni committed Nov 14, 2024
1 parent 6c78fa2 commit bdcb172
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ time_t g_last_log_rotation = -1;
int g_eventloopstarted = false;

void *client_thread(void *data __attribute__ ((__unused__)));
void deregister_callbacks();

static void reap_client_threads()
{
Expand Down Expand Up @@ -371,8 +372,10 @@ void close_socket()
{
if (!g_use_inet_socket)
unlink(g_socket_addr);
iobroker_close(nagios_iobs, g_socket_fd);
g_socket_fd = -1;
if (g_socket_fd != -1) {
iobroker_close(nagios_iobs, g_socket_fd);
g_socket_fd = -1;
}
}

int broker_host(int event_type __attribute__ ((__unused__)), void *data __attribute__ ((__unused__)))
Expand Down Expand Up @@ -483,9 +486,33 @@ static void schedule_timeperiods_cache_update(struct nm_event_execution_properti
return;
}

void shutdown_threads() {
size_t i;

g_should_terminate = true;
close_socket();

/*
* Make sure all client connections are terminated before
* returning control. Since we touch global naemon state everywhere,
* this is strictly mandatory.
*/
if(g_client_threads != NULL) {
for (i = 0; i < g_num_client_threads; ++i) {
if (pthread_join(*(g_client_threads[i]), NULL) != 0) {
logger(LG_ERR, "Failed to join with client thread");
}
free(g_client_threads[i]);
}
free(g_client_threads);
g_client_threads = NULL;
}
}

int broker_process(int event_type __attribute__ ((__unused__)), void *data)
{
int ret;

struct nebstruct_process_struct *ps = (struct nebstruct_process_struct *)data;
if (ps->type == NEBTYPE_PROCESS_EVENTLOOPSTART) {
g_eventloopstarted = true;
Expand All @@ -498,6 +525,12 @@ int broker_process(int event_type __attribute__ ((__unused__)), void *data)
return ERROR;
}
}
if (ps->type == NEBTYPE_PROCESS_EVENTLOOPEND) {
logger(LG_INFO, "deinitializing");
g_eventloopstarted = false;
deregister_callbacks();
shutdown_threads();
}
return OK;
}

Expand Down Expand Up @@ -810,26 +843,8 @@ int nebmodule_init(int flags __attribute__ ((__unused__)), char *args, void *han

int nebmodule_deinit(int flags __attribute__ ((__unused__)), int reason __attribute__ ((__unused__)))
{
size_t i;
logger(LG_INFO, "deinitializing");
g_should_terminate = true;
close_socket();

/*
* Make sure all client connections are terminated before
* returning control. Since we touch global naemon state everywhere,
* this is strictly mandatory.
*/
for (i = 0; i < g_num_client_threads; ++i) {
if (pthread_join(*(g_client_threads[i]), NULL) != 0) {
logger(LG_ERR, "Failed to join with client thread");
}
free(g_client_threads[i]);
}
free(g_client_threads);

shutdown_threads();
store_deinit();
deregister_callbacks();
close_logfile();
return 0;
}

0 comments on commit bdcb172

Please sign in to comment.