Skip to content

Handle disposal of message store correctly. #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/model/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mixin MessageStore {
/// All known messages, indexed by [Message.id].
Map<int, Message> get messages;

Set<MessageListView> get debugMessageListViews;

void registerMessageList(MessageListView view);
void unregisterMessageList(MessageListView view);

Expand Down Expand Up @@ -37,6 +39,9 @@ class MessageStoreImpl with MessageStore {

final Set<MessageListView> _messageListViews = {};

@override
Set<MessageListView> get debugMessageListViews => _messageListViews;

@override
void registerMessageList(MessageListView view) {
final added = _messageListViews.add(view);
Expand All @@ -56,7 +61,10 @@ class MessageStoreImpl with MessageStore {
}

void dispose() {
for (final view in _messageListViews) {
// When a MessageListView is disposed, it removes itself from the Set
// `MessageStoreImpl._messageListViews`. Instead of iterating on that Set,
// iterate on a copy, to avoid concurrent modifications.
for (final view in _messageListViews.toList()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit in commit message:

This fixes a bug caused by MessageListView instances unregistering themselves
from the collection containing these instances, while we are iterating the exact
same set when disposing the message store.

lines too long for prose; from https://zulip.readthedocs.io/en/latest/contributing/commit-discipline.html#formatting-guidelines :

Your commit message should be line-wrapped to about 68 characters per line, but no more than 70, so that your commit message will be easy to read in git log in a normal terminal.

These lines are as long as 80 columns.

See also the tip there about configuring Git and your editor.

view.dispose();
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ class PerAccountStore extends ChangeNotifier with ChannelStore, MessageStore {
// TODO(#650) notify [recentDmConversationsView] of the just-fetched messages
}

@override
Set<MessageListView> get debugMessageListViews => _messages.debugMessageListViews;

final MessageStoreImpl _messages;

final Unreads unreads;
Expand Down
12 changes: 12 additions & 0 deletions test/model/message_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ void main() {
checkNotified(count: messageList.fetched ? messages.length : 0);
}

test('disposing multiple registered MessageListView instances', () async {
// Regression test for: https://github.com/zulip/zulip-flutter/issues/810
await prepare(narrow: const MentionsNarrow());
MessageListView.init(store: store, narrow: const StarredMessagesNarrow());
check(store.debugMessageListViews).length.equals(2);

// When disposing, the [MessageListView]s are expected to unregister
// themselves from the message store.
store.dispose();
check(store.debugMessageListViews).isEmpty();
});

group('reconcileMessages', () {
test('from empty', () async {
await prepare();
Expand Down
28 changes: 28 additions & 0 deletions test/model/store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:zulip/api/model/events.dart';
import 'package:zulip/api/model/model.dart';
import 'package:zulip/api/route/events.dart';
import 'package:zulip/api/route/messages.dart';
import 'package:zulip/model/message_list.dart';
import 'package:zulip/model/narrow.dart';
import 'package:zulip/model/store.dart';
import 'package:zulip/notifications/receive.dart';

Expand Down Expand Up @@ -427,6 +429,32 @@ void main() {
check(store.userSettings!.twentyFourHourTime).isTrue();
}));

test('expired queue disposes registered MessageListView instances', () => awaitFakeAsync((async) async {
// Regression test for: https://github.com/zulip/zulip-flutter/issues/810
await prepareStore();
updateMachine.debugPauseLoop();
updateMachine.poll();

// Make sure there are [MessageListView]s in the message store.
MessageListView.init(store: store, narrow: const MentionsNarrow());
MessageListView.init(store: store, narrow: const StarredMessagesNarrow());
check(store.debugMessageListViews).length.equals(2);

// Let the server expire the event queue.
connection.prepare(httpStatus: 400, json: {
'result': 'error', 'code': 'BAD_EVENT_QUEUE_ID',
'queue_id': updateMachine.queueId,
'msg': 'Bad event queue ID: ${updateMachine.queueId}',
});
updateMachine.debugAdvanceLoop();
async.flushMicrotasks();
await Future<void>.delayed(Duration.zero);

// The old store's [MessageListView]s have been disposed.
// (And no exception was thrown; that was #810.)
check(store.debugMessageListViews).isEmpty();
}));

void checkRetry(void Function() prepareError) {
awaitFakeAsync((async) async {
await prepareStore(lastEventId: 1);
Expand Down