mirror of https://github.com/zulip/zulip.git
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.
This commit is contained in:
parent
295b4fc337
commit
c539b8ee46
|
@ -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;
|
||||
|
|
|
@ -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", () => {
|
||||
|
|
Loading…
Reference in New Issue