Skip to content

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

Open

Description

@t-kietzmann

Describe the bug

Session.duration is computed as DateTime.now().difference(_startTime) (serverpod/lib/src/server/session.dart:166, :190-191). DateTime.now() follows the system wall clock, so when NTP or an operator steps the clock while a session is open, the duration jumps: negative after a step back, too long by the step after a step forward. The same pattern measures every query in the query log, and decides its slow flag (serverpod_database/lib/src/adapters/postgres/database_connection.dart:681, :884), bulk data (serverpod_database/lib/src/bulk_data.dart:126, :136) and the health check (serverpod/lib/src/server/health_check.dart:56).

SessionLogManager already has a Stopwatch (serverpod/lib/src/server/log_manager/session_log_manager.dart:35, started at :77) and writes _stopwatch.elapsed as the session's duration (:261), but decides isSlow with session.duration (:229-236). The logged duration and the slow flag can therefore disagree.

To reproduce

  1. Open a session (e.g. an endpoint method that awaits 5 s, or a method stream).
  2. While it runs, step the system clock back by one minute (sudo date -s ..., or let NTP correct a drifted clock).
  3. session.duration inside the method is now negative; the session log has a positive duration from the stopwatch while the slow flag was computed from the negative wall-clock value. Stepping forward by one hour marks a 5 s session and any query running at that moment as slow.

Expected behavior

Durations come from a monotonic clock: a Stopwatch started with the session (the session log manager already has one) backing Session.duration, and a Stopwatch per query in _query/_logQuery, bulk data and the health check. startTime can stay a DateTime for display.

Library version

  • serverpod 4.1.0 (lib/src/server/session.dart, lib/src/server/log_manager/session_log_manager.dart, lib/src/server/health_check.dart), serverpod_database 4.1.0 (lib/src/adapters/postgres/database_connection.dart, lib/src/bulk_data.dart).
  • Unchanged on main (checked on 2026-11-13).

Platform information

  • Not platform specific.
  • Our setup: Dart SDK 3.13.7 (stable), macOS for development, Linux in production.

Additional context

We use Session.duration to measure the time since a call arrived (it starts before authentication). The effect on us is bounded, but a duration that can go negative is surprising in an API named duration. Small change.

How experienced are you with this library?

None

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    bug
    on Nov 12, 2026
  2. mackings commented on Nov 13, 2026

    @mackings
    Contributor

    Hi! I'd like to take this one. I reproduced it by faking a clock step with libfaketime (session.duration went to -58s after stepping back a minute) and have a fix ready that uses a Stopwatch in all the places you listed. PR coming shortly.

  3. moved this from Inbox 📫 to Contributor In Progress 👩‍💻 in Serverpod Frameworkson Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions