Skip to content

fix: UnboundLocalError in set_weights_subtensor when exception is raised before assignment #331

@Grizouforever

Description

@Grizouforever

Description

In neurons/validators/weights.py, the set_weights_subtensor function references message (and implicitly expects success) in the except branch, but both variables are only assigned inside the try block:

async def set_weights_subtensor(...):
    try:
        success, message = await subtensor.set_weights(...)  # only assigned here
        return success, message
    except Exception as e:
        bt.logging.error(...)
        return False, message  # UnboundLocalError if exception raised before assignment

If subtensor.set_weights() raises before returning, the except handler attempts to reference message which has never been defined, causing UnboundLocalError: local variable 'message' referenced before assignment.

Steps to Reproduce

Any exception raised by subtensor.set_weights() — e.g., network timeout, invalid netuid, RPC error — will trigger this code path.

Expected Behavior

The except handler returns (False, "") (or similar) gracefully.

Actual Behavior

UnboundLocalError: local variable 'message' referenced before assignment is raised instead of the intended error handling.

Fix

Initialize success = False and message = "" before the try block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions