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

Feature/mvds00/test axon subprocess error checking #2226

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 68 additions & 23 deletions tests/e2e_tests/multistep/test_axon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import sys
import textwrap

import pytest

Expand Down Expand Up @@ -29,6 +30,8 @@

"""

INDENT = " "


@pytest.mark.asyncio
async def test_axon(local_chain):
Expand Down Expand Up @@ -68,35 +71,52 @@ async def test_axon(local_chain):

# register miner
# "python neurons/miner.py --netuid 1 --subtensor.chain_endpoint ws://localhost:9945 --wallet.name wallet.name --wallet.hotkey wallet.hotkey.ss58_address"
cmd = " ".join(
[
f"{sys.executable}",
f'"{template_path}{templates_repo}/neurons/miner.py"',
"--no_prompt",
"--netuid",
str(netuid),
"--subtensor.network",
"local",
"--subtensor.chain_endpoint",
"ws://localhost:9945",
"--wallet.path",
wallet.path,
"--wallet.name",
wallet.name,
"--wallet.hotkey",
"default",
]
)

axon_process = await asyncio.create_subprocess_shell(
cmd,
args = [
f"{template_path}{templates_repo}/neurons/miner.py",
"--no_prompt",
"--netuid",
str(netuid),
"--subtensor.network",
"local",
"--subtensor.chain_endpoint",
"ws://localhost:9945",
"--wallet.path",
wallet.path,
"--wallet.name",
wallet.name,
"--wallet.hotkey",
"default",
]

cmd = sys.executable + " " + " ".join(args)

axon_process = await asyncio.create_subprocess_exec(
sys.executable,
*args,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env={"NEST_ASYNCIO": "0"},
)
logging.info("Neuron Alice is now mining")
await asyncio.sleep(1) # wait for the miner to start up or fail
if axon_process.returncode != None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not instead of !=

stdout, stderr = await axon_process.communicate()
stdout = textwrap.indent(stdout.decode("UTF8"), INDENT)
stderr = textwrap.indent(stderr.decode("UTF8"), INDENT)
assert (
axon_process.returncode == None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is instead of ==

), f"miner process stopped immediately or did not start:\n{cmd}\nstdout:\n{stdout}\nstderr:\n{stderr}"

logging.info(f"Neuron Alice is now mining: {axon_process}")
await asyncio.sleep(
5
) # wait for 5 seconds for the metagraph to refresh with latest data
if axon_process.returncode != None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not instead of !=

stdout, stderr = await axon_process.communicate()
stdout = textwrap.indent(stdout.decode("UTF8"), INDENT)
stderr = textwrap.indent(stderr.decode("UTF8"), INDENT)
assert (
axon_process.returncode == None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is instead of ==

), f"miner process stopped unexpectedly:\n{cmd}\nstdout:\n{stdout}\nstderr:\n{stderr}"

# refresh metagraph
metagraph = bittensor.metagraph(netuid=netuid, network="ws://localhost:9945")
Expand All @@ -109,4 +129,29 @@ async def test_axon(local_chain):
assert updated_axon.port == 8091
assert updated_axon.hotkey == alice_keypair.ss58_address
assert updated_axon.coldkey == alice_keypair.ss58_address

# Terminate miner and make sure nothing bad happened.
if axon_process.returncode == None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is

logging.info("terminating miner")
try:
# try/except to prevent exception in case the process just ends now
axon_process.terminate()
except:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not do bare excepts. Ideally, we should know what we're trying to catch, but if not, at least except Exception as e and log it.

pass
await asyncio.sleep(1)

if axon_process.returncode == None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is

logging.info("killing miner")
try:
# try/except to prevent exception in case the process just ends now
axon_process.kill()
except:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bare except

pass

stdout, stderr = await axon_process.communicate()
stdout = textwrap.indent(stdout.decode("UTF8"), INDENT)
stderr = textwrap.indent(stderr.decode("UTF8"), INDENT)
assert (
"Exception" not in stderr
), f"miner output indicates issue:\nstdout:\n{stdout}\nstderr:\n{stderr}"
logging.info("Passed test_axon")
Loading