Fix foreground crash caused by a race condition on ProfileModel

`seen_event` set was not isolated, which lead to occasional race
conditions between different actors accessing it simultaneously, leading
to crashes.

Closes: https://github.com/damus-io/damus/issues/3311
Changelog-Fixed: Fixed an occasional random crash related to viewing profiles
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-11-03 15:01:14 -08:00
parent 58f4988237
commit a31f6bce0e

View File

@@ -29,6 +29,7 @@ class ProfileModel: ObservableObject, Equatable {
let pubkey: Pubkey let pubkey: Pubkey
let damus: DamusState let damus: DamusState
@MainActor // Isolate this to the main actor to avoid crashes arising from race conditions
var seen_event: Set<NoteId> = Set() var seen_event: Set<NoteId> = Set()
var findRelaysListener: Task<Void, Never>? = nil var findRelaysListener: Task<Void, Never>? = nil
@@ -115,8 +116,8 @@ class ProfileModel: ObservableObject, Equatable {
let conversations_filter_us = NostrFilter(kinds: conversation_kinds, pubkeys: [pubkey], limit: limit, authors: [damus.pubkey]) let conversations_filter_us = NostrFilter(kinds: conversation_kinds, pubkeys: [pubkey], limit: limit, authors: [damus.pubkey])
print("subscribing to conversation events from and to profile \(pubkey)") print("subscribing to conversation events from and to profile \(pubkey)")
for await noteLender in self.damus.nostrNetwork.reader.streamIndefinitely(filters: [conversations_filter_them, conversations_filter_us]) { for await noteLender in self.damus.nostrNetwork.reader.streamIndefinitely(filters: [conversations_filter_them, conversations_filter_us]) {
try? noteLender.borrow { ev in try? await noteLender.borrow { ev in
if !seen_event.contains(ev.id) { if await !seen_event.contains(ev.id) {
let event = ev.toOwned() let event = ev.toOwned()
Task { await self.add_event(event) } Task { await self.add_event(event) }
conversation_events.insert(ev.id) conversation_events.insert(ev.id)