From f2c4288d2dd7da469e763a3d43e299d78b7a1aa1 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 25 Jan 2024 00:17:05 -0800 Subject: [PATCH] Run pthread_atfork only once Close #7704 --- native/src/core/daemon.cpp | 1 + native/src/core/include/core.hpp | 1 + native/src/core/thread.cpp | 6 ++++-- native/src/core/zygisk/main.cpp | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/native/src/core/daemon.cpp b/native/src/core/daemon.cpp index 3472a1b64..6e739995f 100644 --- a/native/src/core/daemon.cpp +++ b/native/src/core/daemon.cpp @@ -433,6 +433,7 @@ static void daemon_entry() { register_poll(&main_socket_pfd, handle_request); // Loop forever to listen for requests + init_thread_pool(); poll_loop(); } diff --git a/native/src/core/include/core.hpp b/native/src/core/include/core.hpp index 429c70640..3051c36c2 100644 --- a/native/src/core/include/core.hpp +++ b/native/src/core/include/core.hpp @@ -52,6 +52,7 @@ void unregister_poll(int fd, bool auto_close); void clear_poll(); // Thread pool +void init_thread_pool(); void exec_task(std::function &&task); // Daemon handlers diff --git a/native/src/core/thread.cpp b/native/src/core/thread.cpp index d9a2e0c37..fff315839 100644 --- a/native/src/core/thread.cpp +++ b/native/src/core/thread.cpp @@ -42,8 +42,6 @@ static void reset_pool() { } static void *thread_pool_loop(void * const is_core_pool) { - pthread_atfork(nullptr, nullptr, &reset_pool); - // Block all signals sigset_t mask; sigfillset(&mask); @@ -83,6 +81,10 @@ static void *thread_pool_loop(void * const is_core_pool) { } } +void init_thread_pool() { + pthread_atfork(nullptr, nullptr, &reset_pool); +} + void exec_task(function &&task) { mutex_guard g(lock); pending_task.swap(task); diff --git a/native/src/core/zygisk/main.cpp b/native/src/core/zygisk/main.cpp index 85e70d768..f66a857f7 100644 --- a/native/src/core/zygisk/main.cpp +++ b/native/src/core/zygisk/main.cpp @@ -15,6 +15,8 @@ static void zygiskd(int socket) { if (getuid() != 0 || fcntl(socket, F_GETFD) < 0) exit(-1); + init_thread_pool(); + #if defined(__LP64__) set_nice_name("zygiskd64"); LOGI("* Launching zygiskd64\n");