Use time.monotonic() instead of time.time() for elapsed time calculations#2714
Open
veeceey wants to merge 1 commit intodpkp:masterfrom
Open
Use time.monotonic() instead of time.time() for elapsed time calculations#2714veeceey wants to merge 1 commit intodpkp:masterfrom
veeceey wants to merge 1 commit intodpkp:masterfrom
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
time.time()withtime.monotonic()across 18 source files for measuring elapsed time, timeouts, and latencytime.monotonicinstead oftime.time, ensuring all unit tests continue to passtime.time()inkafka/record/legacy_records.pywhere wall clock time is needed for Kafka message timestampsMotivation
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:time.monotonic()provides a clock that is guaranteed to never go backward, making it the correct choice for all interval/elapsed time measurements.Fixes #1546
Test plan
test_crc32c[None]is unrelated)test_metrics.py,test_client_async.py,test_conn.py,test_coordinator.py,test_sender.py,test_fetcher.py, andtestutil.pyto patchtime.monotonicinstead oftime.timekafka/record/legacy_records.pystill usestime.time()for Kafka message timestamps (wall clock time is correct there)🤖 Generated with Claude Code