Skip to content

Commit 231713f

Browse files
committed
Resolve a finished event's future before starting the next queued one
The next queued handler for a token starts eagerly on Python 3.12+, and could supersede the run that just finished while its future was still unresolved and its task already forgotten, cancelling a completed run. Claude-Session: https://claude.ai/code/session_01LwJUCU8RtEco15mQvkxdQb
1 parent 20cc4b6 commit 231713f

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎packages/reflex-base/src/reflex_base/event/processor/event_processor.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,12 @@ def _finish_task(self, task: asyncio.Task):
978978
else:
979979
task_ctx = task.get_context().run(EventContext.get)
980980
self._tasks.pop(task_ctx.txid, None)
981+
future = self._futures.get(task_ctx.txid)
982+
# Before the next event starts: a handler started eagerly below can
983+
# supersede this one, which must not cancel a future still unresolved.
984+
handling_exception = task.done() and self._resolve_future(
985+
task, task_ctx, future
986+
)
981987
# Chain the next sequential event for this token if applicable.
982988
token_queue = self._token_queues.get(task_ctx.token)
983989
if token_queue and token_queue[0][0].ctx.txid == task_ctx.txid:
@@ -986,8 +992,7 @@ def _finish_task(self, task: asyncio.Task):
986992
self._dispatch_next_for_token(task_ctx.token)
987993
else:
988994
del self._token_queues[task_ctx.token]
989-
future = self._futures.get(task_ctx.txid)
990-
if task.done() and self._resolve_future(task, task_ctx, future):
995+
if handling_exception:
991996
return
992997
if future is not None:
993998
# The task is gone; clean up now in case the future resolved

‎tests/units/reflex_base/event/processor/test_event_processor.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,49 @@ async def supersede() -> EventFuture:
15681568
assert _CALL_LOG == []
15691569

15701570

1571+
async def test_queued_handler_superseding_a_finished_run_keeps_its_result(
1572+
processor: EventProcessor,
1573+
token: str,
1574+
):
1575+
"""The next queued handler cannot cancel the finished run it follows.
1576+
1577+
The handler queued behind a superseding run starts as soon as that run's
1578+
task finishes (eagerly on Python 3.12+), and supersedes it before
1579+
suspending. The finished run still resolves with its result.
1580+
1581+
Args:
1582+
processor: The event processor fixture.
1583+
token: The client token.
1584+
"""
1585+
release = asyncio.Event()
1586+
1587+
async def _search(value: str = "default") -> str:
1588+
if value == "first":
1589+
await release.wait()
1590+
return value.upper()
1591+
1592+
async def _search_again():
1593+
await EventContext.get().enqueue(
1594+
Event.from_event_type(search_event("second"))[0]
1595+
)
1596+
1597+
_search._reflex_supersedes = True # type: ignore[attr-defined]
1598+
search_event = EventHandler(fn=_search)
1599+
search_again_event = EventHandler(fn=_search_again)
1600+
RegistrationContext.register_event_handler(search_event)
1601+
RegistrationContext.register_event_handler(search_again_event)
1602+
1603+
processor.configure()
1604+
async with processor as ep:
1605+
first = await ep.enqueue(token, Event.from_event_type(search_event("first"))[0])
1606+
again = await ep.enqueue(token, Event.from_event_type(search_again_event())[0])
1607+
release.set()
1608+
assert await asyncio.wait_for(first, timeout=1) == "FIRST"
1609+
await asyncio.wait_for(again.wait_all(), timeout=1)
1610+
(second,) = again.children
1611+
assert second.result() == "SECOND"
1612+
1613+
15711614
async def test_superseding_event_logs_debug_on_cancel(
15721615
processor: EventProcessor,
15731616
token: str,

0 commit comments

Comments
 (0)