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

Handle exceptions in runtime service so unittest can catch them #813

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ def start(self):

def build_actor(self, target_fn: ty.Callable, builder: ty.Union[
ty.Dict['AbstractProcess', 'PyProcessBuilder'], ty.Dict[
SyncDomain, 'RuntimeServiceBuilder']]) -> ty.Any:
SyncDomain, 'RuntimeServiceBuilder']],
exception_q: mp.Queue = None) -> ty.Any:
"""Given a target_fn starts a system (os) process"""
system_process = SystemProcess(target=target_fn,
args=(),
kwargs={"builder": builder})
kwargs={"builder": builder,
"exception_q": exception_q})
system_process.start()
self._actors.append(system_process)
return system_process
Expand Down
25 changes: 19 additions & 6 deletions src/lava/magma/runtime/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from lava.magma.core.run_conditions import (AbstractRunCondition,
RunContinuous, RunSteps)
from lava.magma.compiler.channels.watchdog import WatchdogManagerInterface
from multiprocessing import Queue

"""Defines a Runtime which takes a lava executable and a pluggable message
passing infrastructure (for instance multiprocessing+shared memory or ray in
Expand Down Expand Up @@ -94,13 +95,15 @@ def target_fn(*args, **kwargs):
"""
try:
builder = kwargs.pop("builder")
exception_q = kwargs.pop('exception_q')
actor = builder.build()
# No exception occured
exception_q.put(None)
actor.start(*args, **kwargs)
except Exception as e:
print("Encountered Fatal Exception: " + str(e))
print("Traceback: ")
print(traceback.format_exc())
raise e
e.trace = traceback.format_exc()
exception_q.put(e)
raise(e)


class Runtime:
Expand Down Expand Up @@ -134,6 +137,7 @@ def __init__(self,
self.num_steps: int = 0

self._watchdog_manager = None
self.exception_q = Queue()

def __del__(self):
"""On destruction, terminate Runtime automatically to
Expand All @@ -160,6 +164,13 @@ def initialize(self, node_cfg_idx: int = 0):
self._build_processes()
self._build_runtime_services()
self._start_ports()

# Get Exception from exception queue and raise them in main process
e = self.exception_q.get()
if e:
print(str(e), e.trace)
raise(e)

self.log.debug("Runtime Initialization Complete")
self._is_initialized = True

Expand Down Expand Up @@ -294,7 +305,8 @@ def _build_processes(self):
# Assign current Runtime to process
proc._runtime = self
self._messaging_infrastructure.build_actor(target_fn,
proc_builder)
proc_builder,
self.exception_q)

def _build_runtime_services(self):
"""Builds the runtime services"""
Expand All @@ -303,7 +315,8 @@ def _build_runtime_services(self):
for _, rs_builder in runtime_service_builders.items():
self._messaging_infrastructure. \
build_actor(target_fn,
rs_builder)
rs_builder,
self.exception_q)

def _get_resp_for_run(self):
"""
Expand Down
Loading