From db8ea36ba81fbf1421f7d42e4a249fc51fb478d4 Mon Sep 17 00:00:00 2001 From: JIA-ZI-LONG <1497176841@qq.com> Date: Mon, 30 Mar 2026 13:51:04 +0800 Subject: [PATCH 1/2] refactor(s08): use separate locks for tasks and notification queue Use dedicated locks for BackgroundManager data members: - _tasks_lock: protects self.tasks dictionary - _queue_lock: protects _notification_queue Benefits: - Clearer code: consistent pattern of acquiring lock before write operations - Better concurrency: reading tasks and draining notifications no longer block each other - Safer notification handling: notification append/clear operations are properly synchronized Co-Authored-By: Claude Opus 4.6 --- agents/s08_background_tasks.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/agents/s08_background_tasks.py b/agents/s08_background_tasks.py index 390a77780..6000a1d7e 100644 --- a/agents/s08_background_tasks.py +++ b/agents/s08_background_tasks.py @@ -51,12 +51,14 @@ class BackgroundManager: def __init__(self): self.tasks = {} # task_id -> {status, result, command} self._notification_queue = [] # completed task results - self._lock = threading.Lock() + self._tasks_lock = threading.Lock() # 保护 self.tasks + self._queue_lock = threading.Lock() # 保护 _notification_queue def run(self, command: str) -> str: """Start a background thread, return task_id immediately.""" task_id = str(uuid.uuid4())[:8] - self.tasks[task_id] = {"status": "running", "result": None, "command": command} + with self._tasks_lock: + self.tasks[task_id] = {"status": "running", "result": None, "command": command} thread = threading.Thread( target=self._execute, args=(task_id, command), daemon=True ) @@ -78,9 +80,10 @@ def _execute(self, task_id: str, command: str): except Exception as e: output = f"Error: {e}" status = "error" - self.tasks[task_id]["status"] = status - self.tasks[task_id]["result"] = output or "(no output)" - with self._lock: + with self._tasks_lock: + self.tasks[task_id]["status"] = status + self.tasks[task_id]["result"] = output or "(no output)" + with self._queue_lock: self._notification_queue.append({ "task_id": task_id, "status": status, @@ -90,19 +93,20 @@ def _execute(self, task_id: str, command: str): def check(self, task_id: str = None) -> str: """Check status of one task or list all.""" - if task_id: - t = self.tasks.get(task_id) - if not t: - return f"Error: Unknown task {task_id}" - return f"[{t['status']}] {t['command'][:60]}\n{t.get('result') or '(running)'}" - lines = [] - for tid, t in self.tasks.items(): - lines.append(f"{tid}: [{t['status']}] {t['command'][:60]}") + with self._tasks_lock: + if task_id: + t = self.tasks.get(task_id) + if not t: + return f"Error: Unknown task {task_id}" + return f"[{t['status']}] {t['command'][:60]}\n{t.get('result') or '(running)'}" + lines = [] + for tid, t in self.tasks.items(): + lines.append(f"{tid}: [{t['status']}] {t['command'][:60]}") return "\n".join(lines) if lines else "No background tasks." def drain_notifications(self) -> list: """Return and clear all pending completion notifications.""" - with self._lock: + with self._queue_lock: notifs = list(self._notification_queue) self._notification_queue.clear() return notifs From 6c9ec9a347fc4019d146926e3c58e51e3a5c48c3 Mon Sep 17 00:00:00 2001 From: JIA-ZI-LONG <1497176841@qq.com> Date: Mon, 30 Mar 2026 14:21:27 +0800 Subject: [PATCH 2/2] refactor(s08): change lock comments to English Co-Authored-By: Claude Opus 4.6 --- agents/s08_background_tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agents/s08_background_tasks.py b/agents/s08_background_tasks.py index 6000a1d7e..68b29b6d9 100644 --- a/agents/s08_background_tasks.py +++ b/agents/s08_background_tasks.py @@ -51,8 +51,8 @@ class BackgroundManager: def __init__(self): self.tasks = {} # task_id -> {status, result, command} self._notification_queue = [] # completed task results - self._tasks_lock = threading.Lock() # 保护 self.tasks - self._queue_lock = threading.Lock() # 保护 _notification_queue + self._tasks_lock = threading.Lock() # protect self.tasks + self._queue_lock = threading.Lock() # protect _notification_queue def run(self, command: str) -> str: """Start a background thread, return task_id immediately."""