Skip to content

fix: Measure session and query durations with a monotonic clock - #6006

Open
mackings wants to merge 2 commits into
serverpod:mainfrom
mackings:fix/monotonic-durations
Open

mackings wants to merge 2 commits into
serverpod:mainfrom
mackings:fix/monotonic-durations

Conversation

@mackings

@mackings mackings commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Session.duration, the query log, bulk data and the health check measured durations with DateTime.now(), so a system clock change while a session or query was running made the duration jump (negative after a step back). They now use a Stopwatch. startTime and lastDatabaseOperationTime stay DateTime.

The session log now uses its own stopwatch for both the logged duration and the slow flag, so they always agree.

I tested it by running a session under libfaketime and stepping the clock: before, session.duration went to -0:00:58.98 after a one-minute step back and 1:00:01.5 after a one-hour step forward; with the fix it shows the real elapsed time (~1 s).

Fixes #6001

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

None.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T08:42:53.951642Z cf4a31b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@mackings

Copy link
Copy Markdown
Contributor Author

Hi @marcelomendoncasoares, this one is ready for a look when you have time. The failing CLI e2e check is generate_watch_test failing to delete its temp folder, which is unrelated to this change. Could you rerun it? Thanks!

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hey @mackings, thanks for picking this up!. I went through the change and the fix looks right to me. It matches what the SQLite adapter already does, and I couldn't find any spots you missed.

The one thing I wanted to ask about is the test:

await pod.shutdown(exitProcess: false);
});

test(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think both of these would pass on main as well, since nothing in them steps the clock and the old DateTime.now().difference(_startTime) also grows and doesn't go backwards in a normal run.

The session is also created with enableLogging: false (line 32), so the finalizeLog change isn't touched by them.

I don't see a good way to fake a clock step in a unit test here, so I'm wondering if we'd be better off dropping the file and just going with the manual check you described in the PR description. Or do you see a way to make it fail without the fix?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, I ran them without the fix and both pass. I looked at package:clock too, but its stopwatch reads the wall clock, so it would bring the bug back. I've dropped the file and updated the description.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cf4a31becb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}) async {
_stopwatch.stop();
final duration = session.duration;
final duration = _stopwatch.elapsed;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep the logged duration anchored to session creation

With database support and logging enabled, Session starts its monotonic timer and records startTime before server.createDatabase(this) invokes the synchronous databaseInterceptor, but the manager's stopwatch starts only after that callback returns. If an interceptor performs substantial setup, the close row reports a shorter duration than the session's elapsed time even though its SessionOpen.startTime includes that setup; measure this duration from the session's stopwatch or otherwise align both timer origins.

Useful? React with 👍 / 👎.

@mackings
mackings requested a review from FXschwartz November 26, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session.duration and query log durations use the wall clock instead of a monotonic clock

2 participants