Skip to content

Commit

Permalink
fix: http_server_native when closing with signal abort event (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored Feb 25, 2024
1 parent a3f0076 commit ff1dfe8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions http_server_native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -56,7 +57,7 @@ Deno.test({
console.error(e);
unreachable();
} finally {
await server.close();
abortController.abort();
}
},
});
Expand Down
2 changes: 1 addition & 1 deletion http_server_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export class Server<AS extends State = Record<string, any>>
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<Listener>();
this.#stream = new ReadableStream<NativeRequest>({
Expand All @@ -94,6 +93,7 @@ export class Server<AS extends State = Record<string, any>>
},
});

signal?.addEventListener("abort", () => this.close(), { once: true });
return promise;
}

Expand Down

0 comments on commit ff1dfe8

Please sign in to comment.