Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions shiny/reactive/_reactives.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"event",
)

import asyncio
import functools
import traceback
import warnings
Expand Down Expand Up @@ -575,6 +576,10 @@ async def _run(self) -> None:
try:
with ctx():
await self._fn()

# Yield so that messages can be sent to the client if necessary.
# https://github.com/posit-dev/py-shiny/issues/1381
await asyncio.sleep(0)
except SilentException:
# It's OK for SilentException to cause an Effect to stop running
pass
Expand Down
7 changes: 7 additions & 0 deletions shiny/session/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

__all__ = ("Session", "Inputs", "Outputs")

import asyncio
import contextlib
import dataclasses
import enum
Expand Down Expand Up @@ -653,6 +654,12 @@ def verify_state(expected_state: ConnectionState) -> None:
f"Unrecognized method {message_obj['method']}"
)

# Progress messages (of the "{binding: {id: xxx}}"" variety) may
# have queued up at this point; let them drain before we send
# the next message.
# https://github.com/posit-dev/py-shiny/issues/1381
await asyncio.sleep(0)

self._request_flush()

await flush()
Expand Down
10 changes: 10 additions & 0 deletions tests/pytest/mocktime.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ async def _sleep(self, delay: float) -> None:
self._i += 1
# Oldest first
self._sleepers.sort()

# This is necessary for some tests that call logic that internally awaits on
# asyncio.sleep(0). Without this, they hang.
#
# Another potential workaround would've been to check if delay <= 0 and just
# return. But this solution has the nice property of actually yielding control
# (I think...!), which is the whole point of asyncio.sleep(0).
if not self._is_advancing:
await self.advance_time(0)

await wake.wait()


Expand Down