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

Error when making asynchronous requests in Pyodide #4926

Open
1 task done
Klu1d opened this issue Feb 20, 2025 · 4 comments
Open
1 task done

Error when making asynchronous requests in Pyodide #4926

Klu1d opened this issue Feb 20, 2025 · 4 comments

Comments

@Klu1d
Copy link

Klu1d commented Feb 20, 2025

Duplicate Check

Describe the bug

изображение
I don’t fully understand this issue. In problem #4840, it was discovered that Pyodide in Flet needs to be updated to resolve certain issues. I’m currently waiting for that update, and in the meantime, I applied a temporary solution to keep my project running.

This temporary solution worked initially, but after a while, I started encountering this error (as shown in the picture). The error occurs when I try to make requests to the server using httpx. It seems like the issue could be related to the way Pyodide handles requests or asynchronous tasks, but I'm not sure what exactly is causing it.

The error in the picture is from a working project inside a try-except block. Below, I show the same error without the try-except block, along with minimal code to reproduce it:

Python engine initialized! python.js:23:13
Unhandled error processing page session : Traceback (most recent call last):
  File "/lib/python3.11/site-packages/flet/app.py", line 336, in on_session_created
    await target(page)
  File "/home/pyodide/main.py", line 14, in main
    data = await fetch_data()
           ^^^^^^^^^^^^^^^^^^
  File "/home/pyodide/main.py", line 7, in fetch_data
    response = await client.get("https://reqres.in/api/users/2")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpx/_client.py", line 1768, in get
    return await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpx/_client.py", line 1540, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpx/_client.py", line 1629, in send
    response = await self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpx/_client.py", line 1657, in _send_handling_auth
    response = await self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpx/_client.py", line 1694, in _send_handling_redirects
    response = await self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpx/_client.py", line 1730, in _send_single_request
    response = await transport.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpx/_transports/default.py", line 394, in handle_async_request
    resp = await self._pool.handle_async_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpcore/_async/connection_pool.py", line 256, in handle_async_request
    raise exc from None
  File "/lib/python3.11/site-packages/httpcore/_async/connection_pool.py", line 236, in handle_async_request
    response = await connection.handle_async_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpcore/_async/connection.py", line 101, in handle_async_request
    raise exc
  File "/lib/python3.11/site-packages/httpcore/_async/connection.py", line 78, in handle_async_request
    stream = await self._connect(request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpcore/_async/connection.py", line 124, in _connect
    stream = await self._network_backend.connect_tcp(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpcore/_backends/auto.py", line 31, in connect_tcp
    return await self._backend.connect_tcp(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/httpcore/_backends/anyio.py", line 115, in connect_tcp
    stream: anyio.abc.ByteStream = await anyio.connect_tcp(
                                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/anyio/_core/_sockets.py", line 208, in connect_tcp
    gai_res = await getaddrinfo(
              ^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/anyio/_core/_sockets.py", line 581, in getaddrinfo
    gai_res = await get_async_backend().getaddrinfo(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2683, in getaddrinfo
    return await get_running_loop().getaddrinfo(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/asyncio/events.py", line 295, in getaddrinfo
    raise NotImplementedError
NotImplementedError
<empty string>

Code sample

main.py
import httpx
import flet as ft


async def fetch_data():
    async with httpx.AsyncClient() as client:
        response = await client.get("https://reqres.in/api/users/2")
        return response.json()

async def main(page: ft.Page):
    data = await fetch_data()
    page.add(ft.Text(f'data: {data}', ))

ft.app(target=main)
requirements.txt
flet
ssl
httpx

To reproduce

  1. flet publish

  2. Change the Pyodide version in python-worker.js

  3. python.exe -m http.server 8080 --bind 127.0.0.1 --directory dist

if linux system: python3 -m http.server 8080 --bind 127.0.0.1 --directory dist

Operating System

Windows

Operating system details

Windows 11 22H2

Flet version

0.26.0x

Regression

I'm not sure / I don't know

@Klu1d
Copy link
Author

Klu1d commented Feb 20, 2025

20250220-1725-05.8885035.mp4

It seems I’ve found the issue and recorded it on video. As mentioned in #4840, the temporary solution is to simply change the version in the link. However, after launching, the first run works fine, but then it reverts to the version that is currently available in Flet.

I didn’t capture this on video, but I suspect that it also overwrites the dist folder if I enter my commands into it, similar to what happens with the first line.

  1. In the video, I start by launching the app and waiting for the files to load.
  2. Then, I press Ctrl + F5, and I notice that the file reverts to its previous state, before the changes.
  3. I have to unregister the worker to initiate it with my settings.
  4. After that, I press Ctrl + F5 again and the issue reoccurs, bringing me back to step 2.

I’m confident that this issue will be resolved after the next update (#4840), but I suspect that the fact that python-worker.js reverts to its original state upon creation is the root cause of the problem. If a developer tries to make changes to the files, they won’t take effect.

@FeodorFitsner
Copy link
Contributor

Are you running flet publish?

@Klu1d
Copy link
Author

Klu1d commented Feb 20, 2025

Are you running flet publish?

yes

@FeodorFitsner
Copy link
Contributor

Right, then contents of python-worker.js will be overwritten from a built-in template.
You can, actually, try publishing with Flet pre-release which uses Pyodide 0.27.2.
Flet 0.27 release is coming tomorrow.

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

2 participants