fix(history): implement thread-safe connection pool for SQLite to fix resource leaks#35
Open
mhajder wants to merge 1 commit into
Open
fix(history): implement thread-safe connection pool for SQLite to fix resource leaks#35mhajder wants to merge 1 commit into
mhajder wants to merge 1 commit into
Conversation
… resource leaks Refactors `SyncHistory` to use a `queue.Queue` connection pool instead of `threading.local()`. Previously, connections tied to `threading.local()` in a background worker pool would remain open until the thread died, leading to a steady leak of open file descriptors and locked connections. Changes: - Replaced `threading.local()` with a bounded `queue.Queue` pool (size=5) of `sqlite3.Connection` objects configured with `check_same_thread=False`. - Safely wrapped the initial schema connection in a `try...finally` block to ensure `close()` is called, fixing a silent file lock leak. - Added `PRAGMA synchronous=NORMAL` alongside `journal_mode=WAL` for drastically improved concurrent write performance without sacrificing durability. - Updated all database operations to borrow from the pool using a context manager, allowing true multi-threaded concurrency (concurrent readers/writers via WAL) while strictly bounding the total number of open database connections. - Updated `close()` to gracefully drain the queue and explicitly terminate all pooled connections.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactors
SyncHistoryto use aqueue.Queueconnection pool instead ofthreading.local().Previously, connections tied to
threading.local()in a background worker pool would remain open until the thread died, leading to a steady leak of open file descriptors and locked connections.Changes:
threading.local()with a boundedqueue.Queuepool (size=5) ofsqlite3.Connectionobjects configured withcheck_same_thread=False.try...finallyblock to ensureclose()is called, fixing a silent file lock leak.PRAGMA synchronous=NORMALalongsidejournal_mode=WALfor drastically improved concurrent write performance without sacrificing durability.close()to gracefully drain the queue and explicitly terminate all pooled connections.