Skip to content

Conversation

@MellowYarker
Copy link
Contributor

Historically, if a SQLite DOs local alarm time in SQLite didn't match the AlarmManager's alarm time, we would reschedule the alarm and try to run the alarm again later.

However, if the time we are attempting to sync to is already before the current real system time, there is no point in rescheduling to run later, since we might as well run now.

This commit runs the alarm immediately if the current system time is already past the time we are trying to sync the alarm manager to, since it's better to run an alarm a little late rather than very late.

Historically, if a SQLite DOs local alarm time in SQLite didn't match
the AlarmManager's alarm time, we would reschedule the alarm and try to
run the alarm again later.

However, if the time we are attempting to sync to is already before the
current real system time, there is no point in rescheduling to run
later, since we might as well run now.

This commit runs the alarm immediately if the current system time is
already past the time we are trying to sync the alarm manager to, since
it's better to run an alarm a little late rather than very late.
@MellowYarker MellowYarker marked this pull request as ready for review January 5, 2026 14:55
@MellowYarker MellowYarker requested review from a team as code owners January 5, 2026 14:55
Copy link
Contributor

@jqmmes jqmmes left a comment

Choose a reason for hiding this comment

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

LGTM

static constexpr kj::Date fourMs = 4 * kj::MILLISECONDS + kj::UNIX_EPOCH;
static constexpr kj::Date fiveMs = 5 * kj::MILLISECONDS + kj::UNIX_EPOCH;
// Used as the "current time" parameter for armAlarmHandler in tests.
// Set to epoch (before all test alarm times) so existing tests aren't affected by
Copy link
Member

Choose a reason for hiding this comment

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

Making sure existing tests aren't affected is great, but is this new behavior worth covering with a new test case?

// If the local alarm time is already in the past, just run the handler now. This avoids
// blocking alarm execution on storage sync when storage is overloaded. The alarm will
// either delete itself on success or reschedule on failure.
if (localAlarmState != kj::none && willFireEarlier(localAlarmState, currentTime)) {
Copy link
Member

Choose a reason for hiding this comment

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

Given that this code block appears to be identical both here and below, can it just be moved up above the if (willFireEarlier(scheduledTime, localAlarmState)) { ... } else { ...} block rather than being repeated in both the if and the else?

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.

3 participants