From cf505509360eff5f44dec7162577f44c4da8d494 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Thu, 1 Apr 2021 11:34:28 -0700 Subject: [PATCH] Finish all renderer SQL queries switching to main Don't switch to main process until all renderer SQL queries are completed. If we switch while some of them are still running - we can get SQL_BUSY from concurrent writes in main and renderer processes. --- ts/background.ts | 2 +- ts/sql/Client.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ts/background.ts b/ts/background.ts index 5b4248571d..f16b461d4d 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -2094,7 +2094,7 @@ export async function startApp(): Promise { ); } - window.sqlInitializer.goBackToMainProcess(); + await window.sqlInitializer.goBackToMainProcess(); window.Signal.Util.setBatchingStrategy(false); const attachmentDownloadQueue = window.attachmentDownloadQueue || []; diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index 6662df2d04..f853cd943d 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -242,7 +242,15 @@ const dataInterface: ClientInterface = { export default dataInterface; -function goBackToMainProcess() { +async function goBackToMainProcess(): Promise { + window.log.info('data.goBackToMainProcess: waiting for pending queries'); + + // Let pending queries finish before we'll give write access to main process. + // We don't want to be writing from two processes at the same time! + await waitForPendingQueries(); + + window.log.info('data.goBackToMainProcess: switching to main process'); + shouldUseRendererProcess = false; }