python / python/cpython

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

Offen
#157,838 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core performance topic-free-threading type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue nennt keine Dateien oder Tests. Beginne damit, die free-threaded BRC queue und den Übergang von detached zu suspended nachzuverfolgen, und untersuche anschließend, wie der GC Objekte aus der Warteschlange leert. Als abgeschlossen gilt, dass der queueing thread Referenzen für einen detached owner zusammenführen und Objekte freigeben kann, ohne darauf zu warten, dass dieser owner Python-Code ausführt; validiere dies mit dem im Bericht beschriebenen multiprocessing.Queue-Workload.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
operating-systems, performance
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.