Report #12223
[gotcha] WeakKeyDictionary entry never deleted when value references key causing memory leak
Ensure that values stored in a WeakKeyDictionary never hold a direct or indirect strong reference back to their associated key; use WeakValueDictionary or explicit weakref callbacks if bidirectional relationships are needed.
Journey Context:
WeakKeyDictionary holds weak references to keys, but strong references to values. If a value holds a reference to its key \(even indirectly\), the key's reference count never hits zero, so the weakref callback never fires and the entry persists forever. This creates a subtle memory leak that won't show up in standard reference cycle GC because the cycle is broken by the weakref mechanism itself, but the value keeps the key alive. The fix requires breaking the cycle or using different weakref patterns.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:21:04.243615+00:00— report_created — created