fix(core): break circular reference in Request using weakref to prevent memory leak

Signed-off-by: Kelvin Velasquez <vv22015@ues.edu.sv>
This commit is contained in:
Kelvin Velasquez 2025-12-23 10:30:20 -06:00
parent 38c361f99d
commit b9968373c3

View File

@ -3,6 +3,7 @@
import enum import enum
import time import time
import weakref
from collections.abc import Callable, Mapping from collections.abc import Callable, Mapping
from functools import partial from functools import partial
from typing import TYPE_CHECKING, Any, Optional from typing import TYPE_CHECKING, Any, Optional
@ -132,7 +133,9 @@ class Request:
self.block_hashes: list[BlockHash] = [] self.block_hashes: list[BlockHash] = []
self.get_hash_new_full_blocks: Callable[[], list[BlockHash]] | None = None self.get_hash_new_full_blocks: Callable[[], list[BlockHash]] | None = None
if block_hasher is not None: if block_hasher is not None:
self.get_hash_new_full_blocks = partial(block_hasher, self) # Use weakref to avoid circular reference: Request -> partial -> Request
# This allows immediate reclamation by refcounting without waiting for GC.
self.get_hash_new_full_blocks = partial(block_hasher, weakref.proxy(self))
self.block_hashes = self.get_hash_new_full_blocks() self.block_hashes = self.get_hash_new_full_blocks()
self.skip_reading_prefix_cache = self.get_skip_reading_prefix_cache() self.skip_reading_prefix_cache = self.get_skip_reading_prefix_cache()