-
Notifications
You must be signed in to change notification settings - Fork 301
Open
Labels
bugSomething is not working, or not working as intendedSomething is not working, or not working as intended
Description
Problem
The continuations dictionary in WalletModel is accessed from network callbacks (handle_nwc_response) and UI thread (waitForResponse) without synchronization, causing data races and potential crashes.
Location
damus/Features/Wallet/Models/WalletModel.swift lines 46, 212-240
class WalletModel: ObservableObject {
private var continuations: [NoteId: CheckedContinuation<WalletConnect.Response.Result, any Error>] = [:]
func handle_nwc_response(response: WalletConnect.FullWalletResponse) {
// Called from background thread (network callback)
self.resume(request: response.req_id, with: result) // RACE
}
func waitForResponse(for requestId: NoteId, ...) async throws -> ... {
return try await withCheckedThrowingContinuation({ continuation in
self.continuations[requestId] = continuation // RACE
})
}
}Root Cause
Swift dictionaries are not thread-safe. Concurrent read/write from different threads causes undefined behavior.
Impact
- Crash: "Invalid memory access" or EXC_BAD_ACCESS
- Lost wallet responses
- Continuation resumed twice
Suggested Fix
Either:
- Add NSLock protection around all
continuationsaccess - Convert
WalletModelto an actor (preferred per Swift concurrency guidelines)
Test Plan
- Test multiple concurrent wallet requests
- Test response arriving during request setup
- Run with ThreadSanitizer
Changelog-Fixed: Fix race condition in WalletModel continuation handling
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething is not working, or not working as intendedSomething is not working, or not working as intended