python / python/cpython

Memory held for too long by the BRC queue while the owning thread is detached

Open
#157,838 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core performance topic-free-threading type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

On the free-threaded build, when a non-owner thread decrefs an object and ob_ref_shared drops to zero, the object is queued for its owning thread and is only freed when that thread next runs Python code. If the owner stays detached for a long time — blocked on a lock, in a syscall, waiting on a pipe — the queued objects stay alive for as long as the owner is blocked. The GC drains the queue, but if the objects holds large amounts of memory then it is ineffective.

This was reported in Py314t uses 2× memory: a PyTorch training run uses 10GB on 3.14 and 20GB on 3.14t. The objects are large tensors enqueued on a multiprocessing.Queue, whose feeder thread does the cross-thread decref while the owner sits detached.

I propose to let the queueing thread do the merge itself. When it tries to queue a object of detached owner, it moves the owner from detached to suspended (the same transition stop-the-world uses), merges the refcounts, resumes the owner, and deallocates whatever reached zero outside the bucket mutex.

Linked PRs
  • gh-157839

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by tracing the free-threaded BRC queue and the detached-to-suspended transition, then examine how the GC drains queued objects. Done means the queueing thread can merge references for a detached owner and release objects without waiting for that owner to run Python code; validate with the multiprocessing.Queue workload described in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.