Repository navigation
Conversation
…roject is imported Fixes Geode-solutions/Vease#891
…ommits a session opened after it reopened
…ct_import # Conflicts: # requirements.txt
JulienChampagnol
left a comment
There was a problem hiding this comment.
Cross-repo review: fix/ping_during_project_import
This is the only PR on this branch, so there are no cross-repo contracts to check. The fix is correct: get_session() raises only when the registry is None, so suppressing DatabaseNotInitializedError in before_request lets ping answer, and any route that needs the database still fails on its own get_session() call.
1. Design: the session is resolved two ways (src/opengeodeweb_back/utils_functions.py, terminate_session / before_request)
get_session() returns a per-thread scoped_session, so within one request flask.g.session and a later get_session() call return the same object, unless the registry was replaced in between. That leaves two routes to one session: g.session, and a get_session() fallback for when it is missing.
Suggestion: drop flask.g.session and the eager get_session() in before_request. Have terminate_session always resolve the session with get_session() inside contextlib.suppress(DatabaseNotInitializedError). That gives one code path and one comment.
It also fixes a case the current code gets slightly wrong. For the import_project request itself, g.session holds the stale session from the old registry; this is harmless today only because the route wraps its own sessions in with get_session(). With the simpler version, teardown always commits the current registry's session. It costs nothing: a Session doesn't open a connection until it is used.
2. Tests: the new utils test doesn't restore the database if it fails (tests/test_utils_functions.py)
test_teardown_commits_a_session_opened_after_the_database_reopened calls close_database() and then before_request(...) before init_database(...). If anything in between raises, the shared test database stays closed and every later test fails for an unrelated reason. The route test in this PR already uses try/finally. Use it here too, or better, add a closed_database fixture in tests/conftest.py. Both new tests repeat the same close-then-reopen sequence, so the fixture would also remove the duplication.
3. Tests, nit: the test leaves a row behind (tests/test_utils_functions.py)
The Data row with geode_id="teardown_test" stays in the shared test database. Deleting it at the end of the test would keep the test isolated.
4. Out of scope, worth a follow-up issue: other threads still hold sessions when the database closes
close_database() calls scoped_session_registry.remove(), which only removes the calling thread's session. A request on another thread that opened its session before the import still holds it, and its connection, on the disposed engine. On Windows that can surface as the 423 "files locked" path. This predates the PR; it could be tracked next to the Viewer case the PR description already mentions.
Performance: no concerns.
Verdict: safe to merge on its own into next. I'd fix #2 before merging; #1 is a recommended simplification, and #3 and #4 are optional.
|
🤖 claude-cross-repo-review: complete |
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 <noreply@anthropic.com>
Fixes Geode-solutions/Vease#891
Opening a project while another one is loaded showed a red "Database not initialized. Call init_database() first." snackbar, while the project loaded fine.
import_projectcloses the database while it extracts the.veaseand regenerates the viewables, then reopens it. Every request opened a database session inbefore_request, even the ones that never use it: the front pings every 10 s, and a ping landing in that window answered 500 with that message. On a large project (several seconds of import on Windows) the ping almost always lands in it.before_request: a closed database no longer fails the request. Requests that do not use it (ping) answer; requests that use it still fail on their ownget_session().terminate_session: a request started while the database was closed may use it once it is reopened, its session is now committed (or rolled back) and closed at teardown instead of being lost.Tests:
terminate_sessionchange)Not covered here: the viewer also releases its database during the whole project reset/import; a viewer request reading it in that window would give the same message. Not observed while reproducing.