Fix memory race condition

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-08-18 17:17:26 -07:00
parent 578d47356d
commit 5f3ce30826
2 changed files with 10 additions and 5 deletions

View File

@@ -99,6 +99,7 @@ class RelayPool {
} }
} }
@MainActor
func register_handler(sub_id: String, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) { func register_handler(sub_id: String, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) {
for handler in handlers { for handler in handlers {
// don't add duplicate handlers // don't add duplicate handlers
@@ -201,9 +202,11 @@ class RelayPool {
} }
func subscribe(sub_id: String, filters: [NostrFilter], handler: @escaping (RelayURL, NostrConnectionEvent) -> (), to: [RelayURL]? = nil) { func subscribe(sub_id: String, filters: [NostrFilter], handler: @escaping (RelayURL, NostrConnectionEvent) -> (), to: [RelayURL]? = nil) {
register_handler(sub_id: sub_id, handler: handler) Task {
await register_handler(sub_id: sub_id, handler: handler)
send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to) send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to)
} }
}
/// Subscribes to data from the `RelayPool` based on a filter and a list of desired relays. /// Subscribes to data from the `RelayPool` based on a filter and a list of desired relays.
/// ///
@@ -264,9 +267,11 @@ class RelayPool {
} }
func subscribe_to(sub_id: String, filters: [NostrFilter], to: [RelayURL]?, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) { func subscribe_to(sub_id: String, filters: [NostrFilter], to: [RelayURL]?, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) {
register_handler(sub_id: sub_id, handler: handler) Task {
await register_handler(sub_id: sub_id, handler: handler)
send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to) send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to)
} }
}
func count_queued(relay: RelayURL) -> Int { func count_queued(relay: RelayURL) -> Int {
var c = 0 var c = 0

View File

@@ -60,7 +60,7 @@ class PostBox {
init(pool: RelayPool) { init(pool: RelayPool) {
self.pool = pool self.pool = pool
self.events = [:] self.events = [:]
pool.register_handler(sub_id: "postbox", handler: handle_event) Task { await pool.register_handler(sub_id: "postbox", handler: handle_event) }
} }
// only works reliably on delay-sent events // only works reliably on delay-sent events