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

Async broken on v1.4.0 with FastAPI app #1874

Closed
zilch42 opened this issue Oct 25, 2023 · 5 comments · Fixed by #1877
Closed

Async broken on v1.4.0 with FastAPI app #1874

zilch42 opened this issue Oct 25, 2023 · 5 comments · Fixed by #1877
Labels
bug Something isn't working
Milestone

Comments

@zilch42
Copy link

zilch42 commented Oct 25, 2023

Description

The following async functions work on 1.3.18 but do nothing on 1.4.0 (no notification, no error, no indication something is happening) when using FastAPI. They work fine if using the normal ui.run().

from nicegui import ui, run
from fastapi import FastAPI
import asyncio
import requests 

async def async_task():
    ui.notify('Asynchronous task started')
    await asyncio.sleep(5)
    ui.notify('Asynchronous task finished')
    

async def handle_click():
    URL = 'https://httpbin.org/delay/1'
    response = await run.io_bound(requests.get, URL, timeout=3)
    ui.notify(f'Downloaded {len(response.content)} bytes')


@ui.page('/')
def home():
    ui.button('start async task', on_click=async_task)
    ui.button('Download', on_click=handle_click)

# works on 1.3.18, broken on 1.4.0
FastApp = FastAPI()
ui.run_with(FastApp)
if __name__ == '__main__':
    print('Please start with "uvicorn main:FastApp --reload --log-level debug --port 8000"')

# works on 1.3.18 and 1.4.0
# ui.run()

I guess it's related to #1849 but I didn't think there was a breaking change (I'm not using @on_event("startup") and @on_event("shutdown") explicitly).

@zilch42
Copy link
Author

zilch42 commented Oct 25, 2023

Additional info, adding some logging I get the following error message on reloading the tab after clicking the buttons.

2023-10-25 13:05:35,035 [ERROR] [async_server] disconnect async handler error
Traceback (most recent call last):
  File "C:\Users\XXX\AppData\Local\miniconda3\envs\nice14\lib\site-packages\engineio\async_server.py", line 475, in run_async_handler
    return await self.handlers[event](*args)
  File "C:\Users\XXX\AppData\Local\miniconda3\envs\nice14\lib\site-packages\socketio\async_server.py", line 687, in _handle_eio_disconnect
    await self._handle_disconnect(eio_sid, n)
  File "C:\Users\XXX\AppData\Local\miniconda3\envs\nice14\lib\site-packages\socketio\async_server.py", line 575, in _handle_disconnect
    await self._trigger_event('disconnect', namespace, sid)
  File "C:\Users\XXX\AppData\Local\miniconda3\envs\nice14\lib\site-packages\socketio\async_server.py", line 635, in _trigger_event
    ret = handler(*args)
  File "C:\Users\XXX\AppData\Local\miniconda3\envs\nice14\lib\site-packages\nicegui\nicegui.py", line 151, in _on_disconnect
    client.handle_disconnect()
  File "C:\Users\XXX\AppData\Local\miniconda3\envs\nice14\lib\site-packages\nicegui\client.py", line 222, in handle_disconnect
    self._disconnect_task = background_tasks.create(handle_disconnect())
  File "C:\Users\XXX\AppData\Local\miniconda3\envs\nice14\lib\site-packages\nicegui\background_tasks.py", line 21, in create
    assert core.loop is not None
AssertionError

@falkoschindler falkoschindler added this to the 1.4.1 milestone Oct 25, 2023
@falkoschindler falkoschindler added the bug Something isn't working label Oct 25, 2023
@falkoschindler
Copy link
Contributor

Thanks for the bug report, @zilch42! This a serious one. 🫤

But it seems to be a duplicate of #1870, and I think I found a fix and a workaround: #1870 (comment).

@falkoschindler falkoschindler closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2023
@falkoschindler falkoschindler modified the milestones: 1.4.2, 1.4.1 Oct 25, 2023
@zilch42
Copy link
Author

zilch42 commented Oct 25, 2023

Thanks @falkoschindler I've tried adding the workaround from #1870 (full code below) but it doesn't seem to solve this issue.

from nicegui import ui, run, app
from nicegui.nicegui import _shutdown, _startup
from fastapi import FastAPI
import asyncio
import requests 

async def async_task():
    ui.notify('Asynchronous task started')
    await asyncio.sleep(5)
    ui.notify('Asynchronous task finished')
    

async def handle_click():
    URL = 'https://httpbin.org/delay/1'
    response = await run.io_bound(requests.get, URL, timeout=3)
    ui.notify(f'Downloaded {len(response.content)} bytes')


@ui.page('/')
def home():
    ui.button('start async task', on_click=async_task)
    ui.button('Download', on_click=handle_click)

# works on 1.3.18, broken on 1.4.0
FastApp = FastAPI()
ui.run_with(FastApp)
app.on_event('startup')(_startup)
app.on_event('shutdown')(_shutdown)

if __name__ == '__main__':
    print('Please start with "uvicorn main:FastApp --reload --log-level info --port 8000"')

# works on 1.3.18 and 1.4.0
# ui.run()

Still don't get any ui.notify popups and the same AssertionError at assert core.loop is not None on page reload

@falkoschindler
Copy link
Contributor

@zilch42 In your case the app is called FastApp, so the workaround would be

FastApp.on_event('startup')(_startup)
FastApp.on_event('shutdown')(_shutdown)

@zilch42
Copy link
Author

zilch42 commented Oct 26, 2023

Oh that was silly of me. Thank you. I thought it was app from nicegui.app

@rodja rodja linked a pull request Oct 26, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants