Skip to content

Commit

Permalink
Force exit workerd on exit hook
Browse files Browse the repository at this point in the history
`exitHook()` must be synchronous, and we want to make sure `workerd`
is cleaned up immediately on exit
  • Loading branch information
mrbbot committed May 15, 2023
1 parent 00f23af commit 0f7f211
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ export class Miniflare {
verbose: this.#sharedOpts.core.verbose,
};
this.#runtime = new Runtime(opts);
this.#removeRuntimeExitHook = exitHook(() => void this.#runtime?.dispose());
this.#removeRuntimeExitHook = exitHook(
() => void this.#runtime?.dispose(/* force */ true)
);

// Update config and wait for runtime to start
await this.#assembleAndUpdateConfig(/* initial */ true);
Expand Down
4 changes: 2 additions & 2 deletions packages/miniflare/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ export class Runtime {
return this.#processExitPromise;
}

dispose(): Awaitable<void> {
dispose(force = false): 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.
// See https://github.com/cloudflare/workerd/pull/244.
this.#process?.kill("SIGINT");
this.#process?.kill(force ? "SIGKILL" : "SIGINT");
return this.#processExitPromise;
}
}
Expand Down

0 comments on commit 0f7f211

Please sign in to comment.