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
6 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/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/client.dart"; import "package:nexus/controllers/client.dart";
import "package:nexus/controllers/pending_subprofiles.dart";
import "package:nexus/controllers/rooms.dart"; import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/per_message_profiles.dart"; import "package:nexus/models/per_message_profiles.dart";
import "package:nexus/models/account_data.dart"; import "package:nexus/models/account_data.dart";
@ -85,24 +86,33 @@ class AccountDataController extends Notifier<AccountData> {
(data) => data.perMessageProfiles == profiles, (data) => data.perMessageProfiles == profiles,
); );
Future<void> setRoomProfile(String roomId, String profileId) async { Future<void> setRoomProfile(String roomId, String profileId) {
const type = AccountData.perMessageProfilesKey; const type = AccountData.perMessageProfilesKey;
final rooms = ref.read(RoomsController.provider.notifier); const key = "default_profile_id";
final previous = ref final current = ref
.read(RoomsController.provider)[roomId] .read(RoomsController.provider)[roomId]
?.accountData[type]; ?.accountData[type];
final content = {...?previous, "default_profile_id": profileId};
// Shown straight away, rather than once it comes down sync return ref.read(PendingSubprofilesController.provider.notifier).track(
rooms.setAccountData(roomId, type, content); roomId,
try { profileId,
() async {
await ref await ref
.read(ClientController.provider.notifier) .read(ClientController.provider.notifier)
.setAccountData(.new(type: type, roomId: roomId, content: content)); .setAccountData(
} catch (_) { .new(
rooms.setAccountData(roomId, type, previous); type: type,
rethrow; 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 /// 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:fluttertagger/fluttertagger.dart";
import "package:nexus/controllers/attachment.dart"; import "package:nexus/controllers/attachment.dart";
import "package:nexus/controllers/client.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/event.dart";
import "package:nexus/controllers/rooms.dart"; import "package:nexus/controllers/rooms.dart";
import "package:nexus/controllers/thread_replies.dart"; import "package:nexus/controllers/thread_replies.dart";
@ -399,6 +401,7 @@ class RoomChatController(final ChatParams info)
required RelationType relationType, required RelationType relationType,
int? relatedEventRowId, int? relatedEventRowId,
}) async { }) async {
final subprofile = subprofileFor(text);
final events = ref.read(RoomsController.provider)[info.roomId]?.events; final events = ref.read(RoomsController.provider)[info.roomId]?.events;
final relatedEvent = relatedEventRowId == null final relatedEvent = relatedEventRowId == null
? null ? null
@ -437,6 +440,7 @@ class RoomChatController(final ChatParams info)
); );
final client = ref.read(ClientController.provider.notifier); final client = ref.read(ClientController.provider.notifier);
await awaitSubprofile(text, subprofile);
final event = await client.sendMessage( final event = await client.sendMessage(
SendMessageRequest( SendMessageRequest(
roomId: info.roomId, roomId: info.roomId,
@ -458,6 +462,24 @@ class RoomChatController(final ChatParams info)
addSent(event); 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. /// Shows [event], which was just sent, until it comes down sync.
void addSent(Event? event) { void addSent(Event? event) {
if (event case final event?) { if (event case final event?) {
@ -614,6 +636,7 @@ class RoomChatController(final ChatParams info)
.read(RoomsController.provider)[info.roomId] .read(RoomsController.provider)[info.roomId]
?.events[repliedEventRowId]; ?.events[repliedEventRowId];
await awaitSubprofile("", subprofileFor(""));
final event = await ref final event = await ref
.read(ClientController.provider.notifier) .read(ClientController.provider.notifier)
.sendMessage( .sendMessage(

View file

@ -136,24 +136,6 @@ class RoomsController extends Notifier<IMap<String, Room>> {
.add(event.eventId, event.rowId); .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( void update(
IMap<String, Room> rooms, [ IMap<String, Room> rooms, [
ISet<String> leftRooms = const .empty(), 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/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/account_data.dart"; import "package:nexus/controllers/account_data.dart";
import "package:nexus/controllers/pending_subprofiles.dart";
import "package:nexus/controllers/rooms.dart"; import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/account_data.dart"; import "package:nexus/models/account_data.dart";
import "package:nexus/models/per_message_profiles.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( return .new(
profiles: profiles, profiles: profiles,
triggerOrder: (room?.profiles ?? const IList<StoredProfile>.empty()) triggerOrder: (room?.profiles ?? const IList<StoredProfile>.empty())
.addAll(global.profiles), .addAll(global.profiles),
current: currentId == null || currentId.isEmpty current: byId(
? null ref.watch(
: profiles.firstWhereOrNull((profile) => profile.id == currentId), PendingSubprofilesController.provider.select(
(pending) => pending[roomId]?.id,
),
) ??
syncedId,
),
synced: byId(syncedId),
); );
} }

View file

@ -11,6 +11,7 @@ class const RoomSubprofiles({
// Each trigger is tried in this order, as gomuks does when sending // Each trigger is tried in this order, as gomuks does when sending
required final IList<StoredProfile> triggerOrder, required final IList<StoredProfile> triggerOrder,
final StoredProfile? current, final StoredProfile? current,
final StoredProfile? synced,
}) with _$RoomSubprofiles { }) with _$RoomSubprofiles {
StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull( StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull(
(profile) => (profile) =>