From d494f46739ef2b574aedb83beffe2a3d713bc149 Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Tue, 19 Mar 2024 22:47:34 -0400 Subject: [PATCH] don't clobber global loop policy + propagate exceptions across loops (#1386) --- server/python/plugin_repl.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server/python/plugin_repl.py b/server/python/plugin_repl.py index 1e3414fef..19205325c 100644 --- a/server/python/plugin_repl.py +++ b/server/python/plugin_repl.py @@ -116,12 +116,19 @@ async def eval_async_patched(self: PythonRepl, line: str) -> object: """ scrypted_loop: asyncio.AbstractEventLoop = self.scrypted_loop + def task_done_cb(future: concurrent.futures.Future, task: asyncio.Task): + try: + result = task.result() + future.set_result(result) + except BaseException as e: + future.set_exception(e) + def eval_in_scrypted(future: concurrent.futures.Future, code, *args, **kwargs): try: result = eval(code, *args, **kwargs) if _has_coroutine_flag(code): task = scrypted_loop.create_task(result) - task.add_done_callback(lambda task: future.set_result(task.result())) + task.add_done_callback(partial(task_done_cb, future)) else: future.set_result(result) except BaseException as e: @@ -256,10 +263,7 @@ async def createREPLServer(sdk: ScryptedStatic, plugin: ScryptedDevice) -> int: conn.settimeout(None) filter = conn.recv(1024).decode() server_started_future = concurrent.futures.Future() - - if sys.platform == "win32": - asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) - repl_loop = asyncio.new_event_loop() + repl_loop = asyncio.SelectorEventLoop() # we're not in the main loop, so can't handle any signals anyways repl_loop.add_signal_handler = lambda sig, cb: None