You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reference leak in code object with reference loop #148722
Supporting cycles in code objects adds a lot of complexity to marshal and extra work for the GC to do, which is going to slow down everything.
I think it would be better to warn that introducing a cycle with code_object.replace will leak memory. code_object.replace is a bit of an odd thing, and almost all code objects live as long as the interpreter anyway.
Alternatively, and this is a lot more complex, we could reject calls to code_object.replace that create cycles.
@markshannon We discussed this today, it seems like the GC overhead should be minimal (visiting the consts tuple will almost always bail early for a non-GC tracked consts) and marshal shouldn't be impacted - we still can't create cycles without adding a type which marshal doesn't support. It looks like the PR still applies cleanly too.
If the additional GC overhead were problematic we could also consider just not tracking VM created code objects known not to have a cycle.
I suggest extracting notify_code_watchers from init_code and calling it immediately after. This ensures that future changes to init_code won't cause an early return before the code object co is tracked.
Activity
markshannon commentedon Jun 11, 2026
Supporting cycles in code objects adds a lot of complexity to
marshaland extra work for the GC to do, which is going to slow down everything.I think it would be better to warn that introducing a cycle with
code_object.replacewill leak memory.code_object.replaceis a bit of an odd thing, and almost all code objects live as long as the interpreter anyway.Alternatively, and this is a lot more complex, we could reject calls to
code_object.replacethat create cycles.serhiy-storchaka commentedon Jun 11, 2026
At first glance #151155 looks correct to me. Does it add too much overhead in the GIL-enabled build?
serhiy-storchaka commentedon Jun 11, 2026
I am fine with just adding a tiny warn in the documentation.
DinoV commentedon Nov 12, 2026
@markshannon We discussed this today, it seems like the GC overhead should be minimal (visiting the consts tuple will almost always bail early for a non-GC tracked consts) and marshal shouldn't be impacted - we still can't create cycles without adding a type which marshal doesn't support. It looks like the PR still applies cleanly too.
If the additional GC overhead were problematic we could also consider just not tracking VM created code objects known not to have a cycle.
sergey-miryanov commentedon Nov 12, 2026
#151155 Looks good.
I suggest extracting
notify_code_watchersfrominit_codeand calling it immediately after. This ensures that future changes toinit_codewon't cause an early return before the code objectcois tracked.