Ensure that waitForAll functions catch and log thrown errors

This commit is contained in:
Scott Nonnenberg 2022-04-11 10:53:57 -07:00 committed by GitHub
parent 45fcf827dd
commit 9e54f55c22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 5 deletions

View file

@ -1693,7 +1693,7 @@ async function requestShutdown() {
}
getLogger().info('requestShutdown: Requesting close of mainWindow...');
const request = new Promise<void>((resolveFn, reject) => {
const request = new Promise<void>(resolveFn => {
let timeout: NodeJS.Timeout | undefined;
if (!mainWindow) {
@ -1705,7 +1705,10 @@ async function requestShutdown() {
getLogger().info('requestShutdown: Response received');
if (error) {
return reject(error);
getLogger().error(
'requestShutdown: got error, still shutting down.',
error
);
}
clearTimeoutIfNecessary(timeout);

View file

@ -648,6 +648,7 @@ export async function startApp(): Promise<void> {
server !== undefined,
'WebAPI should be initialized together with MessageReceiver'
);
log.info('background/shutdown: shutting down messageReceiver');
server.unregisterRequestHandler(messageReceiver);
messageReceiver.stopProcessing();
await window.waitForAllBatchers();

View file

@ -673,6 +673,8 @@ function keysFromBytes(keys: Array<string>, data: any) {
// Top-level calls
async function shutdown() {
log.info('Client.shutdown');
// Stop accepting new SQL jobs, flush outstanding queue
await _shutdown();

View file

@ -352,6 +352,7 @@ export default class MessageReceiver
}
public stopProcessing(): void {
log.info('MessageReceiver.stopProcessing');
this.stoppingProcessing = true;
}

View file

@ -5,6 +5,7 @@ import PQueue from 'p-queue';
import { sleep } from './sleep';
import * as log from '../logging/log';
import * as Errors from '../types/errors';
import { clearTimeoutIfNecessary } from './clearTimeoutIfNecessary';
declare global {
@ -20,7 +21,15 @@ declare global {
window.batchers = [];
window.waitForAllBatchers = async () => {
await Promise.all(window.batchers.map(item => item.flushAndWait()));
log.info('batcher#waitForAllBatchers');
try {
await Promise.all(window.batchers.map(item => item.flushAndWait()));
} catch (error) {
log.error(
'waitForAllBatchers: error flushing all',
Errors.toLogFormat(error)
);
}
};
export type BatcherOptionsType<ItemType> = {

View file

@ -5,6 +5,7 @@ import PQueue from 'p-queue';
import { sleep } from './sleep';
import * as log from '../logging/log';
import * as Errors from '../types/errors';
import { clearTimeoutIfNecessary } from './clearTimeoutIfNecessary';
declare global {
@ -22,12 +23,26 @@ window.waitBatchers = [];
window.flushAllWaitBatchers = async () => {
log.info('waitBatcher#flushAllWaitBatchers');
await Promise.all(window.waitBatchers.map(item => item.flushAndWait()));
try {
await Promise.all(window.waitBatchers.map(item => item.flushAndWait()));
} catch (error) {
log.error(
'flushAllWaitBatchers: Error flushing all',
Errors.toLogFormat(error)
);
}
};
window.waitForAllWaitBatchers = async () => {
log.info('waitBatcher#waitForAllWaitBatchers');
await Promise.all(window.waitBatchers.map(item => item.onIdle()));
try {
await Promise.all(window.waitBatchers.map(item => item.onIdle()));
} catch (error) {
log.error(
'waitForAllWaitBatchers: Error waiting for all',
Errors.toLogFormat(error)
);
}
};
type ItemHolderType<ItemType> = {