Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

fixed /stop race condition

Alishahryar15d26bdbb9faf
diff --git a/messaging/handler.py b/messaging/handler.py
index 72177f05..7ac54d83 100644
--- a/messaging/handler.py
+++ b/messaging/handler.py
@@ -469,15 +469,20 @@ class ClaudeMessageHandler:
Stop all pending and in-progress tasks.
Order of operations:
- 1. Stop CLI sessions first (kills subprocesses, unblocks I/O)
- 2. Cancel async tasks in tree queue
- 3. Update UI for all affected nodes
+ 1. Set stopping flag to prevent new tasks from starting
+ 2. Cancel tree queue tasks
+ 3. Stop CLI sessions
+ 4. Update UI for all affected nodes
"""
- # 1. Cancel tree queue tasks FIRST
- # This ensures we capture the count of active tasks before they clean up
- logger.info("Cancelling tree queue tasks...")
- cancelled_nodes = await self.tree_queue.cancel_all()
- logger.info(f"Cancelled {len(cancelled_nodes)} nodes")
+ # Set a temporary flag on the tree_queue manager if possible, or just lock everything
+ # Since we are in the handler, we can use the manager's lock to ensure consistency
+
+ async with self.tree_queue._lock:
+ # 1. Cancel tree queue tasks FIRST while holding the manager lock
+ # This ensures we capture the count of active tasks before they clean up
+ logger.info("Cancelling tree queue tasks...")
+ cancelled_nodes = self.tree_queue.cancel_all_sync()
+ logger.info(f"Cancelled {len(cancelled_nodes)} nodes")
# 2. Stop CLI sessions - this kills subprocesses and ensures everything is dead
logger.info("Stopping all CLI sessions...")
@@ -486,7 +491,6 @@ class ClaudeMessageHandler:
# 3. Update UI for all cancelled nodes
for node in cancelled_nodes:
# Fire and forget to avoid blocking the cleanup process
- # Note: We don't await because we are in a massive loop potentially
self.platform.fire_and_forget(
self.platform.queue_edit_message(
node.incoming.chat_id,
diff --git a/messaging/tree_queue.py b/messaging/tree_queue.py
index 68dc75db..c884e069 100644
--- a/messaging/tree_queue.py
+++ b/messaging/tree_queue.py
@@ -271,13 +271,12 @@ class TreeQueueManager:
logger.info(f"Cancelled {len(cancelled_nodes)} nodes in tree {root_id}")
return cancelled_nodes
- async def cancel_all(self) -> List[MessageNode]:
- """Cancel all messages in all trees."""
- async with self._lock:
- all_cancelled = []
- for root_id in list(self._repository.tree_ids()):
- all_cancelled.extend(self.cancel_tree(root_id))
- return all_cancelled
+ def cancel_all_sync(self) -> List[MessageNode]:
+ """Cancel all messages in all trees (synchronous/locked version)."""
+ all_cancelled = []
+ for root_id in list(self._repository.tree_ids()):
+ all_cancelled.extend(self.cancel_tree(root_id))
+ return all_cancelled
def register_node(self, node_id: str, root_id: str) -> None:
"""Register a node ID to a tree (for external mapping)."""