Skip to content

fix(wdk): prevent resetting cron job lastRun timestamps to 0 in storage#1016

Closed
Ysh204 wants to merge 3 commits into
0xsequence:masterfrom
Ysh204:fix/cron-persistence
Closed

fix(wdk): prevent resetting cron job lastRun timestamps to 0 in storage#1016
Ysh204 wants to merge 3 commits into
0xsequence:masterfrom
Ysh204:fix/cron-persistence

Conversation

@Ysh204

@Ysh204 Ysh204 commented Jun 21, 2026

Copy link
Copy Markdown

Description

This PR resolves a critical bug in the WDK Cron scheduler utility (packages/wallet/wdk/src/sequence/cron.ts) that caused persisted cron job lastRun timestamps to be overwritten with 0 in localStorage when WDK reloaded or initialized.

The Bug

  1. When WDK is instantiated, registered jobs are initialized with lastRun = 0.
  2. The background checking loop runJobs() loads the persisted last run timestamps from localStorage asynchronously.
  3. If the elapsed time since the persisted lastRun is less than the job's interval, the job's handler is not executed.
  4. Because the handler did not execute, the in-memory job.lastRun remained at 0.
  5. At the end of runJobs(), syncWithStorage() serialized the in-memory this.jobs (where the job's lastRun was 0) and wrote it back to localStorage, erasing the actual last run timestamp.
  6. Consequently, on the very next tick, the scheduler loaded lastRun: 0 and triggered the job prematurely.

In practice, this meant every background cron job (such as checking queued recovery payloads) would run on every single app restart/tab refresh plus exactly 1 tick later, completely ignoring the configured execution interval.

The Fix

Update runJobs() in cron.ts to sync the in-memory job.lastRun state with the loaded lastRun value from storage before evaluating the elapsed time. This ensures that non-triggered jobs preserve their actual persisted timestamps when writing back to storage.

Testing & Verification

  • Added a comprehensive unit test suite in packages/wallet/wdk/test/cron.test.ts to verify last run persistence, check skip behavior, and ensure correct interval triggers.
  • Verified the tests pass successfully (4ms execution time).
  • Verified the tests fail on the original buggy implementation.

@Ysh204 Ysh204 requested a review from a team June 21, 2026 10:14
@Ysh204 Ysh204 requested a review from a team as a code owner June 21, 2026 10:14

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 32f7180fa3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@@ -0,0 +1,5 @@
---
'@0xsequence/dapp-client': patch

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add wallet-wdk to the changeset

This commit changes packages/wallet/wdk/src/sequence/cron.ts in the public @0xsequence/wallet-wdk package, but the changeset only lists @0xsequence/dapp-client. With the repo's Changesets release flow, only packages named in the frontmatter are versioned/changelogged, so the cron persistence fix will not be published to consumers unless @0xsequence/wallet-wdk is included here as a patch release.

Useful? React with 👍 / 👎.

@Ysh204 Ysh204 closed this Jun 21, 2026
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.

1 participant