From 3c50ab18ea41c640375e02dae0b21d1c87e2cb63 Mon Sep 17 00:00:00 2001 From: Tao Schiro Date: Fri, 20 Nov 2026 14:14:01 +0100 Subject: [PATCH 1/5] fix(requests): answer requests that do not use the database while a project is imported Fixes Geode-solutions/Vease#891 --- src/opengeodeweb_back/utils_functions.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/opengeodeweb_back/utils_functions.py b/src/opengeodeweb_back/utils_functions.py index 233b6cc8..a8688d53 100644 --- a/src/opengeodeweb_back/utils_functions.py +++ b/src/opengeodeweb_back/utils_functions.py @@ -2,6 +2,7 @@ # Standard library imports import base64 +import contextlib import hashlib import logging import shutil @@ -20,7 +21,10 @@ import flask import opengeode as og from defusedxml.ElementTree import parse -from opengeodeweb_microservice.database.connection import get_session +from opengeodeweb_microservice.database.connection import ( + DatabaseNotInitializedError, + get_session, +) from opengeodeweb_microservice.database.data import Data from opengeodeweb_microservice.schemas import ErrorResponse, SchemaDict @@ -63,6 +67,11 @@ def update_last_request_time(current_app: flask.Flask) -> None: def terminate_session(exception: BaseException | None) -> None: session = flask.g.pop("session", None) + if session is None: + # The database was closed when the request started (see before_request): it may have + # been reopened since, and the route may have used a session that must be committed + with contextlib.suppress(DatabaseNotInitializedError): + session = get_session() if session is None: return if exception is None: @@ -74,7 +83,10 @@ def terminate_session(exception: BaseException | None) -> None: def before_request(current_app: flask.Flask) -> None: increment_request_counter(current_app) - flask.g.session = get_session() + # The database is closed while import_project replaces the project files: requests that + # do not use it (ping) still answer, the others fail on their own get_session() + with contextlib.suppress(DatabaseNotInitializedError): + flask.g.session = get_session() flask.g.start_time = time.perf_counter() From 93dc1c97ca9e8541e876574533314e6f67cc651d Mon Sep 17 00:00:00 2001 From: Tao Schiro Date: Fri, 20 Nov 2026 14:14:01 +0100 Subject: [PATCH 2/5] test(requests): ping while the project database is closed, teardown commits a session opened after it reopened --- tests/test_routes.py | 11 +++++++++++ tests/test_utils_functions.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index f9b0b051..0056ec82 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -683,6 +683,17 @@ def test_import_project_route(client: FlaskClient, tmp_path: Path) -> None: client.application.config["DATA_FOLDER_PATH"] = original_data_folder +def test_ping_while_project_database_is_closed(client: FlaskClient) -> None: + # import_project closes the database while it replaces the project files, + # the client keeps pinging meanwhile + connection.close_database() + try: + response = client.post("/opengeodeweb_back/ping", json={}) + assert response.status_code == 200 + finally: + connection.init_database(Path(os.environ["TEST_DB_PATH"])) + + def test_save_viewable_workflow_from_object(client: FlaskClient) -> None: route = "/opengeodeweb_back/create/point_set" point_data = { diff --git a/tests/test_utils_functions.py b/tests/test_utils_functions.py index a4d781d2..ff5f14db 100644 --- a/tests/test_utils_functions.py +++ b/tests/test_utils_functions.py @@ -2,8 +2,10 @@ # Standard library imports import base64 +import os import re import shutil +import sqlite3 import uuid import zipfile from pathlib import Path @@ -12,6 +14,7 @@ # Third party imports import flask import pytest +from opengeodeweb_microservice.database import connection from opengeodeweb_microservice.database.connection import get_session from opengeodeweb_microservice.database.data import Data from werkzeug.exceptions import HTTPException @@ -77,6 +80,26 @@ def test_teardown_request() -> None: assert flask.current_app.config.get("LAST_REQUEST_TIME", 0) >= last_request_time +@pytest.mark.usefixtures("app_context") +def test_teardown_commits_a_session_opened_after_the_database_reopened() -> None: + # A request started while import_project had the database closed, then the route used it + db_path = Path(os.environ["TEST_DB_PATH"]) + connection.close_database() + utils_functions.before_request(flask.current_app) + connection.init_database(db_path) + data = Data.create( + geode_id="teardown_test", + geode_object_type=GeodePolygonalSurface3D.geode_object_type(), + viewer_object_type=GeodePolygonalSurface3D.viewer_object_type(), + viewer_elements_type=GeodePolygonalSurface3D.viewer_elements_type(), + ) + data_id = data.id + utils_functions.teardown_request(flask.current_app) + with sqlite3.connect(db_path) as reader: + row = reader.execute("SELECT id FROM datas WHERE id = ?", (data_id,)).fetchone() + assert row is not None + + def test_versions() -> None: list_packages = [ "OpenGeode-core", From 3516e158df768387a639c512a922f959cdca7ac8 Mon Sep 17 00:00:00 2001 From: SpliiT <106495600+SpliiT@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:14:46 +0000 Subject: [PATCH 3/5] Apply prepare changes --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 38266610..597e5178 100644 --- a/requirements.txt +++ b/requirements.txt @@ -84,4 +84,3 @@ werkzeug==3.1.8 # flask # flask-cors -opengeodeweb-microservice==1.*,>=1.3.3 From 1c31c814ff15d739c43e043ae4aa49439167672d Mon Sep 17 00:00:00 2001 From: SpliiT <106495600+SpliiT@users.noreply.github.com> Date: Mon, 23 Nov 2026 12:57:44 +0000 Subject: [PATCH 4/5] Apply prepare changes --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 5c24d6ce..597e5178 100644 --- a/requirements.txt +++ b/requirements.txt @@ -84,4 +84,3 @@ werkzeug==3.1.8 # flask # flask-cors -opengeodeweb-microservice==1.*,>=1.4.0 From e31c86c4a4c3b21c21e238fb4b626a7c41738c16 Mon Sep 17 00:00:00 2001 From: Tao Schiro Date: Mon, 23 Nov 2026 16:23:09 +0100 Subject: [PATCH 5/5] refactor(requests): resolve the request session only at teardown The session was taken both in before_request (flask.g.session) and at teardown as a fallback. Teardown now always ends the session of the currently open database: for import_project it no longer commits the stale session of the database it closed. Tests: close and reopen the database through a closed_database fixture, so a failing test cannot leave it closed, and remove the row created by the teardown test. Co-Authored-By: Claude Opus 5.5 --- src/opengeodeweb_back/utils_functions.py | 28 ++++++++++-------------- tests/conftest.py | 12 +++++++++- tests/test_routes.py | 14 +++++------- tests/test_utils_functions.py | 16 ++++++++------ 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/opengeodeweb_back/utils_functions.py b/src/opengeodeweb_back/utils_functions.py index 826d00ad..a63109df 100644 --- a/src/opengeodeweb_back/utils_functions.py +++ b/src/opengeodeweb_back/utils_functions.py @@ -72,27 +72,21 @@ def update_last_request_time(current_app: flask.Flask) -> None: def terminate_session(exception: BaseException | None) -> None: - session = flask.g.pop("session", None) - if session is None: - # The database was closed when the request started (see before_request): it may have - # been reopened since, and the route may have used a session that must be committed - with contextlib.suppress(DatabaseNotInitializedError): - session = get_session() - if session is None: - return - if exception is None: - session.commit() - else: - session.rollback() - session.close() + # The database is closed while import_project replaces the project files, and may be + # reopened during the request: the session to end is the one of the current database + with contextlib.suppress(DatabaseNotInitializedError): + session = get_session() + if exception is None: + session.commit() + else: + session.rollback() + session.close() def before_request(current_app: flask.Flask) -> None: + # No session here: requests that do not use the database (ping) must answer while + # import_project has it closed, the others fail on their own get_session() increment_request_counter(current_app) - # The database is closed while import_project replaces the project files: requests that - # do not use it (ping) still answer, the others fail on their own get_session() - with contextlib.suppress(DatabaseNotInitializedError): - flask.g.session = get_session() flask.g.start_time = time.perf_counter() diff --git a/tests/conftest.py b/tests/conftest.py index 72bcd389..292a66ae 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ from opengeodeweb_back.app import create_app, register_ogw_back_blueprints if TYPE_CHECKING: - from collections.abc import Generator + from collections.abc import Callable, Generator from flask.ctx import AppContext from flask.testing import FlaskClient @@ -70,6 +70,16 @@ def app_context() -> Generator[AppContext]: yield ctx +@pytest.fixture +def closed_database() -> Generator[Callable[[], None]]: + # import_project closes the database while it replaces the project files. + # Yields a function reopening it; it is reopened at teardown anyway, even if the test fails + db_path = Path(os.environ["TEST_DB_PATH"]) + connection.close_database() + yield lambda: init_database(db_path) + init_database(db_path) + + @pytest.fixture def test_id() -> str: return TEST_ID diff --git a/tests/test_routes.py b/tests/test_routes.py index 7e2a94d5..18eab162 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -12,6 +12,7 @@ # Third party imports import opengeode as og +import pytest from opengeodeweb_microservice.database import connection from opengeodeweb_microservice.database.connection import get_session @@ -37,7 +38,6 @@ from opengeodeweb_back.routes.blueprint_routes import extract_valid_attribute_values if TYPE_CHECKING: - import pytest from flask.testing import FlaskClient from werkzeug.test import TestResponse @@ -687,15 +687,11 @@ def test_import_project_route(client: FlaskClient, tmp_path: Path) -> None: client.application.config["DATA_FOLDER_PATH"] = original_data_folder +@pytest.mark.usefixtures("closed_database") def test_ping_while_project_database_is_closed(client: FlaskClient) -> None: - # import_project closes the database while it replaces the project files, - # the client keeps pinging meanwhile - connection.close_database() - try: - response = client.post("/opengeodeweb_back/ping", json={}) - assert response.status_code == 200 - finally: - connection.init_database(Path(os.environ["TEST_DB_PATH"])) + # The client keeps pinging while import_project has the database closed + response = client.post("/opengeodeweb_back/ping", json={}) + assert response.status_code == 200 def test_save_viewable_workflow_from_object(client: FlaskClient) -> None: diff --git a/tests/test_utils_functions.py b/tests/test_utils_functions.py index 17b26bfc..ea6e85c7 100644 --- a/tests/test_utils_functions.py +++ b/tests/test_utils_functions.py @@ -14,7 +14,6 @@ # Third party imports import flask import pytest -from opengeodeweb_microservice.database import connection from opengeodeweb_microservice.database.connection import get_session from opengeodeweb_microservice.database.data import Data from werkzeug.exceptions import HTTPException @@ -35,6 +34,8 @@ ) if TYPE_CHECKING: + from collections.abc import Callable + from flask.testing import FlaskClient from opengeodeweb_back.geode_objects.geode_object import GeodeObject @@ -81,12 +82,12 @@ def test_teardown_request() -> None: @pytest.mark.usefixtures("app_context") -def test_teardown_commits_a_session_opened_after_the_database_reopened() -> None: +def test_teardown_commits_a_session_opened_after_the_database_reopened( + closed_database: Callable[[], None], +) -> None: # A request started while import_project had the database closed, then the route used it - db_path = Path(os.environ["TEST_DB_PATH"]) - connection.close_database() utils_functions.before_request(flask.current_app) - connection.init_database(db_path) + closed_database() data = Data.create( geode_id="teardown_test", geode_object_type=GeodePolygonalSurface3D.geode_object_type(), @@ -95,8 +96,9 @@ def test_teardown_commits_a_session_opened_after_the_database_reopened() -> None ) data_id = data.id utils_functions.teardown_request(flask.current_app) - with sqlite3.connect(db_path) as reader: - row = reader.execute("SELECT id FROM datas WHERE id = ?", (data_id,)).fetchone() + with sqlite3.connect(os.environ["TEST_DB_PATH"]) as raw_connection: + row = raw_connection.execute("SELECT id FROM datas WHERE id = ?", (data_id,)).fetchone() + raw_connection.execute("DELETE FROM datas WHERE id = ?", (data_id,)) assert row is not None