Skip to content

Harden MWC Slatepack runtime and listener lifecycle - #17

Merged
julian-CStack merged 1 commit into
mainfrom
fix/slatepack-runtime-safety
Dec 1, 2026
Merged

julian-CStack merged 1 commit into
mainfrom
fix/slatepack-runtime-safety

Conversation

@sneurlax

@sneurlax sneurlax commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Hardens the MWC runtime paths used by Stack Wallet's shared Slatepack UI.

  • preserve structured native errors and the native finalize/broadcast result (txFinalize returns posted)
  • keep null transaction messages null and avoid duplicate transaction IDs
  • serialize wallet-aware Slatepack encode/decode calls with the native wallet mutex
  • make MWCMQS listener start, stop and replacement explicit, with one listener per process
  • estimate fees with the same smallest-output selection used to create the transaction
  • update the example and CHANGELOG for the changed listener and finalize APIs

Listener lifecycle

  • The native side keeps the process's one listener in a process-wide slot, and Dart refers to it by id. A Dart isolate that did not start the running listener (after a hot restart, or a new Flutter engine in the same process) finds it there, and its next start replaces it. A stale id reads as exited and can never reach freed memory.
  • The native worker starts the broker and then waits for the broker thread to end, so a finished worker means the broker has exited. Stop waits for that, up to 10 s per call, before it reports success. A stopped broker can therefore no longer reset a newer broker's registration when its long poll ends. Once the broker thread has ended, for any reason including a panic, the worker clears its registration, so a crashed broker cannot block later starts.
  • A stop that has not finished leaves the listener registered to its wallet. That wallet's next stop, or a start that needs the slot, finishes stopping it, and no second listener is started meanwhile. A per-listener watcher thread, asleep until the listener is cancelled, keeps stopping and waking the broker after cancellation, so a stop takes effect even when the broker registers, or starts a new long poll, after the stop call returned.
  • startMwcMqsListener returns MwcMqsListenerStartResult (started, reused, keptOtherWallet, deferredToPreferredWallet). It reports started once the worker has run for 2 s, which catches relays that fail by name, connection or TLS. When the worker ends during startup it throws with the native error, or with the relay it could not keep. Why a listener that ended on its own ended is kept in ListenerManager.lastExitError.
  • allowTakeover: true (the wallet on screen) replaces another wallet's listener and makes the wallet preferred once its listener runs, or once it has told the previous listener to stop. A takeover that fails to start its own listener does not change the preference. While the preferred wallet has no running listener, other wallets defer to it only for 5 minutes after its last start, health check or stop, so a recovering wallet keeps its priority and a handle that is no longer used (for example after the wallet was reopened) stops blocking the others. releasePreference: true gives the priority up on exit. Listener timing uses a monotonic clock.
  • Listener start and stop are serialized by their own mutex, not the wallet mutex, so balance, transaction and send calls never wait on the relay.

The listener's running state still reports worker health, not a completed relay authentication: a relay that never answers is noticed only once the worker ends, up to 30 s later, and Stack Wallet's periodic startMwcMqsListener call then replaces it. Upstream registers the broker inside the same call that starts it, so a start cancelled at that moment can run its broker briefly before the watcher stops it.

Verification

  • cargo test --locked: 3 passed, including a native start failure that reports its error, the one-listener slot, and a released id
  • flutter test test/protocol_result_test.dart: 23 passed. The listener state machine is driven through a fake native layer (reuse, background start, takeover, failed takeover, takeover while the previous listener is still stopping, preference expiry, unfinished stop, a leftover listener from another isolate, the exit reason, start errors, settle, late start and timeout, stop not holding the wallet mutex), plus one start failure through the real native library
  • dart analyze lib test: no errors; 1 existing warning
  • dnix.lock rebuilt for all 11 targets on macOS with Nix and Xcode 27.0 (native-0a96723388a1)
  • the x86_64 Linux library built here by the test run matches the committed lock exactly

@sneurlax
sneurlax force-pushed the fix/slatepack-runtime-safety branch from 84815e2 to 041c8f2 Compare December 1, 2026 18:06
@julian-CStack
julian-CStack merged commit d4852f3 into main Dec 1, 2026
4 of 7 checks passed
@julian-CStack
julian-CStack deleted the fix/slatepack-runtime-safety branch December 1, 2026 18:58
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