From 246b3f3f83cd43af2ac18b35bcbeb18aad7d7178 Mon Sep 17 00:00:00 2001 From: Mix <32300164+mnixry@users.noreply.github.com> Date: Fri, 16 Dec 2022 17:04:05 +0800 Subject: [PATCH] :bug: Fix `samefile` detect error when file not exist, fix #401 --- nonebot_plugin_gocqhttp/process/process.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/nonebot_plugin_gocqhttp/process/process.py b/nonebot_plugin_gocqhttp/process/process.py index b18e7b98..a429c696 100644 --- a/nonebot_plugin_gocqhttp/process/process.py +++ b/nonebot_plugin_gocqhttp/process/process.py @@ -171,20 +171,18 @@ async def _find_duplicate_process(self): exe = Path(process.exe()).absolute() except psutil.Error: continue - - if Path(exe).is_file() and ( - BINARY_PATH.absolute().samefile(exe) - or self.cwd.absolute().samefile(cwd) - ): + if not (exe.is_file() and cwd.is_dir()): + continue + if BINARY_PATH.absolute().samefile(exe) and self.cwd.samefile(cwd): process.terminate() - return pid + yield pid return async def start(self): if self.worker_thread_running: raise ProcessAlreadyStarted - if duplicate_pid := await self._find_duplicate_process(): + async for duplicate_pid in self._find_duplicate_process(): logger.warning(f"Possible {duplicate_pid=} found, terminated.") if not self.config.exists: