Skip to content

Free-threading: RAM may not be released to the OS - #9429

Open
crusaderky wants to merge 2 commits into
dask:mainfrom
crusaderky:test-memory-freethreading
Open

crusaderky wants to merge 2 commits into
dask:mainfrom
crusaderky:test-memory-freethreading

Conversation

@crusaderky

@crusaderky crusaderky commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

test_memory dereferences 100 MiB of unmanaged memory and expects that the RSS will drop soon afterwards - i.e. the memory will be returned to the OS.

This is not meant to happen instantaneously on free-threaded builds, which use mimalloc. This is by design, as mimalloc implements a userspace arena that delays releasing memory to the OS by up to 100ms.

There is currently an upstream issue (python/cpython#158972) which causes the RAM to never be released.

Impact

In production, this has the potential to mess up all Dask heuristics based on RSS readings:

  • an idle worker may permanently enter paused state;
  • the Active Memory Manager may perform suboptimal choices, moving away data from a worker with this "ghost" memory.

In other words, this is indistinguishable from a genuine memory leak, which Dask routes around.

I'm still triaging how actually likely this is in production.

Changes

This PR neuters test_memory for the time being, preventing

FAILED distributed/tests/test_scheduler.py::test_memory - AssertionError: Expected 0 MiB <= unmanaged <= 500.55859375 MiB; got:

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Unit Test Results

See test report for an extended history of previous test failures. This is useful for diagnosing flaky tests.

    48 files  ±0      48 suites  ±0   14h 51m 34s ⏱️ + 11m 14s
 4 215 tests ±0   4 029 ✅  - 1    185 💤 ±0  0 ❌ ±0  1 🔥 +1 
98 673 runs  +1  93 394 ✅  - 1  5 278 💤 +1  0 ❌ ±0  1 🔥 +1 

For more details on these errors, see this check.

Results for commit 26b7a94. ± Comparison against base commit 88a19ad.

♻️ This comment has been updated with latest results.

The last section of test_memory leaks 100 MiB strings in each worker,
clears them, and asserts that the process RSS drops accordingly. That
assertion is about the interpreter's allocator, not about distributed:
free-threaded builds allocate all Python objects with mimalloc instead
of pymalloc, and mimalloc never hands those pages back. The RSS of the
worker stays at its peak and the test fails with

    Expected 0 MiB <= unmanaged <= 500.56 MiB; got:
    Process memory (RSS)  : 550.72 MiB
      - unmanaged (old)   : 550.72 MiB

Reproduced standalone on Linux:

    object        GIL 3.14            free-threaded 3.14
    str        212 -> 12 MiB      214 -> 214 MiB
    bytearray  212 -> 12 MiB      314 -> 314 MiB
    bytes      212 -> 12 MiB      314 -> 314 MiB

gc.collect(), repeated collections and waiting 18s after the free change
nothing, and MIMALLOC_PURGE_DELAY=0 does not help for strings. This is a
documented limitation of the free-threaded build:
https://docs.python.org/3/howto/free-threading-python.html#mimalloc-allocator-vs-pymalloc

Skip the section on free-threaded builds, exactly like it is already
skipped on MacOS and Windows, and add a FREE_THREADED flag to
distributed.compatibility for this and future use.
@crusaderky
crusaderky force-pushed the test-memory-freethreading branch from 9f67a12 to 26b7a94 Compare December 3, 2026 21:26
@crusaderky crusaderky changed the title [unsupervised AI] Skip memory-release assertions in test_memory on free-threaded builds Free-threading: do not assert that RAM is released immediately to the OS Dec 3, 2026
@crusaderky
crusaderky marked this pull request as ready for review December 3, 2026 21:30
@crusaderky crusaderky changed the title Free-threading: do not assert that RAM is released immediately to the OS Free-threading: RAM may not be released immediately to the OS Dec 3, 2026
@crusaderky crusaderky changed the title Free-threading: RAM may not be released immediately to the OS Free-threading: RAM may not be released to the OS Dec 3, 2026

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.

1 participant