# Silent Failure #20: The Stop Button Cancelled a Handle That Was Already None

> Source: <https://dev.to/dafili/silent-failure-20-the-stop-button-cancelled-a-handle-that-was-already-none-2a5i>
> Published: 2026-11-30 02:09:22+00:00

**Recommendation:** 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.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

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.

The test I wrote for that exact path was green. That is the part that still stings.

I 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.

Here 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.

My 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.

The 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.

The 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.

The part I trust is the reverse test, not the fix. Same script, same fixture, run before and after the patch:

`reconcile after stop: [['a.py']]`. After: `reconcile after stop: []`.` calls: ['b.py']`. After: `calls: ['a.py', 'b.py']`.
The 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.

The 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.

If 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.

There 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.

My 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.

I have only measured this on Python 3.12 with asyncio and a 300 ms debounce, in one service, so treat the sample size accordingly.

If 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?
