Skip to content

Use time.monotonic() instead of time.time() for elapsed time calculations#2714

Open
veeceey wants to merge 1 commit intodpkp:masterfrom
veeceey:fix/use-monotonic-clock-for-elapsed-time
Open

Use time.monotonic() instead of time.time() for elapsed time calculations#2714
veeceey wants to merge 1 commit intodpkp:masterfrom
veeceey:fix/use-monotonic-clock-for-elapsed-time

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 10, 2026

Summary

  • Replaces all 92 uses of time.time() with time.monotonic() across 18 source files for measuring elapsed time, timeouts, and latency
  • Updates 7 test files to mock time.monotonic instead of time.time, ensuring all unit tests continue to pass
  • Preserves time.time() in kafka/record/legacy_records.py where wall clock time is needed for Kafka message timestamps

Motivation

time.time() returns wall clock time which is adjustable -- it can jump forward or backward due to NTP synchronization, DST changes, or manual clock adjustments. Using it for elapsed time calculations can cause:

  • Premature timeouts: if the clock jumps forward, operations may time out before their actual deadline
  • Infinite hangs: if the clock jumps backward, timeout conditions may never be satisfied
  • Incorrect latency metrics: recorded latencies can be negative or wildly inflated

time.monotonic() provides a clock that is guaranteed to never go backward, making it the correct choice for all interval/elapsed time measurements.

>>> import time
>>> time.get_clock_info('time')
namespace(adjustable=True, implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, ...)
>>> time.get_clock_info('monotonic')
namespace(adjustable=False, implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, ...)

Fixes #1546

Test plan

  • All existing unit tests pass (1656 passed, 28 skipped -- the 1 pre-existing failure in test_crc32c[None] is unrelated)
  • Updated test mocks in test_metrics.py, test_client_async.py, test_conn.py, test_coordinator.py, test_sender.py, test_fetcher.py, and testutil.py to patch time.monotonic instead of time.time
  • Verified kafka/record/legacy_records.py still uses time.time() for Kafka message timestamps (wall clock time is correct there)

🤖 Generated with Claude Code

…ions

Replace all uses of time.time() with time.monotonic() for measuring
elapsed time, timeouts, and latency across the library. time.time()
returns wall clock time which is subject to NTP adjustments, DST changes,
and manual clock changes. This can cause incorrect timeout behavior --
including premature timeouts or infinite hangs -- when the system clock
jumps forward or backward.

time.monotonic() provides a clock that always moves forward at a steady
rate, making it the correct choice for measuring time intervals.

The only intentional exception is kafka/record/legacy_records.py, which
uses time.time() to generate Kafka message timestamps that require real
wall clock time.

Fixes dpkp#1546

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Using non monotonic functions to calculate elapsed time

1 participant