chore(api): Add true read-only sessions#38625
Open
cqjjjzr wants to merge 2 commits into
Open
Conversation
Contributor
Pyrefly Type Coverage
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a “true” read-only SQLAlchemy session path in the backend API to prevent accidentally treating a write-capable session as read-only, and updates controller/session behavior and tests to validate read-only enforcement.
Changes:
- Implemented
GuardedSession+create_readonly_session()with runtime write guards and DB-native read-only transaction/connection protections. - Switched
controllers.common.session.with_session(write=False)to use the guarded read-only session context. - Added unit tests covering representative ORM/Core/raw-SQL write attempts being blocked in read-only mode.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| api/core/db/session_factory.py | Adds guarded read-only session implementation and enforcement hooks. |
| api/controllers/common/session.py | Uses the new read-only session for write=False controller handlers. |
| api/tests/unit_tests/core/db/test_session_factory.py | Unit tests validating read-only session behavior at the session factory layer. |
| api/tests/unit_tests/controllers/common/test_session.py | Unit tests validating with_session(write=False) uses read-only behavior and blocks writes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+140
to
+152
| def _start_native_readonly_transaction(session: Session) -> Connection: | ||
| connection = session.connection() | ||
| connection.info[READONLY_CONNECTION_FLAG] = True | ||
| dialect_name = connection.dialect.name | ||
|
|
||
| if dialect_name == "sqlite": | ||
| connection.exec_driver_sql("PRAGMA query_only = ON") | ||
| elif dialect_name == "postgresql": | ||
| connection.exec_driver_sql("BEGIN READ ONLY") | ||
| elif dialect_name in {"mysql", "mariadb"}: | ||
| connection.exec_driver_sql("START TRANSACTION READ ONLY") | ||
|
|
||
| return connection |
|
|
||
|
|
||
| @contextmanager | ||
| def create_readonly_session() -> Generator[GuardedSession]: |
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.
Important
Fixes #<issue number>.Summary
No issue.
Add infrastrucrture to prevent mislabelling writing session as readonly like those in #38559 .
Typing using
ReadonlySessionis not applied yet. We will do this gradually.Checklist
make lint && make type-check(backend) andcd web && pnpm exec vp staged(frontend) to appease the lint gods