fix(validator): serialize TAO source-balance check under axon_lock#456
Merged
Conversation
handle_swap_reserve called provider.get_balance outside axon_lock with a comment claiming the source-chain RPC is a separate connection. That holds for a BTC source (Esplora/Maestro HTTP) but not for a TAO source: the subtensor provider's get_balance runs on the shared axon_subtensor websocket that axon_lock exists to serialize. Every TAO->BTC reserve raced the lock-protected readers, causing recurring 'cannot call recv while another thread is already running recv' errors. Mark substrate-backed providers with uses_substrate and gate the balance check on it: serialize the TAO read under axon_lock, keep BTC's HTTP read lock-free so a slow Esplora call doesn't stall the forward loop.
entrius
approved these changes
Jun 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
The validator logs recurring
cannot call recv while another thread is already running recv or recv_streamingerrors. Root cause: a single substrate websocket call was made outside the lock that serializes that connection.In
handle_swap_reserve(allways/validator/axon_handlers.py), the source-balance checkprovider.get_balance(synapse.from_address)ran outside thewith validator.axon_lock:block. The comment claimed the source-chain RPC is "a separate connection from substrate, so it doesn't need axon_lock."That is true for a BTC source (Esplora/Maestro = HTTP) but false for a TAO source: the subtensor provider's
get_balancecallsself.subtensor.get_balance(...)onaxon_subtensor— the exact websocketaxon_lockexists to serialize. So every TAO->BTC reserve raced the lock-protected readers (other axon handler threads + the forward loop'sbounds_cachereads onaxon_subtensor), tripping the recv collision.Fix
uses_substrate: bool = Falseon the chain-provider base (base.py); setuses_substrate = Trueon the subtensor/TAO provider (subtensor.py). BTC stays at the defaultFalse.axon_lock, keep BTC's HTTP read lock-free so a ~300ms Esplora call can't stall the forward loop. (axon_lockis anRLock, so the later re-acquire in the main block is fine.)Other handlers scanned
Scanned
handle_miner_activateandhandle_swap_confirmfor the same pattern (anyprovider.*/axon_subtensor.*substrate call outsideaxon_lock). No additional leaks found — both wrap all their substrate work (is_hotkey_registered,read_miner_commitment, contract reads/votes,get_current_block,verify_transaction) insidewith validator.axon_lock. The only leak was the reserve-time balance check.Tests
Added
TestSourceBalanceLockintests/test_axon_handlers.py:SubtensorProvider.uses_substrate is True,BitcoinProvider.uses_substrate is False, base defaultFalse;axon_lockaroundget_balance, and that a BTC-sourced reserve does not.Full suite:
689 passed.ruff check+ruff format --checkclean.