Repository navigation
fix(runtime): keep fire-and-forget tasks alive and report their failures - #318
Merged
Lucas1479 merged 2 commits intoNov 25, 2026
Merged
Conversation
asyncio keeps only a weak reference to a task. 31 call sites created a task and discarded it, including the long-running playback and sentence workers started in bootstrap and every EventBus subscriber callback. Such a task can be garbage-collected before it finishes, and an exception it raises surfaces only if and when the task object is collected. Add config.background_tasks.spawn_background, which schedules exactly as asyncio.create_task / loop.create_task do, holds a strong reference until the task completes, and logs a non-cancellation failure with the task name. Route the 31 discarded-task sites through it. The helper lives in config, the lowest layer, so tts and asr can use it without an upward import.
Track scheduled futures directly so emit_now can keep accepting native tasks, futures and custom awaitables. Exercise fan-out delivery, shared failures and garbage collection after a task suspends.
Lucas1479
approved these changes
Nov 25, 2026
Lucas1479
left a comment
Member
There was a problem hiding this comment.
Thanks for fixing the lifetime of fire-and-forget tasks, @AlgoWanderer. This keeps the playback workers alive and makes background failures visible when they happen.
I verified the follow-up in 1763eeb5. Event subscribers can return an existing task, a future, or a custom awaitable, and later subscribers still receive the event. The regression tests also show that a suspended task survives garbage collection and that a shared future reports its failure once. All 17 CI checks passed.
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.
What and why
asyncio keeps only a weak reference to a task. 31 production call sites call
asyncio.create_task/loop.create_task/asyncio.ensure_futureand discard the result (ruffRUF006), including:server/app.pybootstrap:playback_manager.run()andplay_sentence_worker(), the long-running TTS workers. If one dies, playback stops with no error.server/event_bus.py: every subscriber callback is fired with an unobservedensure_future.tts/playback.py(10 sites),tts/pipeline.py(3), and render, system, chat and work-activity handlers.Such a task can be garbage-collected before it finishes (asyncio docs), and an exception it raises shows up only if and when the task object is collected, as "Task exception was never retrieved".
The repository already handles this by hand in ~15 places (a module set plus
add_done_callback(set.discard), e.g.server/app.py_focus_confirmation_tasks,server/vn_tts_bridge.py_TASKS). This PR adds one helper with that pattern and routes the 31 discarded-task sites through it:It schedules exactly like
asyncio.create_task(orloop.create_taskwhenloopis given), holds a strong reference until the task completes, and logs a non-cancellation failure with the task's name. Cancellation is not logged. It returns the task, so call sites keep the same shape.The helper lives in
config/background_tasks.py, stdlib-only, becauseconfigis the lowest layer intools/maintainability_ratchet.py. That letstts/asruse it without a new upward import. No ratchet baseline orSHAREDentry is changed.Existing hand-rolled task sets are left as they are, since several also track per-task metadata or are drained on shutdown. Moving them over would be a separate cleanup.
Change class
Owning layer: Python runtime task scheduling (
config, shared byasr,tts,core,server).User-visible effect, or
none: none in normal operation. A background task that crashes now logs an error fromconfig.background_tasksnaming the task, instead of failing silently or being reported later at garbage collection.Compatibility or migration impact, or
none: none.Evidence
Commands and manual journeys run (Windows 11, CPython 3.12.10, uv 0.12.8,
uv sync --locked --extra dev,AMADEUS_E2E_NO_TTS=1 TTS_DEVICE=cpu WORK_WORKTREE_ISOLATION=0):python -m ruff check .: passesruff check --select RUF006on first-party runtime code: 31 → 0tools/architecture/generate_views.py --checkandgenerate_readme_overview.py --check: passpytest tests/test_background_tasks.py(new): 4 passed. Covers keep-alive, failure logging with the task name, no log on cancellation, explicitlooppytest tests/test_maintainability_ratchet.py: 12 passedtools/run_tests.py: the local run covered 470 of 507 files before the machine ran out of memory. The 11 remaining files that import a changed module were then run separately and all pass. The other 26 unrun files don't import any changed module; Windows CI covers them.Failures seen locally in 7 files (
test_auip_artifact_style,test_auip_cross_axis_assembly,test_auip_entry_preflight,test_auip_work_recovery,test_cooperative_auip_preparation,test_cooperative_planner_after_work,test_work_artifact_registry) reproduce with identical counts on unmodifiedmain(2da1968) on the same machine. They are environment-specific (no Playwright browser installed locally; CRLF in git diff output) and not caused by this change.Relevant Python tests pass
CPU/model-less baseline remains supported
Electron
npm run buildpasses when Electron code changed (no Electron changes)Dependency audit passes when dependencies changed (no dependency changes)
Before/after screenshots are attached for visible UI changes (no UI changes)
Documentation/examples are updated for changed settings or contracts (no settings or contracts changed)
Final check