Use CLOCK_MONOTONIC for background thread sleep to prevent clock rollback stalls#4
Merged
antonio2368 merged 3 commits intodevfrom Apr 2, 2026
Merged
Conversation
When CLOCK_MONOTONIC is available, use it for measuring sleep duration and initializing condvars instead of gettimeofday. This matches the condvar clock attribute set during init and avoids issues with wall clock adjustments (e.g. NTP corrections) affecting sleep calculations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
azat
reviewed
Apr 1, 2026
Member
|
Good that we have CI for jemalloc! |
Member
|
Let's also send patch to upstream (whatever upstream repo is) |
…failure Replace duplicated clock_gettime/gettimeofday blocks with nstime_init_update() which already handles the monotonic vs realtime fallback. Add error handling for pthread_condattr_setclock: in postfork_child, fall back to default CLOCK_REALTIME attributes; in boot1, propagate the failure. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
azat
reviewed
Apr 2, 2026
azat
approved these changes
Apr 2, 2026
In postfork_child, fall back to default condvar (CLOCK_REALTIME) if condattr_init fails. In boot1, return failure since initialization cannot proceed without a properly configured condvar. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 task
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.
background_thread_sleep()computes thepthread_cond_timedwaitdeadline fromgettimeofday()(CLOCK_REALTIME). If the wall clock jumps backward (NTP correction, VMsnapshot, leap second), the deadline ends up far in the future and the background thread stops purging dirty pages — potentially for hours or days.
This was observed in production where NTP corrections caused unbounded RSS growth due to the background thread stalling.
Fix
clock_gettime(CLOCK_MONOTONIC)CLOCK_MONOTONICon the condvar viapthread_condattr_setclockat both init sites (background_thread_boot1andbackground_thread_postfork_child)CLOCK_MONOTONICretain the originalgettimeofdayfallback