attempt to recover from stalling the main loop

This commit is contained in:
Mononaut 2023-04-27 10:20:52 +09:00
parent 95df317f56
commit 70e19bc41c
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E
3 changed files with 20 additions and 4 deletions

View File

@ -530,7 +530,7 @@ class Blocks {
}
public async $updateBlocks() {
// warn if this run stalls the main loop for more than 2 minutes
// throw an error if this stalls the main loop for more than 2 minutes
const timer = this.startTimer();
let fastForwarded = false;
@ -714,6 +714,11 @@ class Blocks {
private updateTimerProgress(state, msg) {
state.progress = msg;
if (Date.now() - state.start > this.mainLoopTimeout) {
// abort the function if it already timed out
logger.err(`$updateBlocks attempted to resume after "${state.progress}"`);
throw new Error(`$updateBlocks attempted to resume after "${state.progress}"`);
}
}
private clearTimer(state) {

View File

@ -122,7 +122,7 @@ class Mempool {
public async $updateMempool(): Promise<void> {
logger.debug(`Updating mempool...`);
// warn if this run stalls the main loop for more than 2 minutes
// throw an error if this stalls the main loop for more than 2 minutes
const timer = this.startTimer();
const start = new Date().getTime();
@ -257,6 +257,11 @@ class Mempool {
private updateTimerProgress(state, msg) {
state.progress = msg;
if (Date.now() - state.start > this.mainLoopTimeout) {
// abort the function if it already timed out
logger.err(`$updateMempool attempted to resume after "${state.progress}"`);
throw new Error(`$updateMempool attempted to resume after "${state.progress}"`);
}
}
private clearTimer(state) {

View File

@ -179,8 +179,14 @@ class Server {
}
}
memPool.deleteExpiredTransactions();
await blocks.$updateBlocks();
await memPool.$updateMempool();
await Promise.race([
blocks.$updateBlocks(),
new Promise((_, reject) => setTimeout(() => reject(new Error('$updateBlocks timed out')), 180000))
]);
await Promise.race([
memPool.$updateMempool(),
new Promise((_, reject) => setTimeout(() => reject(new Error('$updateMempool timed out')), 180000))
]);
indexer.$run();
setTimeout(this.runMainUpdateLoop.bind(this), config.MEMPOOL.POLL_RATE_MS);