Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError raised in handle_javascript_response method of Client #3847

Open
ruiheng opened this issue Oct 8, 2024 · 3 comments
Open

KeyError raised in handle_javascript_response method of Client #3847

ruiheng opened this issue Oct 8, 2024 · 3 comments

Comments

@ruiheng
Copy link

ruiheng commented Oct 8, 2024

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
Copy link
Member

rodja commented Oct 9, 2024

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
Copy link
Author

ruiheng commented Oct 9, 2024

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
Copy link
Contributor

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants