Skip to content

Commit 380c5a3

Browse files
authored
Merge pull request #2702 from opentensor/fix/thewhaleking/dendrite-del-method
Dendrite `__del__` method fix
2 parents ef31635 + 8377fb7 commit 380c5a3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

bittensor/core/dendrite.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,14 @@ def __del__(self):
877877
# ... some operations ...
878878
del dendrite # This will implicitly invoke the __del__ method and close the session.
879879
"""
880-
self.close_session()
880+
try:
881+
self.close_session()
882+
except RuntimeError:
883+
if self._session:
884+
logging.debug(
885+
"A Dendrite session was unable to be closed during garbage-collection of the Dendrite object. This "
886+
"usually indicates that you were not using the async context manager."
887+
)
881888

882889

883890
# For back-compatibility with torch

tests/unit_tests/test_dendrite.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ def test_close(setup_dendrite, setup_axon):
8383
assert setup_dendrite._session is None
8484

8585

86+
def test_garbage_collection(setup_dendrite):
87+
del setup_dendrite # should not raise an error
88+
89+
90+
@pytest.mark.asyncio
91+
async def test_async_garbage_collection(setup_dendrite, setup_axon):
92+
async with setup_dendrite as dendrite:
93+
assert (await dendrite.session) is not None
94+
del setup_dendrite # should not raise error
95+
96+
8697
@pytest.mark.asyncio
8798
async def test_aclose(setup_dendrite, setup_axon):
8899
axon = setup_axon

0 commit comments

Comments
 (0)