8000 gh-94207: Fix struct module leak by mdickinson · Pull Request #94239 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-94207: Fix struct module leak#94239

Merged
mdickinson merged 3 commits intopython:mainfrom
mdickinson:gh-94207-fix-struct-module-leak
Jun 25, 2022
Merged

gh-94207: Fix struct module leak#94239
mdickinson merged 3 commits intopython:mainfrom
mdickinson:gh-94207-fix-struct-module-leak

Conversation

@mdickinson
Copy link
Member
@mdickinson mdickinson commented Jun 24, 2022

Possible fix for the _struct module reference leak reported in #94207. What appears to be happening is that once the _struct cache is nonempty, there's a reference cycle from the module to the cache, to any Struct instance in the cache, to the Struct type, back to the module. Since Struct objects are not GC-tracked, that cycle is never collected.

This PR makes _struct.Struct GC-tracked, and adds a regression test.

This fixes a memory leak in the _struct module, where as soon
as a Struct object is stored in the cache, there's a cycle from
the _struct module to the cache to Struct objects to the Struct
type back to the module. If _struct.Struct is not gc-tracked, that
cycle is never collected.
@miss-islington
Copy link
Contributor

Thanks @mdickinson for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-94265 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Jun 25, 2022
@bedevere-bot
Copy link

GH-94266 is a backport of this pull request to the 3.10 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 25, 2022
Make _struct.Struct a GC type

This fixes a memory leak in the _struct module, where as soon
as a Struct object is stored in the cache, there's a cycle from
the _struct module to the cache to Struct objects to the Struct
type back to the module. If _struct.Struct is not gc-tracked, that
cycle is never collected.

This PR makes _struct.Struct GC-tracked, and adds a regression test.
(cherry picked from commit 6b86534)

Co-authored-by: Mark Dickinson <mdickinson@enthought.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 25, 2022
Make _struct.Struct a GC type

This fixes a memory leak in the _struct module, where as soon
as a Struct object is stored in the cache, there's a cycle from
the _struct module to the cache to Struct objects to the Struct
type back to the module. If _struct.Struct is not gc-tracked, that
cycle is never collected.

This PR makes _struct.Struct GC-tracked, and adds a regression test.
(cherry picked from commit 6b86534)

Co-authored-by: Mark Dickinson <mdickinson@enthought.com>