Cross-interpreter memory leak in _xidata_release when originating interpreter is destroyed
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
Bug description:
Bug report
Bug summary
There is a known memory leak in Python/crossinterp.c related to the teardown of sub-interpreters when they share data.
When a sub-interpreter packages data into _PyXIData_t and passes it to a receiving interpreter, the receiver is expected to call _PyXIData_Release to free it. However, if the originating interpreter is destroyed before the receiver calls release, the original interpreter's memory context is lost.
In _xidata_release (Python/crossinterp.c around line 1049), the code correctly identifies this state:
PyInterpreterState *interp = _PyInterpreterState_LookUpID(
_PyXIData_INTERPID(xidata));
if (interp == NULL) {
// The interpreter was already destroyed.
// This function shouldn't have been called.
// XXX Someone leaked some memory...
Because the originating interpreter is gone, Python intentionally leaks xidata->obj to avoid a use-after-free segfault.
Proposed Solution Architecture
To resolve this without crashing, we would need to track cross-interpreter data at the interpreter level:
- Modify
PyInterpreterStateto include a thread-safe registry tracking all active_PyXIData_tinstances created by that interpreter. - Register instances during
_PyCode_GetXIDataand deregister them during_xidata_release. - Modify
Py_EndInterpreterteardown to check this registry. If outstanding shared data exists, the shutting-down interpreter must either block until it is released, or forcefully invalidate/free the shared data (using anis_invalidatedflag) before tearing down its ownobmallocstate.
I'm opening this issue to get feedback on the proposed architecture before starting a Pull Request.
CPython versions tested on:
main branch
CPython versions tested on:
CPython main branch
Operating systems tested on:
Windows
Linked PRs
- gh-153745
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit Python/crossinterp.c rund um _xidata_release und verfolge anschließend _PyCode_GetXIData sowie den Abbaupfad von Py_EndInterpreter. Prüfe die verlinkte Arbeit gh-153745, bevor du fortfährst, da dieses Issue um architektonisches Feedback bittet und keine festgelegte Implementierung oder keinen Abschlusstest definiert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- c, python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100