From 09e56e36335a8a2458ce000d8cd8457e543d81b3 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:00:00 -0800 Subject: [PATCH] fix: http_server_native when closing with signal abort event --- http_server_native.test.ts | 5 +++-- http_server_native.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/http_server_native.test.ts b/http_server_native.test.ts index 96ca4ac9..18627279 100644 --- a/http_server_native.test.ts +++ b/http_server_native.test.ts @@ -35,8 +35,9 @@ Deno.test({ name: "HttpServer closes gracefully after serving requests", ignore: isNode(), async fn() { + const abortController = new AbortController(); const app = new Application(); - const listenOptions = { port: 4505 }; + const listenOptions = { port: 4505, signal: abortController.signal}; const server = new Server(app, listenOptions); server.listen(); @@ -56,7 +57,7 @@ Deno.test({ console.error(e); unreachable(); } finally { - await server.close(); + abortController.abort(); } }, }); diff --git a/http_server_native.ts b/http_server_native.ts index 262287a2..bef559c7 100644 --- a/http_server_native.ts +++ b/http_server_native.ts @@ -71,7 +71,6 @@ export class Server> throw new Error("Server already listening."); } const { signal } = this.#options; - signal?.addEventListener("abort", () => this.close(), { once: true }); const { onListen, ...options } = this.#options; const { promise, resolve } = createPromiseWithResolvers(); this.#stream = new ReadableStream({ @@ -93,7 +92,8 @@ export class Server> }); }, }); - + + signal?.addEventListener("abort", () => this.close(), { once: true }); return promise; }