Skip to content

fix(requests): answer requests that do not use the database while a project is imported - #297

Open
SpliiT wants to merge 8 commits into
nextfrom
fix/ping_during_project_import
Open

SpliiT wants to merge 8 commits into
nextfrom
fix/ping_during_project_import

Conversation

@SpliiT

@SpliiT SpliiT commented Nov 20, 2026

Copy link
Copy Markdown
Member

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_project closes the database while it extracts the .vease and regenerates the viewables, then reopens it. Every request opened a database session in before_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 own get_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:

  • ping while the database is closed answers 200 (500 before)
  • a session opened by the route after the database reopened is committed at teardown (fails without the terminate_session change)

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.

@JulienChampagnol JulienChampagnol left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JulienChampagnol

Copy link
Copy Markdown
Member

🤖 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants