diff --git a/lib/serve.js b/lib/serve.js index bafc543d..5f495126 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -135,12 +135,20 @@ function createHandler(dir, static, timeout) { { clientContext: buildClientContext(request.headers) || {} }, callback ); + + var invocationTimeoutRef = null + Promise.race([ promiseCallback(promise, callback), - setTimeout(function() { - handleInvocationTimeout(response, timeout) - }, timeout * 1000) - ]) + new Promise(function(resolve) { + invocationTimeoutRef = setTimeout(function() { + handleInvocationTimeout(response, timeout) + resolve() + }, timeout * 1000) + }) + ]).finally(() => { + clearTimeout(invocationTimeoutRef) + }) }; }