Skip to content

Commit a70fba4

Browse files
committed
fix: force-close StaticPool DBAPI connection to release SQLite locks
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.
1 parent 089b09e commit a70fba4

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

investing_algorithm_framework/app/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,19 @@ def initialize_storage(self, remove_database_if_exists: bool = False):
378378
bind = Session.kw.get("bind")
379379

380380
if bind is not None:
381+
382+
try:
383+
conn = bind.connect()
384+
conn.invalidate()
385+
conn.close()
386+
except Exception:
387+
pass
388+
381389
bind.dispose()
382390

391+
import gc
392+
gc.collect()
393+
383394
os.remove(database_path)
384395

385396
# Create the sqlalchemy database uri

investing_algorithm_framework/infrastructure/database/sql_alchemy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ def teardown_sqlalchemy():
9393
bind = Session.kw.get("bind")
9494

9595
if bind is not None:
96+
97+
# StaticPool._close_connection() is a no-op, so
98+
# engine.dispose() alone won't close the underlying DBAPI
99+
# connection. Use invalidate() which bypasses the pool's
100+
# _close_connection and calls dialect.do_close() directly,
101+
# ensuring the sqlite3 file lock is released on Windows.
102+
try:
103+
conn = bind.connect()
104+
conn.invalidate()
105+
conn.close()
106+
except Exception:
107+
pass
108+
96109
bind.dispose()
97110

98111
Session.configure(bind=None)

0 commit comments

Comments
 (0)