Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Miniflare 3] Make process exits/server stops more aggressive to prevent test hangs #590

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,7 @@ export class Miniflare {
verbose: this.#sharedOpts.core.verbose,
};
this.#runtime = new Runtime(opts);
this.#removeRuntimeExitHook = exitHook(
() => void this.#runtime?.dispose(/* force */ true)
);
this.#removeRuntimeExitHook = exitHook(() => void this.#runtime?.dispose());

// Update config and wait for runtime to start
await this.#assembleAndUpdateConfig(/* initial */ true);
Expand Down Expand Up @@ -673,7 +671,10 @@ export class Miniflare {
hostname?: string
): Promise<StoppableServer> {
return new Promise((resolve) => {
const server = stoppable(http.createServer(this.#handleLoopback));
const server = stoppable(
http.createServer(this.#handleLoopback),
/* grace */ 0
);
server.on("upgrade", this.#handleLoopbackUpgrade);
server.listen(port as any, hostname, () => resolve(server));
});
Expand Down
10 changes: 3 additions & 7 deletions packages/miniflare/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,14 @@ export class Runtime {
return waitForPort(SOCKET_ENTRY, controlPipe, options);
}

get exitPromise(): Promise<void> | undefined {
return this.#processExitPromise;
}

dispose(force = false): Awaitable<void> {
dispose(): Awaitable<void> {
// `kill()` uses `SIGTERM` by default. In `workerd`, this waits for HTTP
// connections to close before exiting. Notably, Chrome sometimes keeps
// connections open for about 10s, blocking exit. We'd like `dispose()`/
// `setOptions()` to immediately terminate the existing process.
// Therefore, use `SIGINT` which force closes all connections.
// Therefore, use `SIGKILL` which force closes all connections.
// See https://github.com/cloudflare/workerd/pull/244.
this.#process?.kill(force ? "SIGKILL" : "SIGINT");
this.#process?.kill("SIGKILL");
return this.#processExitPromise;
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/miniflare/test/test-shared/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import http from "http";
import { AddressInfo } from "net";
import { URL } from "url";
import { ExecutionContext } from "ava";
import stoppable from "stoppable";
import NodeWebSocket, { WebSocketServer } from "ws";

export async function useServer(
Expand All @@ -10,15 +11,19 @@ export async function useServer(
webSocketListener?: (socket: NodeWebSocket, req: http.IncomingMessage) => void
): Promise<{ http: URL; ws: URL }> {
return new Promise((resolve) => {
const server = http.createServer(listener);
const server = stoppable(http.createServer(listener), /* grace */ 0);
// Only setup web socket server if listener provided
if (webSocketListener) {
const wss = new WebSocketServer({ server });
wss.on("connection", webSocketListener);
}
// 0 binds to random unused port
server.listen(0, () => {
t.teardown(() => server.close());
t.teardown(() => {
return new Promise((resolve, reject) =>
server.stop((err) => (err ? reject(err) : resolve()))
);
});
const port = (server.address() as AddressInfo).port;
resolve({
http: new URL(`http://localhost:${port}`),
Expand Down