Skip to content

fix(sync): stop idle CPU spin after local configuration changes - #372

Open
sunnysab wants to merge 1 commit into
ThinkWatchProject:mainfrom
sunnysab:fix/sync-idle-dirty-deadline
Open

sunnysab wants to merge 1 commit into
ThinkWatchProject:mainfrom
sunnysab:fix/sync-idle-dirty-deadline

Conversation

@sunnysab

Copy link
Copy Markdown

Summary

Fix sustained CPU usage after a local configuration change when sync is disabled, paused, or cannot be initialized. No dependency upgrades or polling delays are needed.

Root cause

Sync::touch() records a dirty deadline (now + SETTLE). The background scheduler takes the earlier of this deadline and the next periodic poll.

Sync::round() previously cleared dirty only after ensure() and the paused check. Its early returns for Off, Misconfigured, and Paused therefore left the dirty deadline behind. Once that deadline expired, every subsequent scheduler iteration called sleep_until() on the same past instant. Scheduling the next periodic poll did not help, because the expired dirty deadline continued to win.

Move the existing deadline consumption immediately after acquiring the round mutex, before these early returns. A touch() during the asynchronous engine round still schedules a subsequent round, and the existing periodic polling/retry policy is unchanged.

Reproduction and verification

Reproduced with the shipped 0.81.0 Linux binary using only synthetic configuration and a private temporary directory:

  1. Start core with a synthetic control/gateway key, loopback binding, no providers, automatic pricing updates disabled, and no sync section.
  2. Let startup finish: CPU usage is approximately 0%.
  3. Edit the configuration to rename a client, causing a real configuration reload.
  4. Wait past the 10-second settle deadline.
  5. Core consumes approximately 113.2% CPU despite /sync reporting off. No desktop client or gateway traffic is required.

The new dirty-deadline regression assertion also fails on unmodified main. With this fix, the same real-process reproduction using a locally built core remains at 0% CPU after the edit.

The regression test covers Off, Paused, and Misconfigured early returns, including a second local change in each state.

Validation

  • cargo build --locked -p twcore
  • cargo fmt --all -- --check
  • cargo clippy --locked --workspace --all-targets -- -D warnings
  • cargo test --locked --workspace — 4,017 passed, 0 failed, 9 ignored
  • bash scripts/smoke.sh — 84 passed, 0 failed
  • Isolated real-process CPU reproduction before and after the fix

The build reports an existing future-incompatibility warning in proc-macro-error2 2.0.1; this change does not alter dependencies.

Clear the pending local-change deadline after acquiring the round lock, before returning for disabled, paused or misconfigured sync. Otherwise a configuration change leaves an expired deadline that keeps the background scheduler runnable and consumes a CPU core even when sync is off.

Cover all three early-return states and repeated local changes with a regression test.
Copilot AI balanced review requested due to automatic review settings October 24, 2026 10:24
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@sunnysab

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Oct 24, 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.

2 participants