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 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