Skip to content

Commit 3614c96

Browse files
sciyoshiclaude
andcommitted
fix: add weakref callback to clean up resolve cache entries
The cache used dict keyed by id(doc) with no cleanup, causing a slow memory leak in long-running processes. Added a weakref finalizer callback that removes the cache entry when the doc is garbage collected. Fixes ISSUES.md #27. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 87f7e69 commit 3614c96

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

prosemirror/model/resolvedpos.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ def resolve_cached(cls, doc: "Node", pos: int) -> "ResolvedPos":
228228

229229
class _ResolveCache:
230230
def __init__(self, doc: "Node") -> None:
231-
self.doc_ref = weakref.ref(doc)
231+
doc_id = id(doc)
232+
self.doc_ref = weakref.ref(
233+
doc,
234+
lambda _ref: _resolve_cache.pop(doc_id, None),
235+
)
232236
self.elts: list[ResolvedPos | None] = [None] * _RESOLVE_CACHE_SIZE
233237
self.i = 0
234238

0 commit comments

Comments
 (0)