fix: a background task cancelled while entering a state from get_state no longer leaves the client's state locked

Merged
AD
1mo ago
+84-5

Closes #7737

The bug

A background task reaches another state with other = await self.get_state(OtherState) and later modifies it with async with other:. Entering that linked proxy runs the parent-proxy branch of StateProxy.__aenter__, in two steps:

  1. await self._self_parent_state_proxy.__aenter__(), which enters modify_state_with_links and takes the client's state lock;
  2. await parent_state.get_state(...), which loads the linked state (a redis read under the redis manager).

If step 2 raises, or the task is cancelled while it awaits, the exception leaves __aenter__. Python does not call __aexit__ when __aenter__ raises, so the parent's context is never exited and the lock from step 1 stays held. The task then finishes normally with CancelledError or the load error, and nothing records that the lock leaked.

What follows in that process:

  • Every later event for the client waits on the lock forever, so the session stops responding until restart.
  • StateManagerRedis.close() cancels the leases and gathers them with no timeout. The leaked lock's lease breaker shields a do_flush that needs the same lock, so the gather never returns, uvicorn's lifespan shutdown never finishes, and AppHarness.stop() blocks forever in backend_thread.join().

Cancellation is the usual trigger, because shutdown cancels in-flight handlers. Entering a task's own state proxy (the root branch, just below) already guards this case: it calls __aexit__ when entering fails.

How it was found

A downstream app's Playwright suite, which runs the app in-process under AppHarness, hung at session teardown in 3-9% of runs. The test had passed; the backend thread just never exited.

  • 12 hung runs (of 128): all-thread and all-task dumps showed the same picture every time. The main thread was in AppHarness.stop() → backend_thread.join(). The server loop was in lifespan shutdown → StateManagerRedis.close(), gathering a cancelled lease breaker whose do_flush waited at async with state_lock. No live task held that lock.
  • 4 more hung runs (of 128), with every StateManagerRedis lock instrumented to record its acquirer: in each, a task finished while still holding the lock, and every one had acquired it through this path:
    app:  async with self            (on a proxy obtained via get_state)
    proxy.py  StateProxy.__aenter__  -> await self._self_parent_state_proxy.__aenter__()
    proxy.py  StateProxy.__aenter__  -> await self._self_actx.__aenter__()
    redis.py  modify_state -> _try_modify_state -> _get_state_cached -> async with state_lock
    
    The lock was taken in the same millisecond the test passed and teardown began cancelling handlers.

The fix

When loading the linked state fails, call the parent proxy's __aexit__ before re-raising, the same way the root branch restores itself. The parent then releases the lock and its proxy lock, and leaves modify_state_with_links, so the client's state and its proxy can be entered again.

Validation

  • Regression test: test_linked_state_proxy_releases_lock_when_entering_fails, parametrized for the load raising and for cancellation during the load. On unpatched code both cases time out acquiring the client's state lock. With the fix both pass, and the root proxy can be re-entered.
  • Unit suite: uv run pytest tests/units gives 15,490 passed and 64 skipped. The only test left out was reflex_bench/drivers/test_browser.py, which needs a Chromium binary not installed locally.
  • ruff check, ruff format --check, pyright (0 errors) and pre-commit are clean on the changed files.

šŸ¤– Generated with Claude Code