Refactor queue core and fix orphaned task cleanup#7
Merged
Conversation
- Create queue_core.py with shared database, logging, and cleanup logic - Refactor task_queue.py and tq.py to use shared module - Fix Ctrl+C leaving orphaned waiting tasks by splitting registration from wait - Stream output directly to file with bounded deque for memory efficiency - Add signal handling integration tests for tq CLI Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
- Add is_task_queue_process() to detect if PID is actually running task_queue vs being reused by an unrelated process (Chrome, etc.) - Add SERVER_INSTANCE_ID and _active_task_ids tracking in MCP server to detect orphaned tasks from disconnected clients - Add CLI_INSTANCE_ID tracking in tq.py for same protection - Handle asyncio.CancelledError to properly clean up when sub-agents are cancelled - Add server_id column to queue table with migration for existing databases - Fix wait time calculation to use POLL_INTERVAL_WAITING instead of hardcoded value This fixes the bug where cancelled sub-agents would leave orphaned tasks in the queue that blocked all subsequent tasks from running. Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Move queue_core import before module-level constants in task_queue.py and tq.py - Remove unused imports and variables in tests Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Pin pypa/gh-action-pypi-publish to commit SHA (resolves unpinned action alert) - Add nosec B602 comments explaining intentional shell=True usage: - CLI tool executes user-provided commands (like bash -c or make) - Shell features (pipes, redirects, globs) required for build commands - Input controlled by users who explicitly invoke the CLI/MCP tool Co-Authored-By: Claude Opus 4.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
queue_core.pyfor code reuse between MCP server and CLIserver_id) to detect orphaned tasks from crashed/restarted processesChanges
New:
queue_core.pyShared infrastructure used by both
task_queue.py(MCP server) andtq.py(CLI):QueuePathsdataclass for consistent path managementget_db(),init_db(),ensure_db()is_process_alive(),is_task_queue_process(),kill_process_tree()cleanup_queue()with orphan detectionBug Fix: Orphaned Task Cleanup
Problem: When sub-agents are cancelled, their queued tasks remained in the database forever, blocking subsequent tasks.
Solution:
is_task_queue_process(pid)- Checks if a PID is actually running task_queue vs being reused by Chrome/Safari/etc.SERVER_INSTANCE_ID/CLI_INSTANCE_ID- Unique ID per process instance to detect stale tasks even if PID is reused_active_task_idstracking - Server tracks which tasks are actively being processedasyncio.CancelledErrorhandling - Properly cleans up when MCP clients disconnectOther Improvements
server_idcolumn to queue table (with migration for existing DBs)POLL_INTERVAL_WAITINGinstead of hardcoded valueTest plan
demo/test_pid_reuse.pyverifies PID reuse detection works correctly🤖 Generated with Claude Code