From f93b2b3e16404136cbda1bdb848925f7e97a625b Mon Sep 17 00:00:00 2001 From: WillB97 Date: Sun, 1 Sep 2024 15:54:30 +0100 Subject: [PATCH] Fix usercode_runner crashing on successful completion of usercode --- simulator/controllers/usercode_runner/usercode_runner.py | 3 ++- simulator/modules/sbot_interface/socket_server.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/simulator/controllers/usercode_runner/usercode_runner.py b/simulator/controllers/usercode_runner/usercode_runner.py index 544ee13..6621c75 100644 --- a/simulator/controllers/usercode_runner/usercode_runner.py +++ b/simulator/controllers/usercode_runner/usercode_runner.py @@ -199,10 +199,11 @@ def main() -> bool: # Run cleanup code registered in the usercode atexit._run_exitfuncs() # noqa: SLF001 # Cleanup devices + devices.completed = True devices.stop_event.set() return True if __name__ == '__main__': - exit(main()) + exit(0 if main() else 1) diff --git a/simulator/modules/sbot_interface/socket_server.py b/simulator/modules/sbot_interface/socket_server.py index a51c7d0..c820de9 100644 --- a/simulator/modules/sbot_interface/socket_server.py +++ b/simulator/modules/sbot_interface/socket_server.py @@ -154,6 +154,8 @@ def __init__(self, devices: list[DeviceServer]) -> None: self.devices = devices self.stop_event = Event() g.stop_event = self.stop_event + # flag to indicate that we are exiting because the usercode has completed + self.completed = False def run(self) -> None: """ @@ -199,8 +201,9 @@ def run(self) -> None: for device in self.devices: device.close() - # Stop the usercode - os.kill(os.getpid(), signal.SIGINT) + if self.stop_event.is_set() and self.completed is False: + # Stop the usercode + os.kill(os.getpid(), signal.SIGINT) def links(self) -> dict[str, dict[str, str]]: """Return a mapping of asset tags to ports, grouped by board type."""