Async callbacks in Meteor.setTimeout/setInterval/defer crash the server when they throw
Hi Meteor team
On the server, when an async callback passed to Meteor.setTimeout, Meteor.setInterval or Meteor.defer throws, the whole server exits. A sync callback that throws just gets logged as "Exception in setTimeout callback".
Repro (in server code; meteor shell catches it in its REPL)
Meteor.setTimeout(async () => {
throw new Error("boom");
}, 0);Result: the rejection goes unhandled and the server exits with code 1. Expected: logged like a sync error, server keeps running.
Cause
In packages/meteor/timers.js, the wrapper from withoutInvocation drops the callback's return value:
return function () {
CurrentInvocation.withValue(null, f);
};So Meteor.bindEnvironment never sees the promise and never attaches its .catch. The wrapper only exists when the ddp package is loaded, so this hits basically every app.
Fix
One return:
return function () {
return CurrentInvocation.withValue(null, f);
};I checked it locally: with the return, the async error is logged like a sync one and the server keeps running.
Why it matters
accounts-base runs its token cleanup this way: Meteor.setInterval(async () => { await accounts._expireTokens(); ... }). In #13108 the in-flight write was traced to _expireTokens. When that write fails, e.g. with PoolClearedOnNetworkError after a laptop wakes up, this bug turns it into a crash.
Source: meteor/meteor