Why Nostr? What is Njump?
2023-05-10 04:50:03

prs on Nostr: fix crash related to preloading events (#1115) This PR fixes the top crash in build ...

fix crash related to preloading events (#1115)

This PR fixes the top crash in build 1.4.3(24):

<img width="376" alt="damus_—_Profiles_swift" src="https://github.com/damus-io/damus/assets/445882/1cf51c52-920d-43a7-a832-0776790557d8">;

The crash is caused by a data contention for the `profiles` dictionary in the `Profiles` class. Many views and view models access this dictionary from the main thread by calling `Profiles.lookup(id:)`. `Profiles.add(id:profile:)` is currently called only from the main thread. However, the stack trace in the crash report indicates that it happens when `Profiles.lookup(id:)` is called from a non-main thread during event preloading. When a profile is added on the main thread and a profile lookup occurs on a non-main thread at the same time, we have a data contention and the app crashes.

To resolve this, I added a concurrent `DispatchQueue` to synchronize access to the `profiles` dictionary. Concurrent reads can happen safely from both main and non-main threads, but writes will happen asynchronously and use the `barrier` flag to enforce exclusive access.

An alternative fix would be to make the `Profiles` class an `actor` instead of a `class`, which automatically synchronizes access to all of its members. I chose not to pursue this solution because it would also make `Profiles.lookup(id:)` `async`, which would change all the call sites.

profiles-crash, 1 commits

https://github.com/damus-io/damus/pull/1115
Author Public Key
npub1ahckk8wkr64n2w5r4arsesf42upfhlmgy76vhxmlex7mvv4zhrnqry7v97