diff options
author | Waldemar Zimpel <w.zimpel@dev.utilizer.de> | 2024-09-26 03:37:19 +0200 |
---|---|---|
committer | David Goulet <dgoulet@torproject.org> | 2024-10-10 09:55:46 -0400 |
commit | 6feaea8fa44d4594388232f4e104a7c656013e19 (patch) | |
tree | 4ded9a86ed8eb13c32d5feff7f7a64bbf0ab48c3 /src/core | |
parent | 93df26b11a67423e05c200cd93cba6398b9f2a49 (diff) | |
download | tor-6feaea8fa44d4594388232f4e104a7c656013e19.tar.gz tor-6feaea8fa44d4594388232f4e104a7c656013e19.zip |
Fix: Memory leaks in cpuworker on shutdown
Resources allocated by cpuworker weren't being freed on clean shutdown.
This applies for worker threads, worker thread pool, reply queue, reply
event, ...
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/mainloop/cpuworker.c | 47 | ||||
-rw-r--r-- | src/core/mainloop/cpuworker.h | 3 |
2 files changed, 26 insertions, 24 deletions
diff --git a/src/core/mainloop/cpuworker.c b/src/core/mainloop/cpuworker.c index a42dbb528d..b12879a178 100644 --- a/src/core/mainloop/cpuworker.c +++ b/src/core/mainloop/cpuworker.c @@ -1,6 +1,6 @@ /* Copyright (c) 2003-2004, Roger Dingledine. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2021, The Tor Project, Inc. */ + * Copyright (c) 2007-2024, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -74,7 +74,6 @@ worker_state_free_void(void *arg) worker_state_free_(arg); } -static replyqueue_t *replyqueue = NULL; static threadpool_t *threadpool = NULL; static uint32_t total_pending_tasks = 0; @@ -120,31 +119,33 @@ cpuworker_consensus_has_changed(const networkstatus_t *ns) void cpuworker_init(void) { - if (!replyqueue) { - replyqueue = replyqueue_new(0); - } - if (!threadpool) { - /* - In our threadpool implementation, half the threads are permissive and - half are strict (when it comes to running lower-priority tasks). So we - always make sure we have at least two threads, so that there will be at - least one thread of each kind. - */ - const int n_threads = MAX(get_num_cpus(get_options()), 2); - threadpool = threadpool_new(n_threads, - replyqueue, - worker_state_new, - worker_state_free_void, - NULL); - - int r = threadpool_register_reply_event(threadpool, NULL); - - tor_assert(r == 0); - } + /* + In our threadpool implementation, half the threads are permissive and + half are strict (when it comes to running lower-priority tasks). So we + always make sure we have at least two threads, so that there will be at + least one thread of each kind. + */ + const int n_threads = MAX(get_num_cpus(get_options()), 2); + threadpool = threadpool_new(n_threads, + replyqueue_new(0), + worker_state_new, + worker_state_free_void, + NULL); + + int r = threadpool_register_reply_event(threadpool, NULL); + + tor_assert(r == 0); set_max_pending_tasks(NULL); } +/** Free all resources allocated by cpuworker. */ +void +cpuworker_free_all(void) +{ + threadpool_free(threadpool); +} + /** Return the number of threads configured for our CPU worker. */ unsigned int cpuworker_get_n_threads(void) diff --git a/src/core/mainloop/cpuworker.h b/src/core/mainloop/cpuworker.h index 7821f5612f..7f00f363fe 100644 --- a/src/core/mainloop/cpuworker.h +++ b/src/core/mainloop/cpuworker.h @@ -1,7 +1,7 @@ /* Copyright (c) 2001 Matej Pfajfar. * Copyright (c) 2001-2004, Roger Dingledine. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2021, The Tor Project, Inc. */ + * Copyright (c) 2007-2024, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -13,6 +13,7 @@ #define TOR_CPUWORKER_H void cpuworker_init(void); +void cpuworker_free_all(void); void cpuworkers_rotate_keyinfo(void); void cpuworker_consensus_has_changed(const networkstatus_t *ns); |