Skip to content

Refactor/session for path - #963

Merged
AgreeDK merged 2 commits into
OpenSAK-Org:betafrom
nagisml:refactor/session-for-path
Oct 5, 2026
Merged

AgreeDK merged 2 commits into
OpenSAK-Org:betafrom
nagisml:refactor/session-for-path

Conversation

@nagisml

@nagisml nagisml commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Fix #957: don't swap the global DB engine from worker threads

Background jobs that work on a database other than the active one (GSAK import, GPX/zip import, move/copy caches, PQ e-mail import) used to call init_db() from the worker thread to switch the app-wide engine, and switch it back afterwards. While that ran, anything on the GUI thread that called get_session() (timers, queued signals) could read from or write to the wrong database. A multi-database GSAK import now does this several times in a row.

Changes

  • New session_for(db_path) in db/database.py: a private engine and session for one database, with the schema migrated exactly like init_db() does. The global engine is never touched.
  • The workers use session_for() for a non-active target, and get_session() as before when the target is the active database.
  • The importer's internal helper sessions bind to the engine of the session they're given, so they stay on the same database.
  • Distance helpers (recalculate_distances, distances_up_to_date) open a private session when db_path isn't the active database. Without this, the in-worker distance update from Fixing 955 #956 would have written a non-active target's distances into the active database.

Testing

  • Unit tests for session_for, plus worker tests that fail if the active session or init_db() is used for another database.
  • Full suite: 4463 passed, 105 skipped. mypy is clean.

nagisml and others added 2 commits October 2, 2026 20:29
…enSAK-Org#956)

After rebasing onto beta, GsakImportWorker still referenced the removed
original_path, and recalculate_distances()/distances_up_to_date() assumed
the global engine had been switched to db_path. With session_for() it no
longer is, so a non-active target's distances would have been computed
from and written to the active database.

- database: _session_on(db_path) picks get_session() for the active DB and
  session_for() otherwise; the distance helpers use it.
- gsak_import_dialog: update distances of target_db_path or the active DB.
- Narrow Optional paths for session_for() and fix the Engine annotation
  in importer (mypy clean).
- test: patch session_for instead of init_db.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AgreeDK
AgreeDK merged commit f56ebec into OpenSAK-Org:beta Oct 5, 2026
7 checks passed
nagisml added a commit to nagisml/OpenSAK that referenced this pull request Oct 5, 2026
…stance-followups

- distances_up_to_date(): use _session_on(db_path), keep index comment
- GsakImportWorker.run(): busy progress and _invalidate_distances() on
  target_path in the session_for()-based run()
- test: stub session_for instead of the removed init_db switch
@nagisml
nagisml deleted the refactor/session-for-path branch October 5, 2026 09:32
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