diff --git a/pkg/core/app.py b/pkg/core/app.py index 4b3e3b82..cd140413 100644 --- a/pkg/core/app.py +++ b/pkg/core/app.py @@ -156,6 +156,9 @@ class Application: self.logger.error(f'应用运行致命异常: {e}') self.logger.debug(f'Traceback: {traceback.format_exc()}') + def dispose(self): + self.plugin_connector.dispose() + async def print_web_access_info(self): """打印访问 webui 的提示""" diff --git a/pkg/core/boot.py b/pkg/core/boot.py index aff117e6..548a203e 100644 --- a/pkg/core/boot.py +++ b/pkg/core/boot.py @@ -52,7 +52,7 @@ async def main(loop: asyncio.AbstractEventLoop): def signal_handler(sig, frame): print('[Signal] 程序退出.') - # ap.shutdown() + app_inst.dispose() os._exit(0) signal.signal(signal.SIGINT, signal_handler) diff --git a/pkg/plugin/connector.py b/pkg/plugin/connector.py index 370b7618..c17de9ae 100644 --- a/pkg/plugin/connector.py +++ b/pkg/plugin/connector.py @@ -30,6 +30,8 @@ class PluginRuntimeConnector: stdio_client_controller: stdio_client_controller.StdioClientController + ctrl: stdio_client_controller.StdioClientController | ws_client_controller.WebSocketClientController + runtime_disconnect_callback: typing.Callable[ [PluginRuntimeConnector], typing.Coroutine[typing.Any, typing.Any, None] ] @@ -73,22 +75,22 @@ class PluginRuntimeConnector: self.ap.logger.error('Failed to connect to plugin runtime, trying to reconnect...') await self.runtime_disconnect_callback(self) - ctrl = ws_client_controller.WebSocketClientController( + self.ctrl = ws_client_controller.WebSocketClientController( ws_url=ws_url, make_connection_failed_callback=make_connection_failed_callback, ) - task = ctrl.run(new_connection_callback) + task = self.ctrl.run(new_connection_callback) else: # stdio self.ap.logger.info('use stdio to connect to plugin runtime') # cmd: lbp rt -s python_path = sys.executable env = os.environ.copy() - ctrl = stdio_client_controller.StdioClientController( + self.ctrl = stdio_client_controller.StdioClientController( command=python_path, args=['-m', 'langbot_plugin.cli.__init__', 'rt', '-s'], env=env, ) - task = ctrl.run(new_connection_callback) + task = self.ctrl.run(new_connection_callback) asyncio.create_task(task) @@ -135,3 +137,7 @@ class PluginRuntimeConnector: cmd_ret = command_context.CommandReturn.model_validate(ret) yield cmd_ret + + def dispose(self): + if isinstance(self.ctrl, stdio_client_controller.StdioClientController): + self.ctrl.process.terminate()