Skip to content

Commit 20cc4b6

Browse files
committed
Fix awaiting an event handler from a background task
An exception raised by a handler awaited from a background task (no state lock held, so the event is enqueued) was delivered to the awaiting task and also reported to the backend exception handler, showing the error toast even when the task caught it. The enqueued event's future is now marked as awaited, and its exception is left to the caller: if the task does not catch it, the task's own failure reports it once. A supersedes=True handler run that had returned, but whose task done callback had not yet resolved its future, was cancelled by a newer invocation enqueued in that window, so the awaiting task got CancelledError for a run that completed. The in-memory state manager hits the window because a task waiting for the state lock resumes without suspending before it enqueues. Superseding such a run now only cancels the events it chained. Closes #7839 Claude-Session: https://claude.ai/code/session_01LwJUCU8RtEco15mQvkxdQb
1 parent 8e5bb84 commit 20cc4b6

7 files changed

Lines changed: 255 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Awaiting an event handler from a background task now behaves like the inline call: an exception the task catches is no longer also reported to the backend exception handler (no error toast), and a `supersedes=True` handler run that finished before a newer one superseded it returns its result instead of raising `CancelledError`.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ async def execute_event_inline(spec: EventSpec) -> Any:
616616
root_state = ctx.locked_root
617617
if root_state is None:
618618
(future,) = await ctx.enqueue(event)
619+
# An exception the event raises is delivered here, like the inline
620+
# call's, so it is this caller's to catch or to fail with.
621+
future.awaited = True
619622
return await future
620623
if handler.is_background:
621624
msg = (

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,15 @@ def _cancel_older_chains(self, key: tuple[str, str], root_gen: int) -> None:
704704
f"{key[1]}, superseded by a newer invocation."
705705
)
706706
for previous in slot.values():
707-
previous.cancel()
707+
task = self._tasks.get(previous.txid)
708+
if task is not None and task.done() and not previous.done():
709+
# The run finished, and its done callback has yet to resolve
710+
# the future with its outcome: only the events it chained
711+
# are left to supersede.
712+
for child in previous.children:
713+
child.cancel()
714+
else:
715+
previous.cancel()
708716
slot.clear()
709717

710718
def _on_future_done(self, future: EventFuture) -> None: # type: ignore[override]
@@ -1014,6 +1022,10 @@ def _resolve_future(
10141022
with contextlib.suppress(BaseException):
10151023
# Trigger the future to avoid warnings if the caller didn't wait.
10161024
future.result()
1025+
if future.awaited:
1026+
# The caller awaiting the event received the exception:
1027+
# reporting is its failure path's, if it does not catch it.
1028+
return False
10171029
telemetry.send_error(ex, context="backend")
10181030
if (
10191031
not task.get_name().startswith("reflex_backend_exception_handler|")

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class EventFuture(asyncio.Future):
4747
default=frozenset(), repr=False
4848
)
4949

50+
# Whether a caller awaits this future as a function call (awaiting an event
51+
# handler with no state lock held), so an exception the event raises is
52+
# the caller's to handle rather than the backend exception handler's.
53+
awaited: bool = dataclasses.field(default=False, repr=False)
54+
5055
def __post_init__(self) -> None:
5156
"""Call Future.__init__ for the EventFuture."""
5257
super(EventFuture, self).__init__(loop=self.loop)

‎tests/integration/tests_playwright/test_background_task.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ async def await_handlers(self):
125125
assert self.counter == 3
126126
await State.increment_by(await State.pick_amount(2))
127127

128+
@rx.event(background=True)
129+
async def await_failing_handler(self):
130+
try:
131+
await State.failing_increment()
132+
except ValueError:
133+
async with self:
134+
self.counter += 10
135+
136+
@rx.event
137+
def failing_increment(self):
138+
self.counter += 1
139+
msg = "caught by the awaiting background task"
140+
raise ValueError(msg)
141+
128142
@rx.event
129143
def increment_by(self, amount: int):
130144
self.counter += amount
@@ -236,6 +250,11 @@ def index() -> rx.Component:
236250
on_click=State.await_handlers,
237251
id="await-handlers",
238252
),
253+
rx.button(
254+
"Await Failing Handler",
255+
on_click=State.await_failing_handler,
256+
id="await-failing-handler",
257+
),
239258
rx.button("Reset", on_click=State.reset_counter, id="reset"),
240259
)
241260

@@ -457,6 +476,32 @@ def test_await_handlers_from_background_task(
457476
expect(counter).to_have_text("7", timeout=5_000)
458477

459478

479+
def test_await_failing_handler_from_background_task(
480+
background_task: AppHarness,
481+
background_page: Page,
482+
token: str,
483+
):
484+
"""An awaited handler's exception that the background task catches shows no toast.
485+
486+
Args:
487+
background_task: harness for BackgroundTask app.
488+
background_page: Playwright page.
489+
token: The token for the connected client.
490+
"""
491+
assert background_task.app_instance is not None
492+
493+
counter = background_page.locator("#counter")
494+
expect(counter).to_have_text("0")
495+
496+
background_page.locator("#await-failing-handler").click()
497+
# The failed handler's write is kept, then the task handles the exception.
498+
expect(counter).to_have_text("11", timeout=5_000)
499+
# A sequential event after it, so an error toast would have arrived by now.
500+
background_page.locator("#increment").click()
501+
expect(counter).to_have_text("12")
502+
expect(background_page.locator("[data-sonner-toast]")).to_have_count(0)
503+
504+
460505
@pytest.mark.parametrize(
461506
"button_id",
462507
[

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5582,3 +5582,131 @@ def ping(self):
55825582
BaseStateToken(ident=token, cls=State)
55835583
)
55845584
assert (await state.get_state(CancelInsideState)).log == ["ping"]
5585+
5586+
5587+
@pytest.mark.parametrize(
5588+
("catches", "expected_results", "expected_reported"),
5589+
[
5590+
(True, ["ValueError('boom')"], []),
5591+
(False, [], ["ValueError('boom')"]),
5592+
],
5593+
)
5594+
async def test_exception_of_handler_awaited_from_background_is_the_callers(
5595+
wired_app: App,
5596+
real_base_state_processor: BaseStateEventProcessor,
5597+
token: str,
5598+
catches: bool,
5599+
expected_results: list[str],
5600+
expected_reported: list[str],
5601+
):
5602+
"""An awaited handler's exception reaches the backend handler only if uncaught.
5603+
5604+
The background task awaits with no lock held, so the handler is enqueued.
5605+
Its exception is raised from the await like the inline call's: caught, it
5606+
is not reported; uncaught, the background task's failure reports it once.
5607+
5608+
Args:
5609+
wired_app: The App wired to the processor's state manager.
5610+
real_base_state_processor: The unmocked BaseStateEventProcessor.
5611+
token: The client token.
5612+
catches: Whether the background task catches the exception.
5613+
expected_results: The exceptions the background task caught.
5614+
expected_reported: The exceptions the backend exception handler got.
5615+
"""
5616+
results: list[str] = []
5617+
reported: list[str] = []
5618+
5619+
class BgBoomState(State):
5620+
total: int = 0
5621+
5622+
@event
5623+
def boom(self):
5624+
self.total += 1000
5625+
msg = "boom"
5626+
raise ValueError(msg)
5627+
5628+
@event(background=True)
5629+
async def run(self, catches: bool):
5630+
try:
5631+
await BgBoomState.boom()
5632+
except ValueError as ex:
5633+
if not catches:
5634+
raise
5635+
results.append(repr(ex))
5636+
5637+
real_base_state_processor.backend_exception_handler = lambda ex: reported.append(
5638+
repr(ex)
5639+
)
5640+
async with real_base_state_processor as processor:
5641+
future = await processor.enqueue(
5642+
token, Event.from_event_type(BgBoomState.run(catches))[0]
5643+
)
5644+
with contextlib.suppress(ValueError):
5645+
await future.wait_all()
5646+
5647+
assert results == expected_results
5648+
assert reported == expected_reported
5649+
async with _read_back(real_base_state_processor, token) as root:
5650+
assert (await root.get_state(BgBoomState)).total == 1000
5651+
5652+
5653+
@pytest.mark.parametrize(
5654+
"processor_state_manager", ["in_process", "redis"], indirect=True
5655+
)
5656+
async def test_superseding_handler_awaited_from_background_returns_finished_result(
5657+
wired_app: App,
5658+
real_base_state_processor: BaseStateEventProcessor,
5659+
token: str,
5660+
):
5661+
"""A background task gets the result of a superseding handler run that finished.
5662+
5663+
The second task waits for the state lock the first search holds, and
5664+
supersedes it as soon as that search has returned. With the in-memory
5665+
manager that is before the first search's future was resolved.
5666+
5667+
Args:
5668+
wired_app: The App wired to the processor's state manager.
5669+
real_base_state_processor: The unmocked BaseStateEventProcessor.
5670+
token: The client token.
5671+
"""
5672+
results: list[str] = []
5673+
started = asyncio.Event()
5674+
release = asyncio.Event()
5675+
5676+
class BgSearchState(State):
5677+
query: str = ""
5678+
5679+
@event(supersedes=True)
5680+
async def search(self, q: str) -> str:
5681+
if q == "first":
5682+
started.set()
5683+
await release.wait()
5684+
self.query = q
5685+
return q.upper()
5686+
5687+
@event(background=True)
5688+
async def run(self, q: str):
5689+
try:
5690+
result = await BgSearchState.search(q)
5691+
except asyncio.CancelledError:
5692+
result = "cancelled"
5693+
results.append(f"{q} -> {result}")
5694+
5695+
async with real_base_state_processor as processor:
5696+
first = await processor.enqueue(
5697+
token, Event.from_event_type(BgSearchState.run("first"))[0]
5698+
)
5699+
await asyncio.wait_for(started.wait(), timeout=5)
5700+
second = await processor.enqueue(
5701+
token, Event.from_event_type(BgSearchState.run("second"))[0]
5702+
)
5703+
# Let the second task start and queue for the lock the search holds.
5704+
await asyncio.sleep(0.1)
5705+
release.set()
5706+
await asyncio.wait_for(
5707+
asyncio.gather(first.wait_all(), second.wait_all()), timeout=5
5708+
)
5709+
5710+
assert results == ["first -> FIRST", "second -> SECOND"]
5711+
async with _read_back(real_base_state_processor, token) as root:
5712+
assert (await root.get_state(BgSearchState)).query == "second"

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,66 @@ async def test_superseding_event_cancels_previous_chain(
15081508
assert _CALL_LOG == [{"value": "stale_cancelled"}, {"value": "fresh"}]
15091509

15101510

1511+
async def test_superseding_event_keeps_the_result_of_a_finished_run(
1512+
processor: EventProcessor,
1513+
token: str,
1514+
):
1515+
"""A run that finished before it was superseded resolves with its result.
1516+
1517+
The newer invocation is enqueued after the older handler returned and
1518+
before its task's done callback resolved the future. Only the events the
1519+
finished run chained are cancelled.
1520+
1521+
Args:
1522+
processor: The event processor fixture.
1523+
token: The client token.
1524+
"""
1525+
release = asyncio.Event()
1526+
returning = asyncio.Event()
1527+
1528+
async def _search(value: str = "default", chain: bool = False) -> str:
1529+
if chain:
1530+
await EventContext.get().enqueue(
1531+
Event.from_event_type(logging_event(f"{value}_chained"))[0]
1532+
)
1533+
await release.wait()
1534+
# Wakes the superseding enqueue ahead of this task's done callbacks.
1535+
returning.set()
1536+
return value.upper()
1537+
1538+
_search._reflex_supersedes = True # type: ignore[attr-defined]
1539+
search_event = EventHandler(fn=_search)
1540+
RegistrationContext.register_event_handler(search_event)
1541+
1542+
processor.configure()
1543+
async with processor as ep:
1544+
first = await ep.enqueue(
1545+
token, Event.from_event_type(search_event("first", True))[0]
1546+
)
1547+
1548+
async def supersede() -> EventFuture:
1549+
await returning.wait()
1550+
assert ep._tasks[first.txid].done()
1551+
assert not first.done()
1552+
return await ep.enqueue(
1553+
token, Event.from_event_type(search_event("second"))[0]
1554+
)
1555+
1556+
superseding = asyncio.create_task(supersede())
1557+
# Park the superseding task on the gate before the handler sets it.
1558+
await asyncio.sleep(0)
1559+
release.set()
1560+
second = await asyncio.wait_for(superseding, timeout=1)
1561+
assert await asyncio.wait_for(first, timeout=1) == "FIRST"
1562+
assert await asyncio.wait_for(second.wait_all(), timeout=1) == "SECOND"
1563+
(chained,) = first.children
1564+
assert chained.cancelled()
1565+
await _drain_superseded(ep)
1566+
assert ep._superseded == {}
1567+
1568+
assert _CALL_LOG == []
1569+
1570+
15111571
async def test_superseding_event_logs_debug_on_cancel(
15121572
processor: EventProcessor,
15131573
token: str,

0 commit comments

Comments
 (0)