Skip to content

Commit

Permalink
warn about late responses
Browse files Browse the repository at this point in the history
which where dropped silently before
  • Loading branch information
rodja committed Nov 28, 2023
1 parent 74fb536 commit a208217
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion nicegui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .client import Client
from .favicon import create_favicon_route
from .language import Language
from .logging import log

if TYPE_CHECKING:
from .api_router import APIRouter
Expand Down Expand Up @@ -87,6 +88,10 @@ def __call__(self, func: Callable[..., Any]) -> Callable[..., Any]:
core.app.remove_route(self.path) # NOTE make sure only the latest route definition is used
parameters_of_decorated_func = list(inspect.signature(func).parameters.keys())

def warn_about_late_returns(task: asyncio.Task) -> None:
if task.result():
log.error(f'ignoring {task.result()}; it was returned after the html was delivered to the client')

async def decorated(*dec_args, **dec_kwargs) -> Response:
request = dec_kwargs['request']
# NOTE cleaning up the keyword args so the signature is consistent with "func" again
Expand All @@ -105,7 +110,7 @@ async def wait_for_result() -> None:
if time.time() > deadline:
raise TimeoutError(f'Response not ready after {self.response_timeout} seconds')
await asyncio.sleep(0.1)
result = task.result() if task.done() else None
result = task.result() if task.done() else task.add_done_callback(warn_about_late_returns) # type: ignore
if isinstance(result, Response): # NOTE if setup returns a response, we don't need to render the page
return result
binding._refresh_step() # pylint: disable=protected-access
Expand Down
13 changes: 13 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import re
from uuid import uuid4

from fastapi.responses import PlainTextResponse
Expand Down Expand Up @@ -286,6 +287,18 @@ async def page(plain: bool = False):
screen.should_not_contain('normal NiceGUI page')


def test_warning_about_to_late_responses(screen: Screen):
@ui.page('/')
async def page(client: Client):
await client.connected()
ui.label('NiceGUI page')
return PlainTextResponse('custom response')

screen.open('/')
screen.should_contain('NiceGUI page')
screen.assert_py_logger('ERROR', re.compile('it was returned after the html was delivered to the client'))


def test_reconnecting_without_page_reload(screen: Screen):
@ui.page('/', reconnect_timeout=3.0)
def page():
Expand Down

0 comments on commit a208217

Please sign in to comment.