PMP race condition fixes

This commit is contained in:
Henry Hiles 2026-11-26 16:21:43 -05:00
commit 3cf0b811bb
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
5 changed files with 108 additions and 36 deletions

View file

@ -5,6 +5,7 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/client.dart";
import "package:nexus/controllers/pending_subprofiles.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/per_message_profiles.dart";
import "package:nexus/models/account_data.dart";
@ -85,24 +86,33 @@ class AccountDataController extends Notifier<AccountData> {
(data) => data.perMessageProfiles == profiles,
);
Future<void> setRoomProfile(String roomId, String profileId) async {
Future<void> setRoomProfile(String roomId, String profileId) {
const type = AccountData.perMessageProfilesKey;
final rooms = ref.read(RoomsController.provider.notifier);
final previous = ref
const key = "default_profile_id";
final current = ref
.read(RoomsController.provider)[roomId]
?.accountData[type];
final content = {...?previous, "default_profile_id": profileId};
// Shown straight away, rather than once it comes down sync
rooms.setAccountData(roomId, type, content);
try {
await ref
.read(ClientController.provider.notifier)
.setAccountData(.new(type: type, roomId: roomId, content: content));
} catch (_) {
rooms.setAccountData(roomId, type, previous);
rethrow;
}
return ref.read(PendingSubprofilesController.provider.notifier).track(
roomId,
profileId,
() async {
await ref
.read(ClientController.provider.notifier)
.setAccountData(
.new(
type: type,
roomId: roomId,
content: {...?current, key: profileId},
),
);
await ref
.read(RoomsController.provider.notifier)
.awaitRooms(
(rooms) => rooms[roomId]?.accountData[type]?[key] == profileId,
);
},
);
}
/// Saves [rule] of [kind], creating it if it's new, or otherwise changing only

View file

@ -0,0 +1,45 @@
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
typedef PendingSubprofile = ({String id, Future<void> synced});
/// Subprofiles just chosen for rooms, by room ID, until they come down sync.
/// gomuks only learns of them then, so messages sent before would use the
/// previous one.
class PendingSubprofilesController
extends Notifier<IMap<String, PendingSubprofile>> {
@override
IMap<String, PendingSubprofile> build() => const .empty();
Future<void> track(
String roomId,
String id,
Future<void> Function() save,
) async {
final synced = save();
state = state.add(roomId, (id: id, synced: synced));
try {
await synced;
} finally {
// Unless another subprofile was chosen meanwhile
if (state[roomId]?.synced == synced) state = state.remove(roomId);
}
}
Future<void> settled(String roomId) async {
try {
await state[roomId]?.synced;
} catch (_) {
throw Exception(
"The message wasn't sent, as the subprofile chosen for this room couldn't be saved",
);
}
}
static final provider =
NotifierProvider<
PendingSubprofilesController,
IMap<String, PendingSubprofile>
>(PendingSubprofilesController.new);
}

View file

@ -7,6 +7,8 @@ import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:fluttertagger/fluttertagger.dart";
import "package:nexus/controllers/attachment.dart";
import "package:nexus/controllers/client.dart";
import "package:nexus/controllers/pending_subprofiles.dart";
import "package:nexus/controllers/subprofiles.dart";
import "package:nexus/controllers/event.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/controllers/thread_replies.dart";
@ -399,6 +401,7 @@ class RoomChatController(final ChatParams info)
required RelationType relationType,
int? relatedEventRowId,
}) async {
final subprofile = subprofileFor(text);
final events = ref.read(RoomsController.provider)[info.roomId]?.events;
final relatedEvent = relatedEventRowId == null
? null
@ -437,6 +440,7 @@ class RoomChatController(final ChatParams info)
);
final client = ref.read(ClientController.provider.notifier);
await awaitSubprofile(text, subprofile);
final event = await client.sendMessage(
SendMessageRequest(
roomId: info.roomId,
@ -458,6 +462,24 @@ class RoomChatController(final ChatParams info)
addSent(event);
}
String? subprofileFor(String text, {bool synced = false}) {
final subprofiles = ref.read(SubprofilesController.provider(info.roomId));
return (subprofiles.triggeredBy(text) ??
(synced ? subprofiles.synced : subprofiles.current))
?.id;
}
Future<void> awaitSubprofile(String text, String? shown) async {
await ref
.read(PendingSubprofilesController.provider.notifier)
.settled(info.roomId);
if (subprofileFor(text, synced: true) != shown) {
throw Exception(
"The message wasn't sent, as it would have been sent as a different subprofile than shown",
);
}
}
/// Shows [event], which was just sent, until it comes down sync.
void addSent(Event? event) {
if (event case final event?) {
@ -614,6 +636,7 @@ class RoomChatController(final ChatParams info)
.read(RoomsController.provider)[info.roomId]
?.events[repliedEventRowId];
await awaitSubprofile("", subprofileFor(""));
final event = await ref
.read(ClientController.provider.notifier)
.sendMessage(

View file

@ -136,24 +136,6 @@ class RoomsController extends Notifier<IMap<String, Room>> {
.add(event.eventId, event.rowId);
});
void setAccountData(
String roomId,
String type,
Map<String, dynamic>? content,
) {
if (state[roomId] case final room?) {
state = state.add(
roomId,
room.copyWith(
accountData: switch (content) {
final content? => room.accountData.add(type, content),
null => room.accountData.remove(type),
},
),
);
}
}
void update(
IMap<String, Room> rooms, [
ISet<String> leftRooms = const .empty(),

View file

@ -3,6 +3,7 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/account_data.dart";
import "package:nexus/controllers/pending_subprofiles.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/account_data.dart";
import "package:nexus/models/per_message_profiles.dart";
@ -39,14 +40,24 @@ class SubprofilesController(final String roomId)
),
);
final currentId = room?.defaultProfileId ?? global.defaultProfileId;
StoredProfile? byId(String? id) => id == null || id.isEmpty
? null
: profiles.firstWhereOrNull((profile) => profile.id == id);
final syncedId = room?.defaultProfileId ?? global.defaultProfileId;
return .new(
profiles: profiles,
triggerOrder: (room?.profiles ?? const IList<StoredProfile>.empty())
.addAll(global.profiles),
current: currentId == null || currentId.isEmpty
? null
: profiles.firstWhereOrNull((profile) => profile.id == currentId),
current: byId(
ref.watch(
PendingSubprofilesController.provider.select(
(pending) => pending[roomId]?.id,
),
) ??
syncedId,
),
synced: byId(syncedId),
);
}