Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(api): tolerate client disconnects at the socket.io mount (#9400)

Lincoln Stein1524e7d4177d
diff --git a/invokeai/app/api/sockets.py b/invokeai/app/api/sockets.py
index 6dd6ab2382..8320eb7a5b 100644
--- a/invokeai/app/api/sockets.py
+++ b/invokeai/app/api/sockets.py
@@ -8,6 +8,7 @@ from fastapi import FastAPI
from pydantic import BaseModel
from socketio import ASGIApp, AsyncServer
from starlette.concurrency import run_in_threadpool
+from starlette.types import Message, Receive, Scope, Send
from invokeai.app.services.auth.token_service import verify_token
from invokeai.app.services.config.config_default import get_config
@@ -127,6 +128,54 @@ USER_EVENTS = {UserAccessChangedEvent}
LLM_TASK_EVENTS = {LLMTaskProgressEvent, LLMTaskCompleteEvent, LLMTaskErrorEvent}
+class DisconnectTolerantASGIApp:
+ """Wraps the socket.io ASGI app so a peer that vanishes before its first event is a no-op.
+
+ engine.io's ASGI `translate_request()` returns an empty environ when the first event it
+ receives is a disconnect rather than `http.request`/`websocket.connect`, but its caller
+ `AsyncServer.handle_request()` then indexes `environ["REQUEST_METHOD"]` unconditionally and
+ raises `KeyError`. A client that drops mid-poll - a flaky network, a reloaded tab, a closed
+ tab - therefore surfaces as a spurious traceback for a request nobody is waiting on any more.
+
+ Peeking at the first event lets us skip the socket.io app entirely in that case. On the
+ normal path the event is replayed, so the wrapped app cannot tell the difference.
+ """
+
+ def __init__(self, app: ASGIApp) -> None:
+ self._app = app
+
+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
+ if scope["type"] not in ("http", "websocket"):
+ await self._app(scope, receive, send)
+ return
+
+ first_event = await receive()
+ if first_event["type"] in ("http.disconnect", "websocket.disconnect"):
+ if scope["type"] == "http":
+ # Returning without responding is not enough: this mount sits below
+ # `BaseHTTPMiddleware` subclasses, and `call_next()` turns "the app I wrapped
+ # sent nothing" into `RuntimeError("No response returned.")` - swapping one
+ # traceback for another. Emit a throwaway response to satisfy it. Nobody
+ # receives it; uvicorn drops writes once the transport is closed.
+ await send({"type": "http.response.start", "status": 499, "headers": []})
+ await send({"type": "http.response.body", "body": b""})
+ # Websockets need no such placation - `BaseHTTPMiddleware` passes those straight
+ # through. (Under uvicorn a websocket scope always opens with `websocket.connect`,
+ # so that half of this guard is defensive.)
+ return
+
+ replayed = False
+
+ async def replaying_receive() -> Message:
+ nonlocal replayed
+ if not replayed:
+ replayed = True
+ return first_event
+ return await receive()
+
+ await self._app(scope, replaying_receive, send)
+
+
class SocketIO:
_sub_queue = "subscribe_queue"
_unsub_queue = "unsubscribe_queue"
@@ -144,7 +193,7 @@ class SocketIO:
# handshake 404s. The frontend already targets `{basePath}/ws/socket.io`.
base_url = get_config().base_url or ""
self._app = ASGIApp(socketio_server=self._sio, socketio_path=f"{base_url}/ws/socket.io")
- app.mount("/ws", self._app)
+ app.mount("/ws", DisconnectTolerantASGIApp(self._app))
# Track user information for each socket connection
self._socket_users: dict[str, dict[str, Any]] = {}
diff --git a/tests/app/test_socketio_disconnect.py b/tests/app/test_socketio_disconnect.py
new file mode 100644
index 0000000000..bd8cb98304
--- /dev/null
+++ b/tests/app/test_socketio_disconnect.py
@@ -0,0 +1,172 @@
+"""Tests for `DisconnectTolerantASGIApp`.
+
+Regression guard for the `KeyError: 'REQUEST_METHOD'` traceback that python-engineio raises when
+a client drops before its first ASGI event arrives: `translate_request()` returns an empty
+environ, then `AsyncServer.handle_request()` indexes `environ["REQUEST_METHOD"]` anyway.
+
+The load-bearing test here is `test_disconnect_through_the_real_middleware_stack_is_silent`:
+socket.io is mounted *below* `BaseHTTPMiddleware` subclasses in this app, and that changes what
+a correct fix looks like. Simply returning without a response makes `call_next()` raise
+`RuntimeError("No response returned.")`, which trades one traceback for another - so a test that
+exercises the wrapper in isolation passes while the bug is still live in production.
+"""
+
+from typing import Any
+
+import pytest
+from fastapi import FastAPI
+from socketio import ASGIApp, AsyncServer
+from starlette.middleware.base import BaseHTTPMiddleware
+from starlette.types import Message, Receive, Scope, Send
+
+from invokeai.app.api.sockets import DisconnectTolerantASGIApp
+
+SOCKETIO_PATH = "/ws/socket.io"
+
+
+def anyio_backend() -> str:
+ return "asyncio"
+
+
+def _socketio_app() -> ASGIApp:
+ return ASGIApp(socketio_server=AsyncServer(async_mode="asgi"), socketio_path=SOCKETIO_PATH)
+
+
+def _events(*messages: Message) -> Receive:
+ """A `receive` that yields the given messages, then repeats `http.disconnect` forever.
+
+ Real servers keep reporting the disconnect rather than raising, so an over-read shows up as
+ the behaviour under test instead of an unrelated `IndexError`.
+ """
+ pending = list(messages)
+
+ async def receive() -> Message:
+ return pending.pop(0) if pending else {"type": "http.disconnect"}
+
+ return receive
+
+
+def _collector() -> tuple[Send, list[Message]]:
+ sent: list[Message] = []
+
+ async def send(message: Message) -> None:
+ sent.append(message)
+
+ return send, sent
+
+
+def _http_scope(path: str = f"{SOCKETIO_PATH}/", query: bytes = b"EIO=4&transport=polling") -> Scope:
+ return {
+ "type": "http",
+ "asgi": {"version": "3.0", "spec_version": "2.3"},
+ "http_version": "1.1",
+ "method": "GET",
+ "scheme": "http",
+ "path": path,
+ "raw_path": path.encode(),
+ "root_path": "",
+ "query_string": query,
+ "headers": [(b"host", b"testserver")],
+ "client": ("127.0.0.1", 1234),
+ "server": ("127.0.0.1", 8000),
+ }
+
+
+class _PassThroughMiddleware(BaseHTTPMiddleware):
+ """Stands in for `SlidingWindowTokenMiddleware` / `RedirectRootWithQueryStringMiddleware`."""
+
+ async def dispatch(self, request: Any, call_next: Any) -> Any:
+ return await call_next(request)
+
+
+def _app_with_middleware(*, wrapped: bool) -> FastAPI:
+ """The production topology: socket.io mounted below a `BaseHTTPMiddleware`."""
+ app = FastAPI()
+ socketio = _socketio_app()
+ app.mount("/ws", DisconnectTolerantASGIApp(socketio) if wrapped else socketio)
+ app.add_middleware(_PassThroughMiddleware)
+ return app
+
+
+async def test_disconnect_through_the_real_middleware_stack_is_silent():
+ """The whole point of the change: nothing escapes when a client drops mid-poll."""
+ send, _sent = _collector()
+
+ # The unwrapped mount is what `main` does today - it raises.
+ with pytest.raises(KeyError, match="REQUEST_METHOD"):
+ await _app_with_middleware(wrapped=False)(_http_scope(), _events({"type": "http.disconnect"}), send)
+
+ send, sent = _collector()
+ await _app_with_middleware(wrapped=True)(_http_scope(), _events({"type": "http.disconnect"}), send)
+
+ # A response must actually be emitted. Returning nothing instead makes BaseHTTPMiddleware
+ # raise RuntimeError("No response returned."), i.e. a differently-named traceback.
+ assert [message["type"] for message in sent] == ["http.response.start", "http.response.body"]
+ assert sent[0]["status"] == 499
+
+
+async def test_disconnect_never_reaches_socketio():
+ """Called directly, the wrapper absorbs what the bare socket.io app cannot."""
+ send, _sent = _collector()
+ with pytest.raises(KeyError, match="REQUEST_METHOD"):
+ await _socketio_app()(_http_scope(), _events({"type": "http.disconnect"}), send)
+
+ send, sent = _collector()
+ await DisconnectTolerantASGIApp(_socketio_app())(_http_scope(), _events({"type": "http.disconnect"}), send)
+ assert [message["type"] for message in sent] == ["http.response.start", "http.response.body"]
+
+
+async def test_handshake_still_works_through_the_wrapper():
+ """The replay path, proven against real engine.io rather than a stand-in app."""
+ send, sent = _collector()
+ receive = _events({"type": "http.request", "body": b"", "more_body": False})
+
+ await DisconnectTolerantASGIApp(_socketio_app())(_http_scope(), receive, send)
+
+ assert sent[0]["type"] == "http.response.start"
+ assert sent[0]["status"] == 200
+ # engine.io's open packet is a `0` followed by a JSON blob carrying the session id.
+ body = b"".join(message.get("body", b"") for message in sent if message["type"] == "http.response.body")
+ assert body.startswith(b"0{")
+ assert b'"sid"' in body
+
+
+async def test_streamed_body_survives_the_replay():
+ """The peeked event is replayed, and later chunks still stream through untouched."""
+ seen: list[Message] = []
+
+ async def recording_app(scope: Scope, receive: Receive, send: Send) -> None:
+ seen.append(await receive())
+ seen.append(await receive())
+
+ receive = _events(
+ {"type": "http.request", "body": b"first", "more_body": True},
+ {"type": "http.request", "body": b"second", "more_body": False},
+ )
+ send, _sent = _collector()
+
+ await DisconnectTolerantASGIApp(recording_app)(_http_scope(), receive, send)
+
+ assert [event["body"] for event in seen] == [b"first", b"second"]
+
+
+async def test_non_http_scopes_pass_through_untouched():
+ """Lifespan traffic must keep its own first event; engine.io runs a receive loop on it."""
+ forwarded: dict[str, Any] = {}
+
+ async def recording_app(scope: Scope, receive: Receive, send: Send) -> None:
+ forwarded["receive"] = receive
+
+ receive = _events({"type": "lifespan.startup"})
+ send, _sent = _collector()
+
+ await DisconnectTolerantASGIApp(recording_app)({"type": "lifespan"}, receive, send)
+
+ # The original callable is handed over as-is, with nothing already read from it.
+ assert forwarded["receive"] is receive