From 6cbd21542cf7a751e6f0f38864e7e9eae8fa3b14 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Tue, 27 Oct 2026 21:32:24 +0100 Subject: Plug in the new per-thread stats counter, also make sure that the ot_clean sleep does not stall tracker exit --- opentracker.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'opentracker.c') diff --git a/opentracker.c b/opentracker.c index eb97b42..34b8fd5 100644 --- a/opentracker.c +++ b/opentracker.c @@ -43,15 +43,18 @@ uint32_t g_tracker_id; _Atomic int g_opentracker_running = 1; int g_self_pipe[2]; - static void panic(const char *routine) __attribute__((noreturn)); static void panic(const char *routine) { fprintf(stderr, "%s: %s\n", routine, strerror(errno)); exit(111); } +/* Say we are stopping, wake the loop, and let main do the work on its own stack. */ static void signal_handler(int s) { if (s == SIGINT) { + const char byte = 0; + int saved = errno; + /* Any new interrupt signal quits the application */ signal(SIGINT, SIG_DFL); @@ -59,13 +62,8 @@ static void signal_handler(int s) { but cancel their operations and return */ g_opentracker_running = 0; - trackerlogic_deinit(); - -#ifdef WANT_SYSLOGS - closelog(); -#endif - - exit(0); + (void)!write(g_self_pipe[1], &byte, 1); + errno = saved; } } @@ -207,7 +205,7 @@ static void handle_write(const int64_t sock) { handle_dead(sock); } -static void handle_accept(const int64_t serversocket) { +static void handle_accept(const int64_t serversocket, struct ot_workstruct *ws) { struct http_data *cookie; int64_t sock; ot_ip6 ip; @@ -231,7 +229,7 @@ static void handle_accept(const int64_t serversocket) { continue; } - stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); + stats_issue_event(ws, EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); } } @@ -248,6 +246,8 @@ static void *server_mainloop(void *args) { (void)args; /* Initialize our "thread local storage" */ + memset(&ws, 0, sizeof(ws)); + stats_counters_register(&ws); ws.inbuf = malloc(G_INBUF_SIZE); ws.outbuf = malloc(G_OUTBUF_SIZE); #ifdef _DEBUG_HTTPERROR @@ -259,7 +259,7 @@ static void *server_mainloop(void *args) { random_bytes(ws.rand48_state, sizeof(ws.rand48_state)); - for (;;) { + while (g_opentracker_running) { ot_event ready[OT_EVENT_BATCH]; int64_t sock; int count, i; @@ -278,7 +278,7 @@ static void *server_mainloop(void *args) { cookie = fdstate_cookie((int)sock); if ((intptr_t)cookie == FLAG_TCP) - handle_accept(sock); + handle_accept(sock, &ws); else if ((intptr_t)cookie == FLAG_UDP) handle_udp6(sock, &ws); else if ((intptr_t)cookie == FLAG_SELFPIPE) @@ -306,6 +306,16 @@ static void *server_mainloop(void *args) { livesync_ticker(); } + + /* This work struct is about to go out of scope, so hand its tally over + before anything can read it there */ + stats_counters_retire(&ws); + + free(ws.inbuf); + free(ws.outbuf); +#ifdef _DEBUG_HTTPERROR + free(ws.debugbuf); +#endif return 0; } @@ -510,6 +520,13 @@ int main(int argc, char **argv) { server_mainloop(0); + /* The loop has left, so no thread is serving any more and the teardown + can run where a debugger and the sanitizers can follow it */ + trackerlogic_deinit(); +#ifdef WANT_SYSLOGS + closelog(); +#endif + event_deinit(); fdstate_deinit(); return 0; -- cgit v1.3