agent-zero/python/api
Rafael Uzarowski 784fe5589a
Fix: proper task cancellation in scheduler, leakage in defer.py
*   **Mechanism**: `EventLoopThread.terminate()` now correctly stops the asyncio loop and joins the thread, removing it from the global registry.
*   **Cleanup**: `DeferredTask.kill(terminate_thread=True)` now invokes `_drain_event_loop_tasks()`, which runs **inside** the target thread to explicitly cancel and await all pending tasks (including monologue loops) before killing the thread. This prevents "Task was destroyed but it is pending" warnings and ensures clean exits.

*   **Tracking**: The scheduler now maintains a live registry (`_running_deferred_tasks`) of active `DeferredTask` objects, protected by a reentrant lock.
*   **State Management**: The `run_task` wrapper uses `asyncio.shield` to ensure that even when a task is cancelled (e.g., by user action), the task state is reliably reset to `IDLE` in the database, preventing tasks from getting stuck in `RUNNING` state.

The fix is correctly propagated to all relevant destruction points using `terminate_thread=True`:
*   **Dedicated Context**: `scheduler_task_delete.py` cancels the specific running task and terminates its thread.
*   **Shared/Dedicated Context**: Both `chat_remove.py` (Delete Chat) and `chat_reset.py` (Reset Chat) now call `scheduler.cancel_tasks_by_context(...)`. This ensures that if a scheduler task is running in a chat window (monologue), resetting that chat immediately kills the background thread and stops the agent loop.
2025-11-19 12:50:43 +01:00
..
api_files_get.py feat: External API Endpoints 2025-07-27 14:40:36 +02:00
api_log_get.py projects continued 2025-11-03 16:17:12 +01:00
api_message.py projects continued 2025-11-03 16:17:12 +01:00
api_reset_chat.py projects continued 2025-11-03 16:17:12 +01:00
api_terminate_chat.py projects continued 2025-11-03 16:17:12 +01:00
backup_create.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
backup_get_defaults.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
backup_inspect.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
backup_preview_grouped.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
backup_restore.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
backup_restore_preview.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
backup_test.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
chat_create.py projects continued 2025-11-03 16:17:12 +01:00
chat_export.py projects continued 2025-11-03 16:17:12 +01:00
chat_files_path_get.py frontend file browsers, css colors, litellm update, reqs split 2025-11-09 20:56:31 +01:00
chat_load.py v0.8.1 release 2025-01-17 22:50:17 +01:00
chat_remove.py Fix: proper task cancellation in scheduler, leakage in defer.py 2025-11-19 12:50:43 +01:00
chat_reset.py Fix: proper task cancellation in scheduler, leakage in defer.py 2025-11-19 12:50:43 +01:00
csrf_token.py csrf ALLOWED_ORIGINS protection 2025-10-06 12:20:15 +02:00
ctx_window_get.py projects continued 2025-11-03 16:17:12 +01:00
delete_work_dir_file.py v0.8.1 release 2025-01-17 22:50:17 +01:00
download_work_dir_file.py Better download (#628) 2025-07-31 13:51:25 +02:00
file_info.py v0.8.1 release 2025-01-17 22:50:17 +01:00
get_work_dir_files.py projects continued 2025-11-03 16:17:12 +01:00
health.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
history_get.py projects continued 2025-11-03 16:17:12 +01:00
image_get.py image_get fix, build examples 2025-07-30 09:20:36 +02:00
import_knowledge.py projects continued 2025-11-03 16:17:12 +01:00
knowledge_path_get.py frontend file browsers, css colors, litellm update, reqs split 2025-11-09 20:56:31 +01:00
knowledge_reindex.py frontend file browsers, css colors, litellm update, reqs split 2025-11-09 20:56:31 +01:00
mcp_server_get_detail.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
mcp_server_get_log.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
mcp_servers_apply.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
mcp_servers_status.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
memory_dashboard.py projects continued 2025-11-03 16:17:12 +01:00
message.py update checker 2025-11-17 11:32:15 +01:00
message_async.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
notification_create.py notifications polishing 2025-08-04 13:07:25 +02:00
notifications_clear.py notifications polishing 2025-08-04 13:07:25 +02:00
notifications_history.py feat: notifications backend system and frontend display 2025-07-12 16:32:29 +02:00
notifications_mark_read.py feat: notifications backend system and frontend display 2025-07-12 16:32:29 +02:00
nudge.py projects continued 2025-11-03 16:17:12 +01:00
pause.py projects continued 2025-11-03 16:17:12 +01:00
poll.py fixing tasks/projects 2025-11-13 09:24:38 +01:00
projects.py project finalizing, openrouter embeddings 2025-11-13 08:52:45 +01:00
restart.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
rfc.py local TTY implementation 2025-08-07 13:32:56 +02:00
scheduler_task_create.py Squashed commit of the following: 2025-11-13 09:00:10 +01:00
scheduler_task_delete.py Fix: proper task cancellation in scheduler, leakage in defer.py 2025-11-19 12:50:43 +01:00
scheduler_task_run.py (WIP) feat: Task Scheduler Management UI/UX and tools - Part 3 2025-05-01 15:38:51 +02:00
scheduler_task_update.py Squashed commit of the following: 2025-11-13 09:00:10 +01:00
scheduler_tasks_list.py Squashed commit of the following: 2025-11-13 09:00:10 +01:00
scheduler_tick.py security fixes 2025-06-24 14:19:49 +02:00
settings_get.py tunnel component refactor 2025-08-15 11:22:28 +02:00
settings_set.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
synthesize.py projects continued 2025-11-03 16:17:12 +01:00
transcribe.py projects continued 2025-11-03 16:17:12 +01:00
tunnel.py csrf ALLOWED_ORIGINS protection 2025-10-06 12:20:15 +02:00
tunnel_proxy.py csrf ALLOWED_ORIGINS protection 2025-10-06 12:20:15 +02:00
upload.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00
upload_work_dir_files.py api cleanup, microphone permission 2025-07-15 21:04:56 +02:00