agent-zero/python/helpers
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.py frontend file browsers, css colors, litellm update, reqs split 2025-11-09 20:56:31 +01:00
attachment_manager.py console print edits for docker 2024-12-07 21:51:14 +01:00
backup.py Update backup.py 2025-11-13 20:11:56 +01:00
browser.py playwright headless fix 2025-06-11 13:58:08 +02:00
browser_use.py v0.8.1 release 2025-01-17 22:50:17 +01:00
browser_use_monkeypatch.py fix (browser-use): monkeypatch for browser-use and Gemini schemas 2025-08-29 23:41:28 +02:00
call_llm.py UI and settings merge 2024-11-04 22:55:56 +01:00
cloudflare_tunnel._py Better initialization 2025-06-07 22:28:47 +02:00
context.py projects continued 2025-11-03 16:17:12 +01:00
crypto.py RFC & SSH exchange for development 2024-12-03 14:22:18 +01:00
defer.py Fix: proper task cancellation in scheduler, leakage in defer.py 2025-11-19 12:50:43 +01:00
dirty_json.py fix: add missing error handling (#405) 2025-05-30 22:01:20 +02:00
docker.py console print edits for docker 2024-12-07 21:51:14 +01:00
document_query.py Merge branch 'pr/772' into development 2025-10-24 11:43:48 +02:00
dotenv.py console print edits for docker 2024-12-07 21:51:14 +01:00
duckduckgo_search.py duckduckgo search prototype 2024-07-14 23:48:10 +02:00
email_client.py added email_client helper. 2025-10-16 18:16:30 -04:00
errors.py Merge branch 'pr/663' into development 2025-08-14 14:33:30 +02:00
extension.py agents rework, memory improvements and params 2025-07-25 21:47:19 +02:00
extract_tools.py agents rework, memory improvements and params 2025-07-25 21:47:19 +02:00
faiss_monkey_patch.py Update faiss_monkey_patch.py 2025-06-11 12:49:10 +02:00
fasta2a_client.py a2a polishing 2025-07-31 21:49:50 +02:00
fasta2a_server.py Merge branch 'pr/663' into development 2025-08-14 14:33:30 +02:00
file_browser.py File browser fix directory listing (#613) 2025-07-22 17:09:35 +02:00
file_tree.py project finalizing, openrouter embeddings 2025-11-13 08:52:45 +01:00
files.py support kwargs in prompt var plugins 2025-11-18 10:39:05 +01:00
git.py update checker 2025-11-17 11:32:15 +01:00
guids.py short memory ids 2025-08-30 00:52:34 +02:00
history.py v0.9.5-pre cleanup, polishing, bugfixing 2025-08-25 09:59:44 +02:00
images.py vision prototype, terminal sessions 2025-04-01 13:55:50 +02:00
job_loop.py fix: synchronize job loop with development instance 2025-05-27 17:32:57 +02:00
knowledge_import.py frontend file browsers, css colors, litellm update, reqs split 2025-11-09 20:56:31 +01:00
kokoro_tts.py code exec interface + preload 2025-08-12 20:04:38 +02:00
localization.py fix: timezone switching spam bug (#664) 2025-08-15 11:35:15 +02:00
log.py log output lengths adjustment 2025-11-17 11:44:29 +01:00
login.py csrf ALLOWED_ORIGINS protection 2025-10-06 12:20:15 +02:00
mcp_handler.py Merge branch 'pr/693' into development 2025-08-25 13:12:01 +02:00
mcp_server.py mcp tokens and urls fix 2025-08-08 14:29:35 +02:00
memory.py frontend file browsers, css colors, litellm update, reqs split 2025-11-09 20:56:31 +01:00
memory_consolidation.py dev setup, guides, docs 2025-07-29 17:13:54 +02:00
messages.py RFC & SSH exchange for development 2024-12-03 14:22:18 +01:00
notification.py notifications polishing 2025-08-04 13:07:25 +02:00
perplexity_search.py New models, hints, bugfixes, prompt adjustments 2024-08-04 19:06:34 +02:00
persist_chat.py projects prototype, email parser 2025-10-28 09:04:17 +01:00
playwright.py frontend file browsers, css colors, litellm update, reqs split 2025-11-09 20:56:31 +01:00
print_catch.py Browser-use prototype 2025-01-03 23:01:21 +01:00
print_style.py projects continued 2025-11-03 16:17:12 +01:00
process.py console print edits for docker 2024-12-07 21:51:14 +01:00
projects.py project finalizing, openrouter embeddings 2025-11-13 08:52:45 +01:00
providers.py refactor: Standardize model provider configuration 2025-07-17 08:46:12 +08:00
rate_limiter.py rate limiter polishing, file download polishing 2025-07-31 14:46:23 +02:00
rfc.py RFC & SSH exchange for development 2024-12-03 14:22:18 +01:00
rfc_exchange.py RFC & SSH exchange for development 2024-12-03 14:22:18 +01:00
rfc_files.py RAG tool merge 2025-06-14 09:45:04 +02:00
runtime.py windows tty 2025-11-13 19:09:02 +01:00
searxng.py The Merge - initial changes 2025-05-27 18:48:22 +02:00
secrets.py projects continued 2025-11-03 16:17:12 +01:00
settings.py Merge pull request #788 from linuztx/subordinate-agents-settings-override 2025-11-17 13:46:29 +01:00
shell_local.py windows tty 2025-11-13 19:09:02 +01:00
shell_ssh.py ssh: disable systemd OSC metadata 2025-11-14 22:43:07 +08:00
strings.py project finalizing, openrouter embeddings 2025-11-13 08:52:45 +01:00
task_scheduler.py Fix: proper task cancellation in scheduler, leakage in defer.py 2025-11-19 12:50:43 +01:00
timed_input.py threading, readline fixes 2024-09-05 01:00:22 +02:00
tokens.py projects preps 2025-10-23 13:08:27 +02:00
tool.py CET capture partial tool output on intervention 2025-10-10 09:40:20 +08:00
tty_session.py windows tty 2025-11-13 19:09:02 +01:00
tunnel_manager.py cleanup 2025-06-12 16:39:43 +02:00
update_check.py update checker 2025-11-17 11:32:15 +01:00
vector_db.py projects continued 2025-11-03 16:17:12 +01:00
wait.py Add "wait" tool 2025-10-16 13:39:22 +08:00
whisper.py code exec interface + preload 2025-08-12 20:04:38 +02:00