Skip to content

Commit 1ad56c1

Browse files
committed
Keep what a batch of live reads already answered
A batch of live reads gave up the moment one of its paths raised. Whatever the client had answered in that same message was thrown away with it, so the next read of those properties paid again for a round trip the browser had already carried out - and when one path timed out along the way, the timeout was raised even where the client had gone on to explain that another property simply does not exist. Answers which arrived are now cached even when a neighbour fails, and the failure which says something about the data is the one which reaches the handler rather than the one which only says the client was slow.
1 parent 3286b99 commit 1ad56c1

2 files changed

Lines changed: 124 additions & 3 deletions

File tree

‎src/reactpy/_event_proxy.py‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ class EventProxyAccessError(EventProxyError):
128128
"""The client refused or failed to resolve the requested path"""
129129

130130

131+
def _prefer_failure(
132+
current: BaseException | None, candidate: BaseException
133+
) -> BaseException:
134+
"""Pick what to raise when several paths of one batch went wrong
135+
136+
A timeout says nothing about the property which was asked for - only that the
137+
client was not there to answer. Any other failure explains something about the
138+
data, which is the more useful of the two to put in front of a handler.
139+
"""
140+
if current is None:
141+
return candidate
142+
if isinstance(current, EventProxyTimeout) and not isinstance(
143+
candidate, EventProxyTimeout
144+
):
145+
return candidate
146+
return current
147+
148+
131149
class EventProxyBackend:
132150
"""Sends live event access requests and routes the answers back to waiters
133151
@@ -283,6 +301,12 @@ async def resolve_many(
283301
284302
Anything already cached is answered on the spot. Everything else is enqueued
285303
together, so it leaves the process as a single batched request.
304+
305+
Whatever the client did answer is kept even when another path of the same
306+
batch went wrong - that round trip has already been paid for. When more than
307+
one path fails, the failure which says something about the data is the one
308+
which reaches the handler rather than the one which only says the client was
309+
slow to answer.
286310
"""
287311
values: list[Any] = [None] * len(paths)
288312
outstanding: list[
@@ -308,14 +332,34 @@ async def resolve_many(
308332
*(
309333
self._await(request_id, future, event_id, paths[index])
310334
for index, _, (request_id, future) in outstanding
311-
)
335+
),
336+
# One path going wrong must not throw away the answers the client
337+
# already gave to its neighbours in the same batch.
338+
return_exceptions=True,
312339
)
313340
finally:
314341
for key in ours:
315342
self._inflight.pop(key, None)
343+
344+
failure: BaseException | None = None
316345
for (index, key, _), payload in zip(outstanding, payloads, strict=True):
317-
values[index] = self._to_value(payload, event_id, paths[index])
318-
self._remember(key, values[index])
346+
if isinstance(payload, asyncio.CancelledError):
347+
raise payload
348+
if isinstance(payload, BaseException):
349+
failure = _prefer_failure(failure, payload)
350+
continue
351+
try:
352+
value = self._to_value(payload, event_id, paths[index])
353+
except BaseException as error:
354+
# Kept rather than raised on the spot, so that the rest of the batch
355+
# still gets as far as the cache before something is re-raised
356+
failure = _prefer_failure(failure, error)
357+
continue
358+
values[index] = value
359+
self._remember(key, value)
360+
361+
if failure is not None:
362+
raise failure
319363
return values
320364

321365
def notify(

‎tests/test_core/test_event_proxy.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,83 @@ async def test_failures_the_client_cannot_explain_are_still_reported():
959959
assert len(client.requests) == 1
960960

961961

962+
class ScriptedClient(FakeClient):
963+
"""Answers like a real client, except on the paths it has been told to misbehave on"""
964+
965+
def __init__(
966+
self,
967+
backend: EventProxyBackend,
968+
objects: dict[str, Any],
969+
hung: set[tuple[str, ...]] | None = None,
970+
silent: set[tuple[str, ...]] | None = None,
971+
) -> None:
972+
super().__init__(backend, objects)
973+
self.hung = set() if hung is None else hung
974+
self.silent = set() if silent is None else silent
975+
976+
def _reply_for(self, message: dict[str, Any]) -> dict[str, Any] | None:
977+
if tuple(message["path"]) in self.silent:
978+
# The browser never gets round to this one
979+
return None
980+
reply = super()._reply_for(message)
981+
if reply is not None and tuple(message["path"]) in self.hung:
982+
# The browser hung while working this one out, and said as much
983+
reply.update(ok=False, error="browser hung", code="timeout")
984+
return reply
985+
986+
987+
def make_scripted_client(
988+
objects: dict[str, Any],
989+
*,
990+
hung: set[tuple[str, ...]] | None = None,
991+
silent: set[tuple[str, ...]] | None = None,
992+
) -> tuple[EventProxyBackend, ScriptedClient]:
993+
"""Build a backend whose client hangs on, or stays quiet about, named paths"""
994+
backend = EventProxyBackend()
995+
client = ScriptedClient(backend, objects, hung, silent)
996+
backend.set_sender(client.send)
997+
return backend, client
998+
999+
1000+
async def test_a_batch_keeps_the_answers_it_already_paid_for(monkeypatch):
1001+
"""One path going wrong must not throw away the answers beside it
1002+
1003+
A batch used to give up as soon as one of its paths raised: the client had already
1004+
answered the rest, and all of it was discarded anyway. Reading those properties
1005+
again then paid for a round trip the browser had already carried out.
1006+
"""
1007+
monkeypatch.setattr(REACTPY_EVENT_PROXY_TIMEOUT, "current", 0.05)
1008+
backend, client = make_scripted_client(make_objects(), silent={("gone",)})
1009+
event_object = make_event(backend, "evt")
1010+
1011+
with pytest.raises(EventProxyTimeout):
1012+
await event_object.read("gone", "target.value")
1013+
1014+
assert len(client.messages) == 1, "both paths travelled as one message"
1015+
1016+
asked_for_before = len(client.requests)
1017+
assert await event_object.read("target.value") == ("typed",)
1018+
assert len(client.requests) == asked_for_before, "the answer is not asked for twice"
1019+
1020+
1021+
async def test_a_batch_reports_the_failure_which_explains_something():
1022+
"""A timeout must not bury the reason another property was refused
1023+
1024+
Both failures below come back in the same message, and the timeout is the one which
1025+
is listed first. Raising the first failure to exist reported a client which never
1026+
answered, when in fact it had answered - and had explained that the other property
1027+
simply does not exist.
1028+
"""
1029+
backend, _client = make_scripted_client(make_objects(), hung={("target", "value")})
1030+
event_object = make_event(backend, "evt")
1031+
1032+
with pytest.raises(EventProxyNotFound, match="does not exist") as info:
1033+
await event_object.read("target.value", "does_not_exist")
1034+
1035+
assert "does_not_exist" in str(info.value)
1036+
assert not backend.disabled, "one hung request is not yet a dead connection"
1037+
1038+
9621039
async def test_a_failed_send_switches_live_access_off():
9631040
backend = EventProxyBackend()
9641041
sent: list[dict[str, Any]] = []

0 commit comments

Comments
 (0)