Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use good python and don't leave open subtensor websockets! #533

Open
MichaelTrestman opened this issue Mar 12, 2025 · 0 comments
Open

Use good python and don't leave open subtensor websockets! #533

MichaelTrestman opened this issue Mar 12, 2025 · 0 comments

Comments

@MichaelTrestman
Copy link
Collaborator

MichaelTrestman commented Mar 12, 2025

In example python code, we should be using explicit contexts for subtensor instances to avoid connection/memory leaks from leaving these instances open.
Subtensor must be initialized either with keyword with to create a cope, or explicitly closed with close()

If you don't do this correctly, it leaves the websocket open. we do attempt to close the websocket upon garbage collection, but because it's threaded, this is unreliable (see: python-websockets/websockets#1601 (comment))

You don't need to use the context manager and .close() — just one or the other. This is fine:

@thewhaleking for review

import bittensor as bt
with bt.subtensor("finney") as sub:
    # all calls to subtensor instance inside this block

This is also fine, though uglier:

import bittensor as bt
sub = bt.subtensor("finney")
# subtensor calls
sub.close()

This is not fine:

import bittensor as bt
sub = bt.subtensor("finney")
# calls to subtensor
# no close

With async, it's the same. This is fine:

import bittensor as bt
async with bt.AsyncSubtensor() as sub:
    # calls to subtensor

or

import bittensor as bt
sub = bt.AsyncSubtensor()
async with sub:
    # calls to subtensor

or the uglier:

import bittensor as bt
sub = bt.AsyncSubtensor()
await sub.initialize()
# calls to subtensor
await sub.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant