Skip to content

Commit 6ec8080

Browse files
committed
Fix weakref bound method
1 parent 96a03b2 commit 6ec8080

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

async_substrate_interface/utils/cache.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,15 @@ async def __call__(self, *args: Any, **kwargs: Any) -> Any:
423423
class _WeakMethod:
424424
"""
425425
Weak reference to a bound method that allows the instance to be garbage collected.
426+
Preserves the method's signature for introspection.
426427
"""
427428

428429
def __init__(self, method):
429430
self._func = method.__func__
430431
self._instance_ref = weakref.ref(method.__self__)
432+
# Store the bound method's signature (without 'self') for inspect.signature() to find.
433+
# We capture this once at creation time to avoid holding references to the bound method.
434+
self.__signature__ = inspect.signature(method)
431435

432436
def __call__(self, *args, **kwargs):
433437
instance = self._instance_ref()

tests/unit_tests/asyncio_/test_substrate_interface.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ async def test_runtime_switching():
147147

148148
@pytest.mark.asyncio
149149
async def test_memory_leak():
150+
import gc
151+
152+
# Stop any existing tracemalloc and start fresh
153+
tracemalloc.stop()
150154
tracemalloc.start()
151155
two_mb = 2 * 1024 * 1024
152156

@@ -160,6 +164,7 @@ async def test_memory_leak():
160164
for i in range(5):
161165
subtensor = await get_async_substrate_interface(LATENT_LITE_ENTRYPOINT)
162166
await subtensor.close()
167+
gc.collect()
163168

164169
snapshot = tracemalloc.take_snapshot()
165170
stats = snapshot.compare_to(baseline_snapshot, "lineno")

tests/unit_tests/sync/test_substrate_interface.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def test_runtime_switching():
9494

9595

9696
def test_memory_leak():
97+
import gc
98+
99+
# Stop any existing tracemalloc and start fresh
100+
tracemalloc.stop()
97101
tracemalloc.start()
98102
two_mb = 2 * 1024 * 1024
99103

@@ -107,6 +111,7 @@ def test_memory_leak():
107111
for i in range(5):
108112
subtensor = SubstrateInterface(LATENT_LITE_ENTRYPOINT)
109113
subtensor.close()
114+
gc.collect()
110115

111116
snapshot = tracemalloc.take_snapshot()
112117
stats = snapshot.compare_to(baseline_snapshot, "lineno")

0 commit comments

Comments
 (0)