From c539b8ee46baa69911c53ef852f8a26474cf79ab Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Sun, 19 Nov 2023 12:02:48 +0530 Subject: [PATCH] watchdog: Handle exception in `callback`. Earlier, in the 'check_for_unsuspend' function, we didn't handle the exception, if any, during callback execution, resulting in 'watchdog_time' not always being updated. This commit handles the exception using the try/catch block. Fixes #27723. --- web/src/watchdog.ts | 14 +++++++++++++- web/tests/watchdog.test.js | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/web/src/watchdog.ts b/web/src/watchdog.ts index eeff824ba1..07082fa0fa 100644 --- a/web/src/watchdog.ts +++ b/web/src/watchdog.ts @@ -1,3 +1,5 @@ +import * as blueslip from "./blueslip"; + const unsuspend_callbacks: (() => void)[] = []; let watchdog_time = Date.now(); @@ -35,7 +37,17 @@ export function check_for_unsuspend(): void { // Our app's JS wasn't running, which probably means the machine was // asleep. for (const callback of unsuspend_callbacks) { - callback(); + try { + callback(); + } catch (error) { + blueslip.error( + `Error while executing callback '${ + callback.name || "Anonymous function" + }' from unsuspend_callbacks.`, + undefined, + error, + ); + } } } watchdog_time = new_time; diff --git a/web/tests/watchdog.test.js b/web/tests/watchdog.test.js index 662d26127c..4257bf6d56 100644 --- a/web/tests/watchdog.test.js +++ b/web/tests/watchdog.test.js @@ -6,6 +6,7 @@ const MockDate = require("mockdate"); const {set_global, zrequire} = require("./lib/namespace"); const {run_test} = require("./lib/test"); +const blueslip = require("./lib/zblueslip"); let time = 0; let checker; @@ -58,6 +59,20 @@ run_test("basics", () => { advance_secs(21); watchdog.check_for_unsuspend(); assert.equal(num_times_called_back, 1); + + // Error while executing callback + num_times_called_back = 0; + advance_secs(21); + watchdog.on_unsuspend(() => { + num_times_called_back += 1; + throw new Error("Some error while executing"); + }); + blueslip.expect( + "error", + `Error while executing callback 'Anonymous function' from unsuspend_callbacks.`, + ); + watchdog.check_for_unsuspend(); + assert.equal(num_times_called_back, 2); }); run_test("suspect_offline", () => {