{"slug": "silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none", "title": "Silent Failure #20: The Stop Button Cancelled a Handle That Was Already None", "summary": "A developer fixed two lifecycle bugs in magitech-ai/anima 2.0, a Python 3.12 file-watching service, where a debounce flush task was stored in a local variable that was None at registration time, so stop() cancelled a handle that had never been set and a stashed change later woke a dead service. The fix gives the flush task its own slot, cancels it through a shared helper from both stop() and the disposer, and drops changes arriving after the watch is removed; a reverse test now shows 'reconcile after stop: []' where it previously printed a file, with the suite at 1096 passing and 100% branch coverage.", "body_md": "The last line in my soak log was `reconcile after stop: [['a.py']]`. The service had been stopped two seconds earlier. The watcher was supposed to be gone, the debounce was supposed to be cancelled, and the reconciler was still walking the tree and printing the file it had picked up.\n\nThe test I wrote for that exact path was green. That is the part that still stings.\n\nI have been fixing lifecycle bugs in `magitech-ai/anima` 2.0, a Python 3.12 service that watches files and reloads fibers. PR #122 was supposed to close a narrow hole: a change that arrives during shutdown should not wake the reload loop back up. I had read `stop()` and seen the line that cancels `self._debounce_task`. I assumed the watch path used the same slot. I was wrong about which object owned the task, and the test I wrote agreed with me because it never touched the production call.\n\nHere is the shape of the hole. `_start_fs_watch()` creates the debounce flush task inside a closure and keeps it in a local variable named `task`. When it registers the side effect, it passes that local by value into `_stop_fs_watch(watches, task)`. At registration time `task` is still `None`, because the first change has not arrived yet. The disposer now holds `None` forever. The production path never sets `self._debounce_task`; only the reload path does. So `stop()` was cancelling a handle that had been `None` since the watch was installed, and the flush woke up five minutes later with a stashed change and called `reconcile()` into a dead service.\n\nMy first test called `_stop_fs_watch(watches, pending)` directly with a fake pending task. It passed for the wrong reason: I was testing the signature, not the call the service actually makes. I nearly shipped it. The soak log caught it because I had added one line that printed the input list of every reconcile, and the list had a file in it after stop.\n\nThe other bug was in the same function and I found it while writing the removal test. `on_change` cancelled the previous flush task unconditionally. If a second save landed while `reconcile()` was in the middle of a transaction and a fiber migration, the cancel aborted that work halfway. The debounced reload path already knew this: `_debounced_reload()` clears `self._debounce_task` before handing the work to `_ensure_reload_loop()`. The watch path copied the sleep but not the handoff order. My bad, and a silly one, because the correct pattern was three lines above the code I was editing.\n\nThe fix is not clever. The flush task gets its own slot, `self._fs_flush_task`, and both `stop()` and the side-effect disposer cancel it through `_cancel_fs_flush()`. A change that arrives after the watch was removed is dropped instead of stashed. A flush that wakes up after `_active` went false throws its stash away. `_flush_fs_watch()` clears the slot before it calls `reconcile()`, so a second save during the transaction starts a new task instead of cancelling the one in flight. `_stop_fs_watch()` now takes only `(watches)`, because a function that accepts a task it can never be given is a lie with a type signature.\n\nThe part I trust is the reverse test, not the fix. Same script, same fixture, run before and after the patch:\n\n`reconcile after stop: [['a.py']]`. After: `reconcile after stop: []`.` calls: ['b.py']`. After: `calls: ['a.py', 'b.py']`.\nThe stop line says stop actually stops. The reconcile line says the later save is not eating the in-flight transaction. Two runs, two different answers, and the only thing that changed was the code.\n\nThe full suite is 1096 passed, statement and branch coverage 100.00%, up from 99.98%. Five new cases: four for the new behavior and one for the branch where the flush wakes up after `_active` is false. That last branch is easy to miss, and `--cov-fail-under=100` is the only reason I wrote it. `ruff` and `mypy` are clean on the change.\n\nIf you have a debounce in a shutdown path, do not test the canceller with a task you made up. Register the watch, capture the disposer the service really holds, call it, and assert the live task is cancelled. Then test the window: save, call stop, sleep past the debounce, and assert `reconcile()` never ran. Then block `reconcile()` on an event, save a second time, release it, and assert both batches land. Three tests, each one fails on a different ordering bug, and none of them needs a mock of the scheduler.\n\nThere is also a cheap grep that would have saved me an afternoon: walk the production call sites of `_stop_fs_watch(` and check that no caller passes a literal `None`. If a function can only ever receive `None` from its real caller, its parameter is dead weight and the test around it is theater.\n\nMy take, and I will take the argument: a debounce slot has to be cleared before the work is handed off, not checked with `if not task.done()` at every call site. The handoff order is the API. The call-site guard is a footgun that works until someone adds a second caller.\n\nI have only measured this on Python 3.12 with asyncio and a 300 ms debounce, in one service, so treat the sample size accordingly.\n\nIf you run a debounce inside a stop path, how many saves inside your debounce window does it take before your stop test goes red, and does that test call the disposer, or only the function it believes the disposer calls?", "url": "https://wpnews.pro/news/silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none", "canonical_source": "https://dev.to/dafili/silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none-2a5i", "published_at": "2026-11-30 02:09:22+00:00", "updated_at": "2026-11-30 02:15:38.113264+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools"], "entities": ["magitech-ai/anima", "magitech-ai", "ruff", "mypy"], "also_reported_by": [], "takeaway": "Store a debounce flush task in a dedicated instance slot cancelled by both stop() and the disposer, and clear that slot before calling reconcile() so a second save starts a new task instead of cancelling in-flight work.", "recommendation": {"advice": "Test the canceller through the production registration path, not a fabricated task, and add a coverage-gated case for a flush waking after the service is inactive.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none", "markdown": "https://wpnews.pro/news/silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none.md", "text": "https://wpnews.pro/news/silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none.txt", "jsonld": "https://wpnews.pro/news/silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none.jsonld"}}