Repository navigation
refactor(platform-wallet): share the sync loop lifecycle across coordinators - #6163
Conversation
Characterization tests for the five periodic sync coordinators (identity, DashPay, DPNS, platform-address, shielded), ahead of sharing their loop, admission and drain code. No production change. - All five coordinators get the same test hooks (`wedge_sync_slot_for_test`, `release_sync_slot_for_test`, `sync_admission_closed`); the existing tests go through them instead of the private `is_syncing` / `quiescing` fields, so they survive the fields moving. - DPNS had no tests; it now has the full lifecycle set. - Every coordinator now pins: interval clamp, quiesce waiting on the slot, the timeout latch, refusal behind a busy slot (which stays with its owner) and behind a shut gate, held / overlapping / sealed drains, start behind a latch, the drained latch reopening, stop then quick start, the first pass running at start, and a second start being a no-op. - Completion events: fired once per pass and before the slot is released (platform-address, DPNS, shielded). - `sync_wallet`: platform-address keeps two distinct refusal messages; shielded reports `None`, reads the coordinator before admission, and a pass cancelled before it produced anything is neither stamped nor reported. - The manager shutdown test also starts and joins DPNS and shielded. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The five periodic sync coordinators each carried their own copy of the loop thread, the `is_syncing` slot, the admission preamble and the `quiesce*` family. `SyncLoop` (new `manager/sync_loop.rs`) holds that state and those steps once; a coordinator embeds one and keeps its own pass body, result type and completion event. - `SyncLoop::admit` is the preamble: reopen a drained latch, claim the slot, check the gate. It tells a busy slot from a shut gate. - `start_sync_loop` is the loop, unchanged in order: cancel check, pass, sample the latch, sleep the interval or the latched retry interval. - `QuiesceGate`, `drain_pass` and `SyncSlotGuard` stay where they are. DPNS is the first coordinator on it. No behaviour change: same public API, same 8 MB loop stack, same log line, completion event still sent before the slot is released. Its tests pass unedited. Co-Authored-By: Claude Fable 5.2 <[email protected]>
`DashPaySyncManager` embeds `SyncLoop` instead of its own copy of the loop thread, the `is_syncing` slot, the admission preamble and the drain family. No behaviour change: same public API, same 8 MB loop stack, same log line, and still no completion event. Its tests pass unedited. Co-Authored-By: Claude Fable 5.2 <[email protected]>
`IdentitySyncManager` embeds `SyncLoop` instead of its own copy of the loop thread, the `is_syncing` slot, the admission preamble and the drain family. `SyncLoop` is not generic; the coordinator stays generic over its persister. No behaviour change: same public API, same default loop stack, same log line, `sync_now` still returns `()` and fires no completion event. Its tests pass unedited. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…dress) `PlatformAddressSyncManager` embeds `SyncLoop` instead of its own copy of the loop thread, the `is_syncing` slot, the admission preamble and the drain family. `sync_wallet` maps a busy slot and a shut gate to the two refusal messages it had. No behaviour change: same public API, same default loop stack, same log line, completion event still sent before the slot is released. Its tests pass unedited. Co-Authored-By: Claude Fable 5.2 <[email protected]>
`ShieldedSyncManager` embeds `SyncLoop` instead of its own copy of the loop thread, the `is_syncing` slot, the admission preamble and the drain family. The pass-cancellation token stays the coordinator's own, and its ordering is kept as it was: - `sync_now` and `sync_wallet` take the pass token before admission; - `stop()` cancels and rotates the token before it cancels the loop; - `sync_wallet` reads the coordinator before admission, so it returns without touching the slot or the gate when none is configured. No behaviour change: same public API, the loop still passes `force = false`, same log line, a pass cancelled before it produced anything is still neither stamped nor reported, and the completion event is still sent before the slot is released. Its tests pass unedited. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
|
✅ Final review complete — no blockers (commit 0663d29) |
…let-sync-coordinator-lifecycle
…istic Review follow-up on the lifecycle characterization tests. - The four "quiesce blocks until the in-flight pass drains" tests held the slot from a background task for a fixed 200 ms and waited for it with an unbounded loop, so a loaded runner could miss the window and hang. The test now holds the slot itself, polls `quiesce()` once to see it pending with admission shut, releases the slot and awaits the drain under a timeout. - The shielded cancelled-pass test polls the pass by hand, so a pass that read the coordinator before claiming the slot fails an assertion instead of waiting on the lock the test holds. - Shielded gets the sealed-drain-that-times-out test its four siblings already had. - The shielded stamp test is renamed to what it can prove: both sides read the wall clock, so it cannot tell whose stamp was kept. - The identity retry comment named a 15 s default; it is 60 s. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…efactor/platform-wallet-sync-loop # Conflicts: # packages/rs-platform-wallet/src/manager/identity_sync.rs
…-wallet-sync-loop # Conflicts: # packages/rs-platform-wallet/src/manager/dpns_sync.rs
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
thepastaclaw
left a comment
There was a problem hiding this comment.
Final validation — Phase 1 + Phase 2
Changes requested: 1 blocker
- A cancelled worker can repopulate platform-address data after Clear succeeds —
packages/rs-platform-wallet/src/manager/sync_loop.rs:263-268(new)
Impact: A wallet user who clears platform-address data just as a background tick starts can have cached balances and the sync watermark written back without restarting sync.
The PR extracts scheduling, pass admission, and draining into a shared SyncLoop for all five wallet coordinators, leaving each coordinator’s pass body and completion handling in place. Linux wallet tests and Swift and Kotlin SDK checks passed; Rust workspace tests were skipped, and PastaClaw review and PR Hygiene were pending.
🔴 1 blocking | 🟡 3 suggestion(s)
Review provenance
Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 5: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); reviewer 6: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); reviewer 7: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 8: gpt-6.2-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 9: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); reviewer 10: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)
- Triage:
normalbygpt-6.2-sol(effort low) — The refactor centralizes concurrency-sensitive lifecycle logic across five wallet sync coordinators, but the diff does not change a critical surface. - Phase 1 reviewers:
muse-spark-1.3-contributor— general (completed, effort xhigh); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-3.8-flash-high(lane failed),glm-5.3-flash(not used above high effort; tier asks max) - Second Phase-2 pass: on this first full review every Phase-2 reviewer also ran a second, independent time, and the verifier weighed both sets
- Fresh verifier:
gpt-6.2-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-sol— general (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— architecture-layering (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— security-auditor (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— general (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— architecture-layering (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— security-auditor (completed, effort high); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.
In `packages/rs-platform-wallet/src/manager/sync_loop.rs`:
- [BLOCKING] packages/rs-platform-wallet/src/manager/sync_loop.rs:263-268: A cancelled worker can repopulate platform-address data after Clear succeeds
Impact: A wallet user who clears platform-address data just as a background tick starts can have cached balances and the sync watermark written back without restarting sync.
Clear can return success and report sync stopped, yet the cancelled worker can still write a new balance snapshot and watermark. If the worker is descheduled after `cancel.is_cancelled()` but before it claims the sync slot, `reset_platform_address_sync_state` can close admission, cancel the worker, observe an idle slot, reset the wallets, and reopen admission when its guard drops (`manager/mod.rs:1246–1275`). The registry’s clearing latch only prevents new worker registrations. When the old worker resumes, `background_pass()` enters through the reopened gate without checking its token again, and a successful scan calls `persister.store` (`wallet/platform_addresses/sync.rs:172–193`). The base had the same gap in the loop this PR moves into `SyncLoop`. Check the worker’s own token after acquiring the pass permit and before doing wallet work, leaving manual admission independent. Add a test that pauses a tick before admission, completes an unsealed drain, and verifies that the cancelled tick cannot enter the pass body.
- [SUGGESTION] packages/rs-platform-wallet/src/manager/sync_loop.rs:268-281: A restarted loop can sleep a full interval when the old pass drains just after a refused tick
Impact: After restarting sync following a timed-out stop, a wallet user can wait a full configured polling interval for an update even though the old pass has finished.
The loop can miss its one-second recovery retry even though the slot is now free. If the background tick is refused because the old pass still holds the slot, and that pass releases it before the post-pass probe, `reopen_if_drained()` clears the latch and returns `true`. The loop then chooses the normal interval even though the tick did no work. The original loops had the same ordering. Preserve whether the tick was refused during latch recovery and schedule a short retry even if the latch clears before the delay is chosen. Test that specific ordering by releasing the old pass between the refusal and the probe.
- [SUGGESTION] packages/rs-platform-wallet/src/manager/sync_loop.rs:445-455: The retry-cadence test can pass without the worker ever retrying
Impact: On a busy CI runner, maintainers can get a green test even if the loop always sleeps for the configured one-hour interval.
The test does not establish that the worker selected the short delay while the slot was wedged. `is_running()` reports registration, and zero successful passes after 100 ms means either admission was refused or the worker has not run yet. In the latter case, releasing the wedge lets the worker’s first immediate pass satisfy the test. Even observing a refusal is not enough: releasing the slot before the subsequent latch probe can make the unchanged loop select the full interval and fail the test. Keep the slot wedged until the worker signals that it has selected a delay, assert that the delay is `LATCHED_RETRY_INTERVAL`, then release the slot and verify that an admitted pass follows.
- [SUGGESTION] packages/rs-platform-wallet/src/manager/sync_loop.rs:47-48: Discarding the admission result silently releases the sync slot
Impact: If a future coordinator discards `admit()` and proceeds with the pass, reset or shutdown can miss its writes and callbacks.
A drain can report completion while an unguarded pass is still working. A bare `self.sync.admit();` drops the successful permit at the end of the statement, clearing `is_syncing` without a diagnostic. All current callers retain their permit; this is a safeguard for the new shared API, not a current failure or a layer-ownership violation. Mark `Admission` as `#[must_use]` so an accidentally discarded result is diagnosed.
|
Your move: thepastaclaw requested changes on this head; dismiss the review or push a fix; thepastaclaw left review threads unresolved; resolve them. |
A bare `self.sync.admit();` would drop an admitted permit at the end of the statement and release the `is_syncing` slot while the pass went on. Every caller holds its permit; the attribute makes a future slip a compiler warning. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
…-wallet-sync-loop Conflicts in manager/identity_sync.rs: the base reworked the identity coordinator's pass entry and drain (try_sync_pass / SyncAttempt, sync_now_after_in_flight, the persistence barrier in the quiesce family, the gate check before a store). Kept all of it, on SyncLoop: - try_sync_pass maps SyncLoop::admit's Busy / Closed to SyncAttempt. - quiesce_held_within drains through SyncLoop::drain, then waits out the persistence barrier and latches admission on its timeout. - quiesce_sealed_within goes through quiesce_held_within again, so the sealed drain keeps the barrier. - SyncLoop gains admission_closed (no longer test-only), latch_admission_closed and seal_admission for those callers. The base's new identity tests wrote the private is_syncing / quiescing fields; they now use the test hooks (wedge / release / seal), with no other change. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
There was a problem hiding this comment.
Re-review — Final validation — Phase 1 + Phase 2
Updated after discussion: 1 finding(s) of this review were settled in the inline threads.
- ✅ Withdrawn: 🟡 Identity shutdown now duplicates the shared admission-sealing sequence
New on this head: This head marks sync admission as #[must_use] and merges v5.2-dev, preserving identity sync’s persistence barrier and shielded sync’s handling of incomplete scans. The Rust wallet tests and the Swift and Kotlin SDK builds and tests passed; PR Hygiene and PastaClaw review were pending, with no failed code checks.
🟡 3 2 suggestion(s)
Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.
Still open from earlier rounds (1)
- 🟡 Suggestion: The retry-cadence test can pass without the worker ever retrying —
packages/rs-platform-wallet/src/manager/sync_loop.rs:460-474(thread)
⏸️ Awaiting tracking link (1): A restarted loop can sleep a full interval when the old pass drains just after a refused tick (thread). Link an issue on its thread, or ask there and PastaClaw files it.
Review provenance
Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 5: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); reviewer 6: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)
- Triage:
normalbygpt-6.2-sol(effort low) — The refactor centralizes concurrent admission, cancellation, and draining across five wallet coordinators, making behavior preservation nontrivial without changing any of the listed critical surfaces. - Phase 1 reviewers:
muse-spark-1.3-contributor— general (completed, effort xhigh); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-3.8-flash-high(lane failed),glm-5.3-flash(not used above high effort; tier asks max) - Fresh verifier:
gpt-6.2-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-sol— general (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— architecture-layering (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— security-auditor (completed, effort high); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.
In `packages/rs-platform-wallet/src/manager/identity_sync.rs`:
- [SUGGESTION → WITHDRAWN in discussion; do not fix] packages/rs-platform-wallet/src/manager/identity_sync.rs:586-594: Identity shutdown now duplicates the shared admission-sealing sequence
Impact: A future change to drain finalization made only in `SyncLoop::drain_sealed` will reach the other four coordinators but miss identity sync.
The merge preserves shutdown behavior today but leaves the seal-before-release ordering in two places. Identity shutdown previously delegated to `SyncLoop::drain_sealed`; it now calls `quiesce_held_within` to include the new persistence wait, then checks the result, seals admission, and drops the guard itself. Keep the persistence wait in identity sync, but pass its drain result to a shared `SyncLoop` finalizer that seals admission before releasing the guard.
In `packages/rs-platform-wallet/src/manager/sync_loop.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/manager/sync_loop.rs:460-474: The retry-cadence test can pass without the worker ever retrying
(carried; thread: https://github.com/dashpay/platform/pull/6163#discussion_r4694650973)
- [SUGGESTION] packages/rs-platform-wallet/src/manager/sync_loop.rs:268-281: A restarted loop can sleep a full interval when the old pass drains just after a refused tick
(awaiting a tracking link: link an issue on its thread, or ask there and PastaClaw files it; no code change needed; thread: https://github.com/dashpay/platform/pull/6163#discussion_r4694650964)
|
Bots are done — your move: post |
|
/self-reviewed |
|
Policy satisfied — this can merge. |
|
Reviewed the diff against the old per-coordinator code: admission order, permit lifetime across the completion event, shielded's token/stop ordering, the DashPay/DPNS 8 MB stack and platform-address's two refusal messages all carry over unchanged. No blockers from my side.
|
romchornyi
left a comment
There was a problem hiding this comment.
Approving; the table in my comment above lists non-blocking follow-ups.
Follow-up to #6162, which pinned the current behaviour with tests; those tests pass here with their assertions unchanged.
Issue being fixed or feature implemented
The five periodic sync coordinators in
packages/rs-platform-wallet/src/manager/(identity, DashPay, DPNS, platform-address, shielded) each carried their own copy of the same lifecycle: the loop thread, theis_syncingslot, the admission preamble and thequiesce*family. The copies were identical statement for statement apart from the worker key, the stack size, a log label and the pass call, and the drift had already cost a fix that had to touch all five files (#5663).What was done?
One commit per coordinator, each behaviour-preserving.
manager/sync_loop.rswithSyncLoop, embedded by composition in each coordinator:SyncLoop::admitis the admission preamble: reopen a drained latch, claim the slot, check the gate. It returnsAdmitted(PassPermit),BusyorClosed.start_sync_loopis the loop, in the same order as before: cancel check, pass, sample the latch, sleep the interval or the latched retry interval.drain/drain_sealedwrap the existingdrain_pass.QuiesceGate,drain_passandSyncSlotGuardare unchanged and stay inmanager/mod.rs.pub(crate)method, now delegating.sync_walletmapsBusyandClosedto the two refusal messages it had.stop()cancels and rotates the token before it cancels the loop;sync_walletreads the coordinator before admission; the loop passesforce = false.sync_now/sync_walletstaySend + 'staticfor the FFI.Not touched:
rs-platform-wallet-ffi,swift-sdk,kotlin-sdk,rs-unified-sdk-jni.One observable difference: the "sync loop started behind a wedged pass" debug line keeps its text, but is now emitted from
manager::sync_looprather than from each coordinator's module, so its tracing target changes.Production line count is roughly flat (each coordinator loses 46 to 67 lines;
sync_loop.rsadds 291 including docs). What goes away is the five-way duplication of the logic.How Has This Been Tested?
The tests from #6162 pass with their assertions unchanged. One exception to "unedited": identity tests that arrived later with the
v5.2-devmerge (#6283) set the private fields directly, so inidentity_sync.rsthey now go through the test hooks (is_syncing.store(true)→assert!(wedge_sync_slot_for_test()),is_syncing.store(false)→release_sync_slot_for_test(),quiescing.seal()→seal_sync_admission_for_test()). What they assert is the same.sync_loop.rsadds 8 unit tests of its own.After each of the five commits:
RUST_MIN_STACK=4194304 cargo test -p platform-wallet --features shielded manager::— all passed (300 on the current base)cargo check -p platform-wallet --all-targets(withoutshielded)cargo clippy -p platform-wallet --features shielded --tests -- -D warningscargo check -p platform-wallet-ffi --features shielded(proves the futures are stillSend)At the tip, merged with the current
v5.2-dev:cargo test -p platform-wallet --features shielded— 2835 lib tests passed, 0 failed.Not verified: the 8 MB loop stack for DashPay and DPNS has no cheap test; the value is passed through unchanged, but a device run of DashPay/DPNS sync is the real check. Swift and Kotlin builds were not run (their sources are untouched).
Breaking Changes
None.
Checklist:
structure.rs, regeneratedgrovedb-structure.json, and checked the structure viewer link posted on this pull requestFor repository code-owners and collaborators only
🤖 Generated with Claude Code
PR Hygiene ·
0663d29When every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.