Skip to content

Conversation

@jkowalski
Copy link
Contributor

@jkowalski jkowalski commented Jun 19, 2025

@jkowalski jkowalski requested a review from Copilot June 19, 2025 20:11

This comment was marked as outdated.

@jkowalski jkowalski requested a review from Copilot June 19, 2025 20:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR replaces standard timers with a “sleepable” timer that breaks waits into capped intervals to handle machine sleep, and updates the scheduler to use this new timer.

  • Introduce sleepable.Timer with MaxSleepTime to cap sleep intervals.
  • Add comprehensive tests in sleepable_timer_test.go to validate timer behavior (including edge cases and concurrency).
  • Update the scheduler (internal/scheduler/scheduler.go) to use sleepable.NewTimer instead of time.NewTimer.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/sleepable/sleepable_timer.go New sleepable timer implementation and configuration via MaxSleepTime.
internal/sleepable/sleepable_timer_test.go New tests covering timer firing, stopping, concurrency, and edge cases.
internal/scheduler/scheduler.go Scheduler updated to use sleepable.NewTimer and cap sleep between triggers.
Comments suppressed due to low confidence (1)

internal/sleepable/sleepable_timer_test.go:144

  • The loop for range 10 is invalid Go syntax and will not compile; use a standard indexed loop (e.g. for i := 0; i < 10; i++) or iterate over a slice.
		for range 10 {

}

timer = time.NewTimer(sleepTimeUntilNextTrigger)
timer = sleepable.NewTimer(s.TimeNow, targetTime)
Copy link

Copilot AI Jun 19, 2025

Choose a reason for hiding this comment

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

Existing timer isn’t stopped before being overwritten, leaking its goroutine. Add if timer != nil { timer.Stop() } before creating the new one.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the timer is being stopped

Comment on lines +74 to +69
start := clock.Now()
target := start.Add(tt.duration)

timer := NewTimer(clock.Now, target)

// Wait for timer to trigger
<-timer.C

elapsed := clock.Now().Sub(start)
Copy link

Copilot AI Jun 19, 2025

Choose a reason for hiding this comment

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

[nitpick] These tests use the real clock and actual time.Sleep, which can be flaky; consider using a fake or simulated clock to make tests faster and more reliable.

Suggested change
start := clock.Now()
target := start.Add(tt.duration)
timer := NewTimer(clock.Now, target)
// Wait for timer to trigger
<-timer.C
elapsed := clock.Now().Sub(start)
simulatedClock := clock.NewSimulatedClock()
start := simulatedClock.Now()
target := start.Add(tt.duration)
timer := NewTimer(simulatedClock.Now, target)
// Advance the simulated clock to trigger the timer
simulatedClock.Advance(tt.duration)
<-timer.C
elapsed := simulatedClock.Now().Sub(start)

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

simulated clock won't work here.

@codecov
Copy link

codecov bot commented Jun 19, 2025

Codecov Report

Attention: Patch coverage is 94.91525% with 3 lines in your changes missing coverage. Please review.

Project coverage is 76.39%. Comparing base (cb455c6) to head (61bdf6d).
Report is 581 commits behind head on master.

Files with missing lines Patch % Lines
internal/sleepable/sleepable_timer.go 93.87% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4682      +/-   ##
==========================================
+ Coverage   75.86%   76.39%   +0.52%     
==========================================
  Files         470      530      +60     
  Lines       37301    40229    +2928     
==========================================
+ Hits        28299    30732    +2433     
- Misses       7071     7471     +400     
- Partials     1931     2026      +95     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@julio-lopez julio-lopez left a comment

Choose a reason for hiding this comment

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

@jkowalski could you elaborate on what the root cause of the issue was?

Overall, LGTM.
Questions and minor comments inline.
Thanks.

Comment on lines +85 to +74
if elapsed > 10*time.Millisecond {
t.Errorf("zero duration timer took too long: %v", elapsed)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can require.WithinDuration be used here?

Comment on lines +21 to +23
c.stopOnce.Do(func() {
close(c.stopChan)
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice

@jkowalski
Copy link
Contributor Author

@jkowalski could you elaborate on what the root cause of the issue was?

See #3512 (comment)

@jkowalski jkowalski force-pushed the catch-up-scheduler branch from cb9addd to 61bdf6d Compare June 24, 2025 05:51
@julio-lopez julio-lopez enabled auto-merge (squash) June 24, 2025 18:41
@julio-lopez julio-lopez disabled auto-merge June 24, 2025 19:05
@julio-lopez julio-lopez merged commit 4256e87 into kopia:master Jun 24, 2025
27 checks passed
jkowalski added a commit to jkowalski/kopia that referenced this pull request Jul 22, 2025
This was a very stupid bug introduced in
kopia#4682
jkowalski added a commit to jkowalski/kopia that referenced this pull request Jul 22, 2025
This was a very stupid bug introduced in
kopia#4682

Fixes kopia#4729
jkowalski added a commit that referenced this pull request Jul 22, 2025
This was a very stupid bug introduced in
#4682

Fixes #4729
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.

Kopia doesn't automatically run overdue snapshots when waking from sleep

2 participants