Skip to content

Commit

Permalink
Merge pull request #1410 from pi-hole/fix/api_threads_cancel
Browse files Browse the repository at this point in the history
Fix storing of API thread IDs
  • Loading branch information
DL6ER authored Aug 21, 2022
2 parents fc2da51 + a709818 commit 832b8c9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/api/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ void listen_telnet(const enum telnet_type type)
for(unsigned int i = 0; i < MAX_API_THREADS; i++)
{
// Spawn telnet thread
pthread_t telnet_connection_thread;
// Create a private copy of the socket fd for the child thread
struct thread_info *tinfo = calloc(1, sizeof(struct thread_info));
if(!tinfo)
Expand All @@ -273,7 +272,7 @@ void listen_telnet(const enum telnet_type type)
tinfo->tid = i;
tinfo->istelnet = (type == TELNETv4 || type == TELNETv6);
tinfo->stype = stype;
if(pthread_create(&telnet_connection_thread, &attr, telnet_connection_handler_thread, (void*) tinfo) != 0)
if(pthread_create(&api_threads[i], &attr, telnet_connection_handler_thread, (void*) tinfo) != 0)
{
// Log the error code description
logg("WARNING: Unable to open telnet processing thread: %s", strerror(errno));
Expand Down
16 changes: 7 additions & 9 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <errno.h>

pthread_t threads[THREADS_MAX] = { 0 };
pthread_t api_threads[TELNET_MAX][MAX_API_THREADS] = {{ 0 }};
pthread_t api_threads[MAX_API_THREADS] = { 0 };
bool resolver_ready = false;

void go_daemon(void)
Expand Down Expand Up @@ -268,18 +268,16 @@ void cleanup(const int ret)
// Do proper cleanup only if FTL started successfully
if(resolver_ready)
{
// Terminate threads
terminate_threads();

// Cancel and join possibly still running API worker threads
for(enum telnet_type tt = 0; tt < TELNET_MAX; tt++)
for(unsigned int tid = 0; tid < MAX_API_THREADS; tid++)
{
for(unsigned int tid = 0; tid < MAX_API_THREADS; tid++)
{
// Otherwise, cancel and join the thread
logg("Joining API worker thread %d/%d", tt,tid);
pthread_cancel(api_threads[tt][tid]);
pthread_join(api_threads[tt][tid], NULL);
}
// Otherwise, cancel and join the thread
logg("Joining API worker thread %d", tid);
pthread_cancel(api_threads[tid]);
pthread_join(api_threads[tid], NULL);
}

// Close database connection
Expand Down
2 changes: 1 addition & 1 deletion src/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "enums.h"
extern pthread_t threads[THREADS_MAX];
#define MAX_API_THREADS 5
extern pthread_t api_threads[TELNET_MAX][MAX_API_THREADS];
extern pthread_t api_threads[MAX_API_THREADS];

void go_daemon(void);
void savepid(void);
Expand Down

0 comments on commit 832b8c9

Please sign in to comment.