Add @MainActor isolation to BitchatMessage for thread safety#1018
Add @MainActor isolation to BitchatMessage for thread safety#1018j0KZ wants to merge 1 commit intopermissionlesstech:mainfrom
Conversation
Marks BitchatMessage class with @mainactor to ensure thread-safe access to mutable state (deliveryStatus, cached formatted text). Changes: - Add @mainactor attribute to BitchatMessage class - Mark CodingKeys enum as nonisolated for Codable conformance - Mark init as nonisolated to allow creation from BLE callbacks - Mark toBinaryPayload() and init?(_ data:) as nonisolated for serialization - Update documentation to note MainActor isolation and nonisolated methods The nonisolated init allows BitchatMessage to be created from any context (e.g., CoreBluetooth callbacks on background threads) while mutable state access (deliveryStatus, cached text) remains MainActor-isolated for safety. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
5dc9f82 to
4ebc43e
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5dc9f82da3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| @MainActor | ||
| final class BitchatMessage: Codable { |
There was a problem hiding this comment.
Avoid main-actor isolating the entire message model
Marking the whole BitchatMessage type as @MainActor makes its initializers and property reads main-actor isolated. Several call sites construct messages on background queues (for example, CoreBluetooth callbacks in BLEService build BitchatMessage instances before notifyUI hops to the main actor). With this change, those paths either fail to compile under strict concurrency or will synchronously hop to the main actor, which can block BLE processing and cause UI jank. Consider isolating only the mutable cached state (or moving it into a separate @MainActor wrapper) so message creation/parsing can remain off the main thread.
Useful? React with 👍 / 👎.
Summary
BitchatMessageclass with@MainActorto ensure thread-safe access to mutable statedeliveryStatusis updated from background threads while being read from the UIChanges
@MainActorattribute toBitchatMessageclassCodingKeysenum asnonisolatedfor Codable conformanceContext
The
deliveryStatusproperty and cached formatted text dictionary are mutable state that can be accessed from multiple threads. By isolating the entire class to MainActor, we ensure all access is serialized on the main thread where SwiftUI updates occur.Test plan
🤖 Generated with Claude Code