-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: consistent UTC timestamp handling for PostgreSQL in DatabaseSessionService #4441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: consistent UTC timestamp handling for PostgreSQL in DatabaseSessionService #4441
Conversation
Summary of ChangesHello @anmolg1997, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an inconsistency in how timestamps are handled for PostgreSQL within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request addresses an inconsistency in timestamp handling for PostgreSQL, which could lead to incorrect stale session detection. The changes ensure that timestamps are consistently handled as UTC across different database dialects.
My review focuses on the correctness and clarity of the implementation. The core logic changes in append_event and get_update_timestamp are solid and effectively fix the described issue. I've added a couple of comments for minor code quality improvements: one regarding an unused constant and another about an unused function parameter that could be cleaned up in the future.
Overall, this is a good fix for a subtle but important bug.
| _MARIADB_DIALECT = "mariadb" | ||
| _MYSQL_DIALECT = "mysql" | ||
| _POSTGRESQL_DIALECT = "postgresql" | ||
| _DATABRICKS_DIALECT = "databricks" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # SQLite does not support timezone. SQLAlchemy returns a naive datetime | ||
| # object without timezone information. We need to convert it to UTC | ||
| # manually. | ||
| def get_update_timestamp(self, is_sqlite: bool = False) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_sqlite parameter is no longer used within this function's body. The new implementation correctly relies on checking self.update_time.tzinfo is None, which is more robust. To improve code quality, this parameter should be removed from the function signature. This would require updating all call sites. If modifying all call sites is outside the scope of this PR, consider adding a TODO comment to track this for future refactoring.
…ionService Fixes two timestamp inconsistencies that cause stale session detection failures when using PostgreSQL with asyncpg: 1. append_event() now strips timezone info for PostgreSQL (matching create_session() behavior). Previously, PostgreSQL got a local-time naive datetime via datetime.fromtimestamp(), while create_session() stored a UTC-based naive datetime. This mismatch caused get_update_timestamp() to return incorrect values when the server timezone differs from UTC. 2. get_update_timestamp() now uses tzinfo-awareness to decide whether to interpret a naive datetime as UTC, rather than checking only for SQLite. This correctly handles both SQLite and PostgreSQL naive datetimes without requiring callers to pass dialect flags. Fixes google#4366 Related: google#1848 Co-authored-by: Cursor <[email protected]>
c8ce8cf to
56a3317
Compare
Link to Issue or Description of Change
Problem:
DatabaseSessionService.append_event()creates timestamps inconsistently across dialects.create_session()uses UTC and strips timezone for SQLite/PostgreSQL, butappend_event()only strips for SQLite — PostgreSQL gets a local-time naive datetime viadatetime.fromtimestamp(event.timestamp).This causes
get_update_timestamp()to return wrong values when the server's local timezone differs from UTC, which breaks stale session detection.Solution:
Two targeted fixes:
append_event(): Now creates a UTC-aware datetime and strips timezone for both SQLite and PostgreSQL, matching the existingcreate_session()behavior.get_update_timestamp(): Instead of checkingis_sqlite, now checks whether the stored datetime has timezone info. Naive datetimes (from SQLite and PostgreSQL) are interpreted as UTC. Timezone-aware datetimes (MySQL, MariaDB) are used directly. This makes the method correct regardless of dialect.Testing Plan
Unit Tests:
Full session test suite passes (61 tests) including all DATABASE, SQLITE, and IN_MEMORY parametrized variants:
The critical tests that exercise this code path:
test_append_event_complete,test_session_last_update_time_updates_on_event,test_append_event_to_stale_session, andtest_append_event_concurrent_stale_sessions_preserve_all_state.