fix(deno): guard fire-and-forget app.upgrade against fatal unhandled rejection
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 file changed
+11
-3
+11
-3
@@ -99,9 +99,17 @@ export function serveDeno(app: App, options?: DenoServeOptions): DenoHttpServer
|
|||||||
return Deno.serve(options ?? {}, (request, info) => {
|
return Deno.serve(options ?? {}, (request, info) => {
|
||||||
if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') {
|
if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') {
|
||||||
const { socket, response } = Deno.upgradeWebSocket(request);
|
const { socket, response } = Deno.upgradeWebSocket(request);
|
||||||
// fire-and-forget: the graph runs after we return the upgrade response.
|
// Fire-and-forget: the upgrade response must be returned synchronously while
|
||||||
// runWsConnection swallows its own errors (closeOnError), so nothing escapes.
|
// the graph runs. app.upgrade can reject BEFORE runWsConnection's own try/catch
|
||||||
void app.upgrade(toWsRequest(request, info), denoSocket(socket));
|
// (mesh-before-listen throw, or a provider boot failure), so guard the call here —
|
||||||
|
// an uncaught rejection would be fatal on Deno and crash the whole server.
|
||||||
|
void app.upgrade(toWsRequest(request, info), denoSocket(socket)).catch(() => {
|
||||||
|
try {
|
||||||
|
socket.close(1011, 'internal error');
|
||||||
|
} catch {
|
||||||
|
/* already closed */
|
||||||
|
}
|
||||||
|
});
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user