fix(sessions): Prevent MissingGreenlet after append_event with asyncpg#5814
Open
kkj333 wants to merge 1 commit into
Open
fix(sessions): Prevent MissingGreenlet after append_event with asyncpg#5814kkj333 wants to merge 1 commit into
kkj333 wants to merge 1 commit into
Conversation
Post-commit reads of storage_session.update_time lazy-load an expired column and trigger asyncpg pool_pre_ping outside SQLAlchemy's greenlet bridge. Capture revision fields before commit instead. Fixes google#5761
Author
|
Manual asyncpg verification added to the PR test plan. Tested locally against Postgres 16 with
A plain append loop did not naturally expire |
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
last_update_timeand_storage_update_markerinDatabaseSessionService.append_eventbeforecommit(), instead of readingstorage_session.update_timeafterwardFixes #5761
Problem
With
postgresql+asyncpg://and defaultpool_pre_ping=True, post-commit ORM access tostorage_session.update_timecan lazy-load an expired column (due toonupdate=func.now()), triggeringsqlalchemy.exc.MissingGreenletduring asyncpg pool pre-ping.Fix approach
Follows maintainer guidance on #5761: read
get_update_timestamp()/get_update_marker()before commit, then assign locals to the in-memory session.Test plan
uv run pytest tests/unittests/sessions/test_session_service.py::test_append_event_reads_storage_revision_before_commit -quv run pytest tests/unittests/sessions/test_session_service.py -k "append_event or last_update_time" -q(26 passed)Manual verification (asyncpg + Postgres)
postgresql+asyncpg://..., defaultpool_pre_ping=True, Postgres 16 (local docker compose)append_event× 100 → PASS (0MissingGreenlet)mainrepro: post-commit ORM read ofupdate_timeaftersession.expire(..., ["update_time"])→ reproducesMissingGreenlet(lazy load →pool_pre_ping→asyncpg.ping())append_eventloop alone did not naturally trigger attribute expiry onmain; the repro requires the post-commit lazy-load path described above