zauberzeug / nicegui

Create web-based user interfaces with Python. The nice way.
https://nicegui.io
MIT License
9.63k stars 568 forks source link

KeyError raised in handle_javascript_response method of Client #3847

Open ruiheng opened 5 days ago

ruiheng commented 5 days ago

Description

This kind of exception as shown in the following logs, pops up from time to time in the logs of my app.

Task exception was never retrieved
future: <Task finished name='Task-1926' coro=<AsyncServer._handle_event_internal() done, defined at /home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/socketio/async_server.py:608> exception=KeyError('f4d16bd7-1174-443c-b90d-e46b099e27b5')>
Traceback (most recent call last):
  File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/socketio/async_server.py", line 610, in _handle_event_internal
    r = await server._trigger_event(data[0], namespace, sid, *data[1:])
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/socketio/async_server.py", line 641, in _trigger_event
    ret = handler(*args)
          ^^^^^^^^^^^^^^
  File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/nicegui.py", line 203, in _on_javascript_response
    client.handle_javascript_response(msg)
  File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/client.py", line 269, in handle_javascript_response
    JavaScriptRequest.resolve(msg['request_id'], msg.get('result'))
  File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/javascript_request.py", line 20, in resolve
    request = cls._instances[request_id]
              ~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'f4d16bd7-1174-443c-b90d-e46b099e27b5'

I don't know how to reproduce this issue. However theses errors usually occur on "slow" or complicate pages.

My guess is: after "await js_request" throws TimeoutError, the JavaScriptRequest instance gets deleted from JavaScriptRequest._instances dict. Subsequently, when the response of this JavaScriptRequest arrives, a KeyError is raised.

Alternatively there might be some requirements for using 'ui.run_javascript' that I am not aware of.

rodja commented 5 days ago

I don't know how to reproduce this issue. However theses errors usually occur on "slow" or complicate pages.

Are you using async/await properly? See https://github.com/zauberzeug/nicegui/wiki/FAQs#why-is-my-long-running-function-blocking-ui-updates

ruiheng commented 4 days ago

Thanks. I did not know the 'loop.set_debug' method.

After I add these code

def startup():
    loop = asyncio.get_running_loop()
    loop.set_debug(True)
    loop.slow_callback_duration = 0.05

app.on_startup(startup)

I have this new warning in logs:

Executing <Task pending name='....' coro=<run_coro_on_ui_loop.<locals>.wrapped() running at /.../utils.py:994> wait_for=<Future pending cb=[Protocol._on_waiter_completed(), Task.task_wakeup()] created at /home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/asyncpg/connection.py:350> created at /home/ruiheng/lyceum/lyceum/page/utils.py:1001> took 0.415 seconds

It seems that something related to asyncpg takes a little longer time to run. I don't know if 0.4 sec is a big issue. Anyway, the KeyError exception still occurs.

I forgot to mention that my browser that I use to test, actually have some performance issues. I can notice poor performance on all web pages.

falkoschindler commented 3 hours ago

@ruiheng Calling a function repeatedly that takes 0.4 sec can sum up and cause problems.

Can you, please, try to provide a minimum reproducible example? Or should we close this issue?