From c16141db0b51587be646311178c24b9278cd6560 Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Thu, 26 Nov 2026 14:47:48 +0100 Subject: [PATCH 1/2] fix(events): close event streams once the server timed out An open /events stream, kept alive every 15 s, held the cloud instance alive until the request timeout, long after the server stopped answering its health check and its service was deleted. Co-Authored-By: Claude Opus 5.5 --- src/opengeodeweb_back/app.py | 14 ++++++++++---- tests/test_events.py | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/opengeodeweb_back/app.py b/src/opengeodeweb_back/app.py index 07065047..fcb70735 100644 --- a/src/opengeodeweb_back/app.py +++ b/src/opengeodeweb_back/app.py @@ -62,7 +62,7 @@ def publish(self, event: str, data: dict[str, Any]) -> None: for listener in self._queues: listener.put((event, data)) - def stream(self, keep_alive_seconds: float) -> flask.Response: + def stream(self, app: flask.Flask, keep_alive_seconds: float) -> flask.Response: listener: queue.Queue[_Event] = queue.Queue() with self._lock: self._queues.add(listener) @@ -72,17 +72,23 @@ def unregister() -> None: self._queues.discard(listener) response = flask.Response( - _stream_events(listener, keep_alive_seconds), mimetype="text/event-stream" + _stream_events(app, listener, keep_alive_seconds), mimetype="text/event-stream" ) response.call_on_close(unregister) return response -def _stream_events(listener: queue.Queue[_Event], keep_alive_seconds: float) -> Generator[str]: +def _stream_events( + app: flask.Flask, listener: queue.Queue[_Event], keep_alive_seconds: float +) -> Generator[str]: while True: try: event, data = listener.get(timeout=keep_alive_seconds) except queue.Empty: + # Once timed out, the server is about to be shut down: an open stream would keep + # The cloud instance alive (and billed) until the request timeout + if utils_functions.kill_task(app): + return # Comment line, ignored by EventSource: keeps proxies from closing the idle # Stream and lets a dropped connection be detected yield ": keep-alive\n\n" @@ -115,7 +121,7 @@ def after_request(response: flask.Response) -> flask.Response: @app.route("/events") def events() -> flask.Response: return listeners.stream( - flask.current_app.config.get("EVENTS_KEEP_ALIVE_SECONDS", EVENTS_KEEP_ALIVE_SECONDS) + app, app.config.get("EVENTS_KEEP_ALIVE_SECONDS", EVENTS_KEEP_ALIVE_SECONDS) ) diff --git a/tests/test_events.py b/tests/test_events.py index 76512d49..93e25e27 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -42,6 +42,32 @@ def test_every_listener_receives_each_event(client: FlaskClient) -> None: assert _chunk(second) == "event: health\ndata: {}\n\n" +def test_stream_closes_once_server_timed_out(client: FlaskClient) -> None: + # A stream left open would keep the cloud instance (and its billing) alive until the + # Request timeout, long after the server stopped answering its health check + app = client.application + timeout_keys = ("MINUTES_BEFORE_TIMEOUT", "LAST_PING_TIME") + saved = {key: app.config[key] for key in timeout_keys if key in app.config} + app.config["EVENTS_KEEP_ALIVE_SECONDS"] = 0.1 + app.config["MINUTES_BEFORE_TIMEOUT"] = "1" + app.config["LAST_PING_TIME"] = 0 + finished = threading.Event() + + def read_all() -> None: + for _ in client.get("/events").iter_encoded(): + pass + finished.set() + + try: + threading.Thread(target=read_all, daemon=True).start() + assert finished.wait(timeout=CHUNK_TIMEOUT_SECONDS) + finally: + app.config.pop("EVENTS_KEEP_ALIVE_SECONDS") + for key in timeout_keys: + app.config.pop(key, None) + app.config.update(saved) + + def test_idle_stream_sends_keep_alive(client: FlaskClient) -> None: app = client.application app.config["EVENTS_KEEP_ALIVE_SECONDS"] = 0.1 From edfeefb2b7ad7e8568b5cbbb5c9b9260368236f5 Mon Sep 17 00:00:00 2001 From: BotellaA <3213882+BotellaA@users.noreply.github.com> Date: Fri, 27 Nov 2026 08:28:56 +0000 Subject: [PATCH 2/2] Apply prepare changes --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index aefa233f..597e5178 100644 --- a/requirements.txt +++ b/requirements.txt @@ -84,4 +84,3 @@ werkzeug==3.1.8 # flask # flask-cors -opengeodeweb-microservice==1.*,>=1.5.0rc1