fix: make knowledge_graph.py SQLite connections exception-safe#44
Closed
adv3nt3 wants to merge 1 commit intoMemPalace:mainfrom
Closed
fix: make knowledge_graph.py SQLite connections exception-safe#44adv3nt3 wants to merge 1 commit intoMemPalace:mainfrom
adv3nt3 wants to merge 1 commit intoMemPalace:mainfrom
Conversation
5dbe6ce to
dc11fbc
Compare
Contributor
Author
|
The CI failure is not from this PR — both failing tests are in
This is a pre-existing test-vs-code mismatch on All 97 other tests pass, including the knowledge graph tests that exercise the code this PR changes. |
7 tasks
Wrap all 8 methods that open SQLite connections with contextlib.closing() to guarantee conn.close() is called even if execute()/commit()/fetchall() raises. Previously, connections would leak on exception since close() was called manually after operations. Add transaction context manager (with conn:) to write methods (add_entity, add_triple, invalidate) for auto-commit on success and auto-rollback on exception. Read methods use closing() only. _init_db keeps manual commit since executescript has its own transaction semantics.
dc11fbc to
40a979a
Compare
Contributor
Author
|
@bensig @milla-jovovich Closing this, the upstream changes to
The persistent connection + |
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.
Summary
KnowledgeGraphthat open SQLite connections withcontextlib.closing()to guaranteeconn.close()is called even ifexecute()/commit()/fetchall()raises. Previously, connections would leak on exception.with conn:) to write methods (add_entity,add_triple,invalidate) for auto-commit on success and auto-rollback on exception.query_entity,query_relationship,timeline,stats) useclosing()only._init_dbkeeps manualcommit()sinceexecutescripthas its own transaction semantics.add_tripleearly-return path — no more redundant manualconn.close().Test plan
ruff check mempalace/knowledge_graph.pypasses cleanruff format --check mempalace/knowledge_graph.pyalready formattedpython3 -m py_compile mempalace/knowledge_graph.pycompiles OK