Skip to content

fix(wallet_sdk_services): prevent duplicate submissions, stream drops, and blocking in TransactionService - #4242

Open
cyczzhao-lab wants to merge 1 commit into
tari-project:developmentfrom
cyczzhao-lab:fix/transaction-service-submission-watch-and-poll-invariants
Open

cyczzhao-lab wants to merge 1 commit into
tari-project:developmentfrom
cyczzhao-lab:fix/transaction-service-submission-watch-and-poll-invariants

Conversation

@cyczzhao-lab

Copy link
Copy Markdown
Contributor

Description

Fixes four concurrency, event-lag, and retry bugs in tari_ootle_wallet_sdk_services::transaction_service::TransactionService (crates/wallet/sdk_services/src/transaction_service/service.rs):

  1. Prevent concurrent duplicate submission and lost TransactionContext between initial submission and background polls:

    • TransactionService previously stored a new transaction as TransactionStatus::New before recording its TransactionContext in deferred_contexts and without marking its initial submit_transaction call as in flight.
    • A concurrent on_poll (resubmit_new_transactions) could pick up the New row while the initial submission was still in flight, broadcasting the transaction a second time (and if the duplicate call received TransactionRejected, marking the live transaction InvalidTransaction and releasing its linked input locks) or emitting TransactionSubmittedEvent without its TransactionContext (such as ClaimBurn).
    • Coordinates initial submissions with SubmissionCoordinator (InFlightSubmission RAII guard) and populates tracked and deferred_contexts before spawning the network call so resubmit_new_transactions skips in-flight IDs.
  2. Avoid head-of-line blocking the TransactionService event loop during network dry runs and submissions:

    • TransactionService::run previously awaited handle_request(req).await inline inside its tokio::select! loop over an mpsc::channel(1). A single slow submit_dry_run_transaction or submit_transaction network call stalled finalization stream handling, wallet event processing, stale-lock sweeps, and concurrent requests (and reported caller cancellation on reply.send as ServiceShutdown).
    • Performs local transaction registration synchronously and dispatches network submission and dry-run calls onto background tasks over a buffered request channel.
  3. Preserve finalization stream notifications across fast commits, startup New rows, and broadcast event lag:

    • pending was previously seeded only from TransactionStatus::Pending on startup, populated only after events_subscription delivered WalletEvent::TransactionSubmitted, and never resynchronized on RecvError::Lagged. Notifications arriving before submit_transaction returned (while the row was still New) or after a lagged TransactionSubmitted event were ignored.
    • Pre-registers wallet transactions in tracked (New and Pending) before network submission, waits for any in-flight submission write to settle in on_transaction_finalized before checking store status, and resynchronizes tracked from storage on startup and after RecvError::Lagged.
  4. Keep CheckScope::All armed when a catch-up result check fails:

    • check_all_on_next_tick was previously cleared as soon as on_poll(CheckScope::All) spawned its task. If a pending transaction's check_pending_transaction call failed transiently during that catch-up poll (or after a Finalized notification returned None/error), subsequent ticks reverted to CheckScope::PendingFor(silent_transaction_timeout) and stopped querying younger transactions.
    • Tracks must_check_all via AtomicBool and re-arms it whenever a CheckScope::All poll or on_transaction_finalized check does not cleanly resolve.

Motivation and Context

Prevents duplicate network submissions and premature lock release on slow indexers, keeps finalization notifications and concurrent submissions responsive while dry-runs or validator-set syncs are in flight, ensures fast commits and broadcast-lagged submissions still resolve promptly via the SSE stream, and avoids stranding missed finalizations until silent_transaction_timeout after transient query errors.

How Has This Been Tested?

  • Added 5 deterministic multi-threaded integration tests in crates/wallet/sdk_tests/tests/transaction_service.rs:
    • poll_does_not_double_submit_an_in_flight_submission_or_clobber_its_locks
    • slow_dry_run_does_not_block_finalization_stream_or_concurrent_submissions
    • finalization_arriving_during_submission_or_after_event_lag_is_acted_on
    • transient_query_error_on_check_all_poll_retries_on_next_tick
    • startup_tracks_stored_new_transactions_for_early_finalization_notifications
  • Ran cargo test -p tari_ootle_wallet_sdk_services --all-features (37 passed), all tari_ootle_wallet_sdk_tests suites (including 5 consecutive runs of --test transaction_service, 13/13 passed), and cargo test -p tari_ootle_walletd --lib (248 passed).
  • Verified lints.toml cargo clippy --all-targets --all-features, cargo +nightly-2025-12-05 fmt --all -- --check, and ./scripts/file_license_check.sh.

What process can a PR reviewer use to test or verify this change?

cargo test -p tari_ootle_wallet_sdk_tests --test transaction_service
cargo test -p tari_ootle_wallet_sdk_services --all-features

Breaking Changes

  • None
  • Requires data directory to be deleted
  • Other - Please specify

…, and blocking in TransactionService

- Coordinate initial submissions with background polls so in-flight New transactions are not double-submitted or stripped of context
- Dispatch dry-run and transaction submissions onto background tasks so slow network calls do not block finalization events or polls
- Track New and Pending wallet transactions before network submission, await in-flight status writes on early finalization events, and resynchronize tracked IDs after event-channel lag
- Keep CheckScope::All armed until all pending transaction checks complete without error

This branch has not been deployed

No deployments
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