mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Add timeouts to prevent ActiveDoc bad state during shutdown.
Summary: Add two shutdown-related timeouts. 1. One is to limit the duration of any work that happens once shutdown begins. In particular, waiting for an update to current time could block indefinitely if the data engine is unresponsive. Such awaits are now limited to 5 seconds. 2. The other is to allow documents to get shutdown for inactivity even when some work takes forever. Certain work (e.g. applying user actions) generally prevents a document from shutting down while it's pending. This prevention is now limited to 5 minutes. Shutting down a doc while something is pending may break some assumptions, and lead to errors. The timeout is long to let us assume that the work is stuck, and that errors are better than waiting forever. Other changes: - Periodic ActiveDoc work (intervals) is now started when a doc finishes loading rather than in the constructor. The difference only showed up in tests which makes the intervals much shorter. - Move timeoutReached() utility function to gutil, and use it for isLongerThan(), since they are basically identical. Also makes sure that the timer in these is cleared in all cases. - Remove duplicate waitForIt implementation (previously had a copy in both test/server and core/test/server). - Change testUtil.captureLog to pass messages to its callback, to allow asserts on messages within the callback. Test Plan: Added new unittests for the new shutdowns, including a replication of a bad state that was possible during shutdown. Reviewers: paulfitz Reviewed By: paulfitz Subscribers: paulfitz Differential Revision: https://phab.getgrist.com/D4040
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import {delay} from 'app/common/delay';
|
||||
import {BindableValue, DomElementMethod, IKnockoutReadObservable, ISubscribable, Listener, Observable,
|
||||
subscribeElem, UseCB, UseCBOwner} from 'grainjs';
|
||||
import {Observable as KoObservable} from 'knockout';
|
||||
@@ -883,19 +882,31 @@ export function isValidHex(val: string): boolean {
|
||||
return /^#([0-9A-F]{6})$/i.test(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves to true if promise is still pending after msec milliseconds have passed. Otherwise
|
||||
* returns false, including when promise is rejected.
|
||||
*/
|
||||
export async function timeoutReached(msec: number, promise: Promise<unknown>): Promise<boolean> {
|
||||
const timedOut = {};
|
||||
// Be careful to clean up the timer after ourselves, so it doesn't remain in the event loop.
|
||||
let timer: NodeJS.Timer;
|
||||
const delayPromise = new Promise<any>((resolve) => { timer = setTimeout(() => resolve(timedOut), msec); });
|
||||
try {
|
||||
const res = await Promise.race([promise, delayPromise]);
|
||||
return res == timedOut;
|
||||
} catch (err) {
|
||||
return false;
|
||||
} finally {
|
||||
clearTimeout(timer!);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a promise that resolves to true if promise takes longer than timeoutMsec to resolve. If not
|
||||
* or if promise throws returns false.
|
||||
* or if promise throws returns false. Same as timeoutReached(), with reversed order of arguments.
|
||||
*/
|
||||
export async function isLongerThan(promise: Promise<any>, timeoutMsec: number): Promise<boolean> {
|
||||
let isPending = true;
|
||||
const done = () => { isPending = false; };
|
||||
await Promise.race([
|
||||
promise.then(done, done),
|
||||
delay(timeoutMsec)
|
||||
]);
|
||||
return isPending;
|
||||
export async function isLongerThan(promise: Promise<unknown>, timeoutMsec: number): Promise<boolean> {
|
||||
return timeoutReached(timeoutMsec, promise);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user