From f2d9a026db453899e5bb50f101b1f5f1a9f91ddf Mon Sep 17 00:00:00 2001 From: Christophe Besson Date: Sun, 13 Sep 2026 15:40:50 +0200 Subject: fix: hold every background task, in both codebases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit asyncio keeps only a weak reference to a task, so a coroutine started with `asyncio.ensure_future(...)` whose result is discarded can be collected while it is still running: the loop logs "Task was destroyed but it is pending!" and the work simply does not happen. No error reaches the caller, and what is lost is whatever that coroutine was in the middle of. The node already had a guard for this, written after an abandoned stream task lost a transcode slot for good — and it read one file, `webrtc_server.py`, because that is where the defect was found. Outside that file there were nineteen sites: the hub's `chat_notify` (a notification for every member of a group), the indexer's debounce (every real-time index update), eleven in `daemon.py` including the SIGHUP reload and each enrichment pass, two in `ops.py`, and five in the loopback API. `meshbay_common.background.spawn()` is the one door. It holds the task, drops it when it finishes, and logs what it raised under the coroutine's own name — an exception in a task nobody awaits was otherwise reported by asyncio at collection time, out of context or not at all. A peer session's `_spawn` stays as it is: that one can also *cancel* what it holds, which a module-level holder cannot, because a session ends and a process does not. `test_background_tasks.py` walks every package's source and refuses a discarded handle. It parses rather than greps, so an assignment, a comprehension or an await is not mistaken for one, and it was checked against a deliberate reintroduction. A guard that stops at the edge of the file where the bug was found is a guard against that bug, not against its class. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01UMxEQadpzPkYLFf5CYKhpW --- packages/meshbay-node/tests/test_task_lifetime.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'packages/meshbay-node/tests') diff --git a/packages/meshbay-node/tests/test_task_lifetime.py b/packages/meshbay-node/tests/test_task_lifetime.py index 9dffedb..133c030 100644 --- a/packages/meshbay-node/tests/test_task_lifetime.py +++ b/packages/meshbay-node/tests/test_task_lifetime.py @@ -14,6 +14,14 @@ first try included, until the daemon was restarted. It was two slots at the time, which is how few it took. Seen in the wild on 2026-08-16 after a viewer switched films mid-stream. + +**What stays here is the session's own contract**: a peer session holds what it +starts *and can cancel it* on the way out, which a module-level holder cannot — +a session ends, a process does not. The same rule for every other +fire-and-forget task, in every package, is +`meshbay-common/tests/test_background_tasks.py`. This file used to state it for +one file, and therefore did not state it for the nineteen sites outside that +file. """ import re -- cgit v1.2.3