Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(ui-server): keep UI connections alive past the tunnel's idle pool (#1815)

Call White19e1c5e265eb
diff --git a/tests/test_sentry_integration.py b/tests/test_sentry_integration.py
index 467b10982..1cfc67175 100644
--- a/tests/test_sentry_integration.py
+++ b/tests/test_sentry_integration.py
@@ -254,6 +254,52 @@ def test_run_ui_server_retries_when_prebind_reports_port_in_use(monkeypatch):
assert len(run_calls) == 1
+def test_run_ui_server_keepalive_outlives_upstream_proxy_pool(monkeypatch):
+ """The UI's idle keep-alive must outlive any proxy pooling its connections.
+
+ cloudflared reuses idle origin connections for up to its own keep-alive
+ timeout. If the server expires them first, a reused connection races the
+ close and the browser gets a 502 from a perfectly healthy service. Assert
+ the ordering property rather than a specific number, so retuning either
+ constant cannot silently reintroduce the race.
+ """
+
+ from vibe import ui_server
+ from core.services import settings as settings_service
+
+ captured: dict = {}
+
+ class DummySocket:
+ def close(self):
+ return None
+
+ class DummyServer:
+ def __init__(self, *_args, **_kwargs):
+ pass
+
+ def run(self, sockets=None):
+ return None
+
+ def capture_config(*args, **kwargs):
+ captured.update(kwargs)
+ return (args, kwargs)
+
+ fake_uvicorn = types.ModuleType("uvicorn")
+ fake_uvicorn.Config = capture_config
+ fake_uvicorn.Server = DummyServer
+ monkeypatch.setitem(sys.modules, "uvicorn", fake_uvicorn)
+
+ monkeypatch.setattr(ui_server.paths, "ensure_data_dirs", lambda: None)
+ # Keep the probe hermetic: no real config load, no remote-access reconcile.
+ monkeypatch.setattr(settings_service, "load_config", lambda *args, **kwargs: None)
+ monkeypatch.setattr(ui_server, "_bind_ui_socket", lambda _host, _port: DummySocket())
+ monkeypatch.setattr(ui_server, "_reconcile_remote_access_for_ui_start", lambda _config: None)
+
+ ui_server.run_ui_server("127.0.0.1", 5123)
+
+ assert captured["timeout_keep_alive"] > ui_server._CLOUDFLARED_PROXY_KEEPALIVE_TIMEOUT_SECONDS
+
+
def test_ui_error_handler_does_not_explicitly_capture_exceptions():
source = Path("vibe/ui_server.py").read_text(encoding="utf-8")
module = ast.parse(source)
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index e54bfa36b..8845d48ab 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -16127,6 +16127,19 @@ app.add_event_handler("shutdown", _stop_startup_dependency_reconcile)
app.add_event_handler("shutdown", stop_show_runtime_on_shutdown)
+# cloudflared holds idle origin connections in a pool for up to
+# --proxy-keepalive-timeout (default 1m30s) and reuses them for later requests.
+# uvicorn's own default is 5s, so the origin closes connections the tunnel still
+# considers reusable: a request handed to one while it is being torn down loses
+# the race, cloudflared reports "connection reset by peer", and the browser sees
+# a 502 even though the server is healthy. Outliving the upstream pool keeps
+# idle teardown on the proxy side, where it cannot collide with a live request.
+# The same reasoning covers any reverse proxy in front of the UI; nginx's
+# keepalive_timeout default (75s) is also below this value.
+_CLOUDFLARED_PROXY_KEEPALIVE_TIMEOUT_SECONDS = 90
+_UI_KEEPALIVE_TIMEOUT_SECONDS = 120
+
+
def _bind_ui_socket(host: str, port: int) -> socket.socket:
family = socket.AF_INET6 if host and ":" in host else socket.AF_INET
sock = socket.socket(family)
@@ -16183,6 +16196,7 @@ def run_ui_server(host: str, port: int) -> None:
loop="asyncio",
lifespan="on",
workers=1,
+ timeout_keep_alive=_UI_KEEPALIVE_TIMEOUT_SECONDS,
)
bound_socket = _bind_ui_socket(host, port)
_server = uvicorn.Server(uvicorn_config)