Skip to content

fix: resolve SQLite database file locking on Windows for unittests#479

Merged
MDUYN merged 7 commits into
devfrom
291-fix-windows-unittest-database-locking
Apr 26, 2026
Merged

fix: resolve SQLite database file locking on Windows for unittests#479
MDUYN merged 7 commits into
devfrom
291-fix-windows-unittest-database-locking

Conversation

@MDUYN

@MDUYN MDUYN commented Apr 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes SQLite database file locking that prevents unit tests from running on Windows. On Windows, file locks are mandatory (unlike Unix/macOS), so SQLAlchemy's open connections prevent shutil.rmtree() from deleting database files during test teardown.

Changes

Core fix

  • Added teardown_sqlalchemy() function in sql_alchemy.py that properly closes all sessions, disposes the engine, and unbinds the Session — releasing the file lock
  • Fixed clear_db() to call engine.dispose() in a finally block to prevent connection leaks

Test cleanup updates

  • Updated TestBase and FlaskTestBase in test_base.py to use teardown_sqlalchemy() instead of close_all_sessions()
  • Added teardown_sqlalchemy() calls to all standalone test files with their own teardown logic
  • Added ignore_errors=True to all shutil.rmtree() calls as a safety net

CI

  • Added windows-latest to the test matrix in .github/workflows/test.yml
  • Changed source $VENV to poetry run for cross-platform compatibility

Closes #291

MDUYN added 3 commits April 26, 2026 16:20
- Add teardown_sqlalchemy() to properly dispose engine and release file locks
- Fix clear_db() to dispose its engine in a finally block
- Update all test teardowns to use teardown_sqlalchemy()
- Add ignore_errors=True to shutil.rmtree calls as safety net
- Add windows-latest to CI test matrix
- Use poetry run instead of source $VENV for cross-platform CI

Closes #291
Rename OHLCV CSV files that contained colons and spaces in their
filenames (e.g. '08:00:00') to use dashes instead (e.g. '08-00'),
matching the convention used by all other test data files.

Windows does not allow colons in filenames, causing git checkout
to fail on Windows CI runners.
- Call teardown_sqlalchemy() before os.remove() in initialize_storage()
  to release SQLite file locks on Windows (PermissionError WinError 32)
- Use remove_database_if_exists=True in test setUp to prevent stale DB
  state between tests
- Replace manual os.remove/os.rmdir loops with shutil.rmtree in web
  controller test teardowns
- Open verbose output file with encoding='utf-8' in
  validate_backtest_checkpoints to fix UnicodeEncodeError on Windows
  (cp1252 cannot encode checkmark/cross characters)
@MDUYN MDUYN force-pushed the 291-fix-windows-unittest-database-locking branch from 29d3ab1 to 089b09e Compare April 26, 2026 15:50
MDUYN added 4 commits April 26, 2026 18:10
StaticPool._close_connection() is a no-op, so engine.dispose() alone
never closes the underlying sqlite3 connection. On Windows, this leaves
mandatory file locks in place, causing PermissionError when tests try
to delete the database file.

Use Connection.invalidate() which bypasses the pool's _close_connection
and calls dialect.do_close() directly to actually close the DBAPI
connection. Also add gc.collect() in initialize_storage to ensure any
lingering references are cleaned up before file removal.
…ndows

RunTestBase.tearDown() was calling shutil.rmtree with ignore_errors=True
but without first releasing the SQLite file lock via teardown_sqlalchemy().
On Windows, this left the database file behind, causing the next test
to find a stale BITVAVO portfolio when expecting BINANCE.
@MDUYN MDUYN force-pushed the 291-fix-windows-unittest-database-locking branch from 235e62a to 526fb1a Compare April 26, 2026 19:50
@MDUYN MDUYN merged commit 7600754 into dev Apr 26, 2026
8 checks passed
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.

1 participant