Skip to content

ConnectionResetError from loading too many video elements in native browser #5443

@MaidScientistIzutsumiMarin

Description

First Check

  • I added a very descriptive title here.
  • This is not a Q&A. I am sure something is wrong with NiceGUI or its documentation.
  • I used the GitHub search to find a similar issue and came up empty.

Example Code

from collections.abc import Awaitable, Callable

import anyio
from nicegui import app, ui
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response


class IgnoreClientDisconnect(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next: Callable[[Request], Awaitable[Response]]) -> Response:
        try:
            return await call_next(request)
        except RuntimeError as exc:
            if str(exc) == "No response returned.":
                # client closed the connection; return a 499-like response
                return Response(status_code=499)
            raise
        except anyio.EndOfStream:
            return Response(status_code=499)


def main() -> None:
    for _ in range(20):
        ui.video("test.mp4")


app.add_middleware(IgnoreClientDisconnect)
ui.run(main, native=True, reload=False)

Description

Start the app.
The videos load properly, but there is visible lag, and we get the following error:

Exception in callback _ProactorBasePipeTransport._call_connection_lost()
handle: <Handle _ProactorBasePipeTransport._call_connection_lost()>
Traceback (most recent call last):
  File "C:\Users\Cookie\AppData\Local\Python\pythoncore-3.14-64\Lib\asyncio\events.py", line 94, in _run
    self._context.run(self._callback, *self._args)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Cookie\AppData\Local\Python\pythoncore-3.14-64\Lib\asyncio\proactor_events.py", line 165, in _call_connection_lost
    self._sock.shutdown(socket.SHUT_RDWR)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

Everything works properly to the user, but closing the native window doesn't ever cause the terminal to close. It also becomes impossible to Ctrl+C or Ctrl+Z out of the terminal. Even calling app.shutdown() does not always close the terminal. These problems get worse as the number of videos are increased.

NiceGUI Version

3.2.0

Python Version

Python 3.14.0

Browser

Other

Operating System

Windows

Additional Context

This is an issue we have specifically noticed when working with large numbers of video files. I can confirm this error does not occur even when adding an unrealistic number of buttons, which suggests it has something to do with how the videos are loaded and could be caused by the same thing as #4970. Which is to say, likely something to do with the loading/rendering of the video is not being handled asynchronously and results in blocking.

Compared to loading images, the UI appears to freeze for the duration of loading the videos instead of it happening asynchronously. Even loading an unrealistic number of (animated) images doesn't cause this error either. Doing so also doesn't cause the error from #4970. It is only videos from the tests we have done that have this problem.

This is probably the same issue as #3961, but the solution no longer works from tests we have done. Regardless, it is deprecated with Python 3.14 and is scheduled for removal.

This is also building off issue #4970 and using the this solution from the comments there. However, since this is a separate error, we figured we should make a new issue for it.

(Not sure if there is a better way to link specific comments).

Metadata

Metadata

Assignees

No one assigned

    Labels

    analysisStatus: Requires team/community input

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions