Skip to content

fix(runtime): keep fire-and-forget tasks alive and report their failures - #318

Merged
Lucas1479 merged 2 commits into
Code-Amadeus:mainfrom
AlgoWanderer:fix/own-background-tasks
Nov 25, 2026
Merged

Lucas1479 merged 2 commits into
Code-Amadeus:mainfrom
AlgoWanderer:fix/own-background-tasks

Conversation

@AlgoWanderer

Copy link
Copy Markdown
Contributor

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_future and discard the result (ruff RUF006), including:

  • server/app.py bootstrap: playback_manager.run() and play_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 unobserved ensure_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:

spawn_background(coro, *, name=None, loop=None) -> asyncio.Task

It schedules exactly like asyncio.create_task (or loop.create_task when loop is 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, because config is the lowest layer in tools/maintainability_ratchet.py. That lets tts/asr use it without a new upward import. No ratchet baseline or SHARED entry 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

  • Routine fix, documentation, test, maintenance, or presentation-only UI
  • Product-semantic or public-contract change discussed in the linked Issue
  • Isolated, default-off experiment

Owning layer: Python runtime task scheduling (config, shared by asr, tts, core, server).

User-visible effect, or none: none in normal operation. A background task that crashes now logs an error from config.background_tasks naming 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 .: passes

  • ruff check --select RUF006 on first-party runtime code: 31 → 0

  • tools/architecture/generate_views.py --check and generate_readme_overview.py --check: pass

  • pytest tests/test_background_tasks.py (new): 4 passed. Covers keep-alive, failure logging with the task name, no log on cancellation, explicit loop

  • pytest tests/test_maintainability_ratchet.py: 12 passed

  • tools/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 unmodified main (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 build passes 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

  • This PR addresses one coherent problem without unrelated cleanup
  • It does not add a speculative API, fallback, or compatibility path
  • No secrets, local state, model weights, voice material, or restricted assets are included
  • Third-party notices and provenance are preserved

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.
@Lucas1479 Lucas1479 added the good first issue Good for newcomers label Nov 25, 2026
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 Lucas1479 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Lucas1479
Lucas1479 merged commit 14ced4b into Code-Amadeus:main Nov 25, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants