From 9286bd6aa0dd885cae386e208a0ca3db25dd74af Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Thu, 17 Sep 2020 17:00:41 -0700 Subject: [PATCH] Fix unhandled promise rejections --- lib/cwmp.ts | 9 +++++++-- lib/ui.ts | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/cwmp.ts b/lib/cwmp.ts index 0e9584b..b28e046 100644 --- a/lib/cwmp.ts +++ b/lib/cwmp.ts @@ -1211,12 +1211,17 @@ export function listener( }) .catch((err) => { currentSessions.delete(httpResponse.connection); - httpResponse.writeHead(500, { Connection: "close" }); - httpResponse.end(`${err.name}: ${err.message}`); stats.concurrentRequests -= 1; setTimeout(() => { throw err; }); + try { + httpResponse.connection.unref(); + httpResponse.writeHead(500, { Connection: "close" }); + httpResponse.end(`${err.name}: ${err.message}`); + } catch (err) { + // Ignore + } }); } diff --git a/lib/ui.ts b/lib/ui.ts index 759454c..0e2192c 100644 --- a/lib/ui.ts +++ b/lib/ui.ts @@ -60,7 +60,9 @@ const getAuthorizer = memoize( ); koa.on("error", async (err) => { - throw err; + setTimeout(() => { + throw err; + }); }); koa.use(async (ctx, next) => {