Skip to content

Commit

Permalink
Changed signal to threading because signal only works in the main thr…
Browse files Browse the repository at this point in the history
…ead, and our multithreaded e2e tests don't work well with it.
  • Loading branch information
thewhaleking committed Nov 22, 2024
1 parent 0346f1b commit 5a69f4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
30 changes: 15 additions & 15 deletions bittensor/core/extrinsics/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module with helper functions for extrinsics."""

import signal
import threading
from typing import TYPE_CHECKING

from substrateinterface.exceptions import SubstrateRequestException, ExtrinsicNotFound
Expand All @@ -13,10 +13,10 @@
from scalecodec.types import GenericExtrinsic


class _SignalTimeoutException(Exception):
class _ThreadingTimeoutException(Exception):
"""
Exception raised for timeout. Different than TimeoutException because this also triggers
a websocket failure. This exception should only be used with `signal.alarm`.
Exception raised for timeout. Different from TimeoutException because this also triggers
a websocket failure. This exception should only be used with `threading` timer..
"""

pass
Expand Down Expand Up @@ -50,33 +50,31 @@ def submit_extrinsic(
extrinsic_hash = extrinsic.extrinsic_hash
starting_block = substrate.get_block()

def _handler(signum, frame):
def _handler():
"""
Timeout handler for signal. Will raise a TimeoutError if timeout is exceeded.
Timeout handler for threading. Will raise a TimeoutError if timeout is exceeded.
"""
logging.error("Timed out waiting for extrinsic submission.")
raise _SignalTimeoutException
raise _ThreadingTimeoutException

try:
# sets a timeout timer for the next call to 200 seconds
# will raise a _SignalTimeoutException if it reaches this point
signal.signal(signal.SIGALRM, _handler)
signal.alarm(200) # two minute timeout
# sets a timeout timer for the next call to 200 seconds
# will raise a _ThreadingTimeoutException if it reaches this point
timer = threading.Timer(200, _handler)

try:
timer.start()
response = substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)
signal.alarm(0) # remove timeout timer
except SubstrateRequestException as e:
logging.error(format_error_message(e.args[0], substrate=substrate))
# Re-rise the exception for retrying of the extrinsic call. If we remove the retry logic, the raise will need
# to be removed.
signal.alarm(0) # remove timeout timer
raise

except _SignalTimeoutException:
except _ThreadingTimeoutException:
after_timeout_block = substrate.get_block()

response = None
Expand All @@ -93,6 +91,8 @@ def _handler(signum, frame):
continue
if response:
break
finally:
timer.cancel()

if response is None:
logging.error(
Expand Down
1 change: 1 addition & 0 deletions tests/e2e_tests/utils/e2e_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def clone_or_update_templates(specific_commit=None):
os.chdir(install_dir)

for repo, git_link in repo_mapping.items():
print(os.path.abspath(repo))
if not os.path.exists(repo):
print(f"\033[94mCloning {repo}...\033[0m")
subprocess.run(["git", "clone", git_link, repo], check=True)
Expand Down

0 comments on commit 5a69f4f

Please sign in to comment.