From d252a631d292afc492b337c8b32a34b86139f99d Mon Sep 17 00:00:00 2001 From: webbeef Date: Mon, 6 Jan 2025 19:28:30 -0800 Subject: [PATCH] Fix a leak in MacOS thread count function (#34862) Signed-off-by: webbeef --- ports/servoshell/build.rs | 1 + ports/servoshell/platform/macos/count_threads.c | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ports/servoshell/build.rs b/ports/servoshell/build.rs index cdedf953519..c401016201e 100644 --- a/ports/servoshell/build.rs +++ b/ports/servoshell/build.rs @@ -56,6 +56,7 @@ fn main() -> Result<(), Box> { #[cfg(not(windows))] panic!("Cross-compiling to windows is currently not supported"); } else if target_os == "macos" { + println!("cargo:rerun-if-changed=platform/macos/count_threads.c"); cc::Build::new() .file("platform/macos/count_threads.c") .compile("count_threads"); diff --git a/ports/servoshell/platform/macos/count_threads.c b/ports/servoshell/platform/macos/count_threads.c index bc3328d278d..cd03ee63677 100644 --- a/ports/servoshell/platform/macos/count_threads.c +++ b/ports/servoshell/platform/macos/count_threads.c @@ -8,6 +8,13 @@ int macos_count_running_threads() { task_t task = current_task(); thread_act_array_t threads; mach_msg_type_number_t tcnt; - task_threads(task, &threads, &tcnt); + const kern_return_t status = task_threads(task, &threads, &tcnt); + if (status == KERN_SUCCESS) { + // Free data structures attached to the thread list. + for (uint32_t t = 0; t < tcnt; t++) { + mach_port_deallocate(task, threads[t]); + } + vm_deallocate(task, (vm_address_t)threads, sizeof(thread_t) * tcnt); + } return tcnt; }