Fix ConversationController load race condition

This commit is contained in:
Evan Hahn 2021-11-04 16:11:47 -05:00 committed by GitHub
parent d6ffb08a63
commit 8256170066
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 127 additions and 106 deletions

View file

@ -114,19 +114,11 @@ export function start(): void {
}
export class ConversationController {
private _initialFetchComplete: boolean | undefined;
private _initialFetchComplete = false;
private _initialPromise: Promise<void> = Promise.resolve();
private _initialPromise: undefined | Promise<void>;
private _conversations: ConversationModelCollectionType;
constructor(conversations?: ConversationModelCollectionType) {
if (!conversations) {
throw new Error('ConversationController: need conversation collection!');
}
this._conversations = conversations;
}
constructor(private _conversations: ConversationModelCollectionType) {}
get(id?: string | null): ConversationModel | undefined {
if (!this._initialFetchComplete) {
@ -255,7 +247,7 @@ export class ConversationController {
type: ConversationAttributesTypeType,
additionalInitialProps = {}
): Promise<ConversationModel> {
await this._initialPromise;
await this.load();
const conversation = this.getOrCreate(id, type, additionalInitialProps);
if (conversation) {
@ -756,28 +748,24 @@ export class ConversationController {
);
}
async loadPromise(): Promise<void> {
return this._initialPromise;
}
reset(): void {
this._initialPromise = Promise.resolve();
delete this._initialPromise;
this._initialFetchComplete = false;
this._conversations.reset([]);
}
isFetchComplete(): boolean | undefined {
return this._initialFetchComplete;
load(): Promise<void> {
this._initialPromise ||= this.doLoad();
return this._initialPromise;
}
async load(): Promise<void> {
private async doLoad(): Promise<void> {
log.info('ConversationController: starting initial fetch');
if (this._conversations.length) {
throw new Error('ConversationController: Already loaded!');
}
const load = async () => {
try {
const collection = await getAllConversations({
ConversationCollection: window.Whisper.ConversationCollection,
@ -855,10 +843,5 @@ export class ConversationController {
);
throw error;
}
};
this._initialPromise = load();
return this._initialPromise;
}
}

View file

@ -812,9 +812,6 @@ export async function startApp(): Promise<void> {
}
});
// We start this up before window.ConversationController.load() to
// ensure that our feature flags are represented in the cached props
// we generate on load of each convo.
window.Signal.RemoteConfig.initRemoteConfig(server);
let retryReceiptLifespan: number | undefined;

View file

@ -91,7 +91,7 @@ export class NormalMessageSendJobQueue extends JobQueue<NormalMessageSendJobData
timeRemaining,
});
await window.ConversationController.loadPromise();
await window.ConversationController.load();
const message = await getMessageById(messageId);
if (!message) {

View file

@ -70,7 +70,7 @@ export class ReactionJobQueue extends JobQueue<ReactionJobData> {
timeRemaining,
});
await window.ConversationController.loadPromise();
await window.ConversationController.load();
const ourConversationId = window.ConversationController.getOurConversationIdOrThrow();

View file

@ -70,7 +70,7 @@ function openInbox(): ThunkAction<
return async dispatch => {
log.info('open inbox');
await window.ConversationController.loadPromise();
await window.ConversationController.load();
dispatch({
type: OPEN_INBOX,

View file

@ -43,7 +43,7 @@ describe('Conversations', () => {
deviceName: 'my device',
password: 'password',
});
await window.ConversationController.loadPromise();
await window.ConversationController.load();
await window.Signal.Data.saveConversation(conversation.attributes);

View file

@ -55,7 +55,6 @@ describe('KeyChangeListener', () => {
beforeEach(async () => {
window.ConversationController.reset();
await window.ConversationController.load();
await window.ConversationController.loadPromise();
convo = window.ConversationController.dangerouslyCreateAndAdd({
id: uuidWithKeyChange,

View file

@ -13072,6 +13072,34 @@
"reasonCategory": "usageTrusted",
"updated": "2021-10-22T00:52:39.251Z"
},
{
"rule": "jQuery-load(",
"path": "ts/jobs/normalMessageSendJobQueue.js",
"line": " await window.ConversationController.load();",
"reasonCategory": "falseMatch",
"updated": "2021-11-04T16:14:03.477Z"
},
{
"rule": "jQuery-load(",
"path": "ts/jobs/normalMessageSendJobQueue.ts",
"line": " await window.ConversationController.load();",
"reasonCategory": "falseMatch",
"updated": "2021-11-04T16:14:03.477Z"
},
{
"rule": "jQuery-load(",
"path": "ts/jobs/reactionJobQueue.js",
"line": " await window.ConversationController.load();",
"reasonCategory": "falseMatch",
"updated": "2021-11-04T16:14:03.477Z"
},
{
"rule": "jQuery-load(",
"path": "ts/jobs/reactionJobQueue.ts",
"line": " await window.ConversationController.load();",
"reasonCategory": "falseMatch",
"updated": "2021-11-04T16:14:03.477Z"
},
{
"rule": "jQuery-append(",
"path": "ts/logging/debuglogs.js",
@ -13215,6 +13243,20 @@
"updated": "2021-01-21T23:06:13.270Z",
"reasonDetail": "Doesn't manipulate the DOM."
},
{
"rule": "jQuery-load(",
"path": "ts/state/ducks/app.js",
"line": " await window.ConversationController.load();",
"reasonCategory": "falseMatch",
"updated": "2021-11-04T16:14:03.477Z"
},
{
"rule": "jQuery-load(",
"path": "ts/state/ducks/app.ts",
"line": " await window.ConversationController.load();",
"reasonCategory": "falseMatch",
"updated": "2021-11-04T16:14:03.477Z"
},
{
"rule": "jQuery-load(",
"path": "ts/types/Stickers.js",