Skip to content

static_thread_pool: stop leaking the NUMA node table - #2475

Open
aarochu wants to merge 1 commit into
NVIDIA:mainfrom
aarochu:fix-numa-policy-leak
Open

aarochu wants to merge 1 commit into
NVIDIA:mainfrom
aarochu:fix-numa-policy-leak

Conversation

@aarochu

@aarochu aarochu commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Hi! This is for #1370, the NUMA policy in static_thread_pool leaking an object at shutdown.

What was going on

default_numa_policy kept its node -> thread-index table in a function-local static std::vector<int> wrapped in __indestructible, so it was never destroyed on purpose. #1369 did that to dodge a static destruction order problem, back when the policy was a thread_local object that the pool held by pointer.

But the policy is a type-erased value type now and the pool stores its own copy, so the table doesn't need to outlive anything except the pool that uses it. The global is leftover from the old design, and since it's immortal it leaks one block every time a shared library that uses static_thread_pool is unloaded. LeakSanitizer can't flag it because the static still points at the block while the library is loaded, which is probably why it went unnoticed.

The fix

The table is now a member of default_numa_policy, built in its constructor with the same logic as before, so its lifetime is tied to the pool that holds the policy. That removes _node_to_thread_index and the __indestructible use there. I kept the nodemask::any() one since nodemask is trivially destructible, so it can't leak.

How I checked it

  • Reproduced the leak first: a plugin .so that builds a static_thread_pool (with STDEXEC_ENABLE_NUMA on), loaded and unloaded 300 times with dlopen/dlclose, confirming each time that the library really unloaded. Heap in use grew by 32 bytes per cycle (9600 bytes over 300 cycles) before the change and by 0 bytes after, using the same script back to back.
  • get_numa_policy() gives the same results before and after (node count, per-node CPU count, thread_index_to_node for the first 64 indices), and a copy of the policy behaves the same.
  • test_static_thread_pool.cpp passes with NUMA enabled (11 test cases), and the two benchmark examples that derive from default_numa_policy still compile.

Some caveats: I only have a single-NUMA-node machine (WSL), so I couldn't exercise a multi-node layout. The calculation itself is unchanged and I'd welcome a run on real multi-node hardware. There's also no regression test in the repo, since NUMA is off by default and showing the leak needs a dlopen loop. Happy to add one if you'd like. Also, the table is now computed once per policy rather than once per process, so each get_numa_policy() call does a few libnuma queries. Pool creation already spawns threads, so I don't expect that to matter.

default_numa_policy kept its node -> thread-index table in a
function-local static std::vector wrapped in __indestructible, so it was
deliberately never destroyed. That was introduced to avoid a static
destruction order problem back when the policy was a thread_local object
that a pool referred to by pointer.

The policy is a value type now and the pool owns its copy, so there is no
reason for the table to be global anymore. Because it was never
destroyed, the heap block was leaked every time a shared library using
static_thread_pool was unloaded (dlopen/dlclose in a loop grew the heap
by one block per cycle). LeakSanitizer can't see it either, since the
static keeps pointing at the block while the library is loaded.

Make the table a member of default_numa_policy, computed in its
constructor, so its lifetime is tied to the pool that uses it. Behavior
is unchanged: the same values are computed the same way.

Fixes NVIDIA#1370
@copy-pr-bot

copy-pr-bot Bot commented Nov 22, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ericniebler

Copy link
Copy Markdown
Collaborator

/ok to test b5ea160

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants