Skip to content

fix(session/database): truncate session creation timestamps to micros… - #1950

Open
ktsoator wants to merge 5 commits into
google:mainfrom
ktsoator:fix/session-create-precision
Open

ktsoator wants to merge 5 commits into
google:mainfrom
ktsoator:fix/session-create-precision

Conversation

@ktsoator

Copy link
Copy Markdown
Contributor

Linked issue

Fixes #1928.

Problem: MySQL can round creation timestamps up when storing them in datetime(6), causing the first AppendEvent to incorrectly fail with a stale-session error.

Solution: Truncate creation timestamps to microseconds before persistence, matching the existing AppendEvent behavior.

Python also uses microsecond-precision creation timestamps: database_session_service.py:698–708.

Behavior change

The first append succeeds when MySQL would previously have rounded the creation timestamp upward. Newly created database sessions now use microsecond precision, including on SQLite.

Testing Plan

TestDatabaseService_CreateMicrosecondPrecision covers timestamp rounding and first append, including the second boundary, and fails without the fix. Build, race tests, lint, and tidy checks pass across both modules.

Verified on MySQL 8.4.8 in Docker with Go 1.26.10 and no timeTruncate DSN parameter. The two previously failing cases now pass.

Checklist

  • I have read CONTRIBUTING.md.
  • I have performed a self-review, including an independent review.
  • I have commented the precision handling and simulated database rounding.
  • I have added tests that prove the fix is effective.
  • New and existing unit tests pass locally.
  • I have manually tested the change end-to-end.
  • Dependent changes: N/A.

…econds

Match database microsecond precision before returning a new session so MySQL rounding cannot make its first append appear stale. Cover timestamp rounding and the second boundary in regression tests.
@indurireddy-TF

indurireddy-TF commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Hi @ktsoator Thanks for the fix! I’ve verified the changes, and everything looks good. All tests are passing.

image image

@karolpiotrowicz karolpiotrowicz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must change before merge:

  • The new test creates the session without app: or user: state, so the app and user rows written by Create are never part of it.
  • It also stops at the first AppendEvent and never compares the returned LastUpdateTime() with the stored UpdateTime afterwards. That is the value the next stale check reads.

The change itself is correct:

I checked this against the merge base (6ede178) and against the PR merged into current main. The new test fails on the merge base in three of its four cases, and two of them fail with the stale-session error from the first AppendEvent. With the change applied, all four pass. Build, go vet, gofmt, staticcheck and the full test suite are clean on the merged tree, and the exported API of session/database is unchanged.

The truncated time reaches every value the stale check compares against: the session row's CreateTime and UpdateTime (storage_session.go#L51-L58), the app and user state rows, and the session Create returns (service.go#L140-L159). The earlier untruncated clock read at service.go#L116 is overwritten before the session is returned, so it never reaches the caller.

@ktsoator
ktsoator marked this pull request as draft October 27, 2026 13:31
Extend the precision test to catch incorrect app and user state timestamps
and mismatches between the returned session and storage after the first append.
@ktsoator

Copy link
Copy Markdown
Contributor Author

Added app: and user: state to the test and checked both rows’ timestamps. It now also compares the returned session’s LastUpdateTime() with the stored UpdateTime after the first append. I verified that the added checks fail when the corresponding timestamp handling is broken.

Could you take another look when you have time? 🙂

@ktsoator
ktsoator marked this pull request as ready for review October 27, 2026 16:16
@karolpiotrowicz
karolpiotrowicz dismissed their stale review November 1, 2026 14:49

Every item this review raised is fixed and verified on the current head.

@karolpiotrowicz karolpiotrowicz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closes both gaps from my earlier review. The test now creates the session with app: and user: state and checks the UpdateTime of both state rows (service_test.go#L89-L102), and after the first AppendEvent it compares LastUpdateTime() with the stored UpdateTime (service_test.go#L118-L126).

I checked that each new assertion fails when the code it guards is broken. Writing an untruncated time into the app or user row at service.go#L140 or service.go#L147 fails the matching check in the three cases that are not already aligned. Removing the sess.updatedAt updates at service.go#L405 and service.go#L509 fails the post-append comparison in all four cases, and removing the truncation in AppendEvent at service.go#L385 fails the UpdateTime after append check.

The test still fails on the merge base and passes on this head. I re-ran the other checks on this head merged into current main, with the same results as before.

@ktsoator

ktsoator commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking again and verifying the tests! 🙂

@ktsoator

Copy link
Copy Markdown
Contributor Author

hi @karolpiotrowicz
I noticed the same timestamp fix and a regression test for the first append have already landed in #2542. This PR also covers rounding across a second boundary, app/user state timestamps, and timestamp consistency after the first append.

Would you prefer that I update this PR to keep only the additional test coverage, or close it as superseded?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session/database: first AppendEvent after Create fails with "stale session error" on MySQL

3 participants