From 8279059624b0c137bc9bdb4a7527a4b3d91aaae4 Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Mon, 28 Aug 2023 15:27:14 -0400 Subject: [PATCH] fix(dev): kill app server when `remix dev` terminates --- .changeset/olive-chefs-jam.md | 5 +++++ packages/remix-dev/cli/commands.ts | 5 ++++- packages/remix-dev/devServer_unstable/index.ts | 11 +++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 .changeset/olive-chefs-jam.md diff --git a/.changeset/olive-chefs-jam.md b/.changeset/olive-chefs-jam.md new file mode 100644 index 00000000000..168d26cc22f --- /dev/null +++ b/.changeset/olive-chefs-jam.md @@ -0,0 +1,5 @@ +--- +"@remix-run/dev": patch +--- + +kill app server when remix dev terminates diff --git a/packages/remix-dev/cli/commands.ts b/packages/remix-dev/cli/commands.ts index f97bd9702c8..18b7c19b603 100644 --- a/packages/remix-dev/cli/commands.ts +++ b/packages/remix-dev/cli/commands.ts @@ -169,7 +169,10 @@ export async function dev( let config = await readConfig(remixRoot); let resolved = await resolveDevServe(config, flags); - await devServer_unstable.serve(config, resolved); + devServer_unstable.serve(config, resolved); + + // keep `remix dev` alive by waiting indefinitely + await new Promise(() => {}); } let clientEntries = ["entry.client.tsx", "entry.client.js", "entry.client.jsx"]; diff --git a/packages/remix-dev/devServer_unstable/index.ts b/packages/remix-dev/devServer_unstable/index.ts index e54047b43ef..ef3a3fb370c 100644 --- a/packages/remix-dev/devServer_unstable/index.ts +++ b/packages/remix-dev/devServer_unstable/index.ts @@ -7,6 +7,7 @@ import prettyMs from "pretty-ms"; import execa from "execa"; import express from "express"; import pc from "picocolors"; +import exitHook from "exit-hook"; import * as Channel from "../channel"; import { type Manifest } from "../manifest"; @@ -22,7 +23,7 @@ import type { Result } from "../result"; import { err, ok } from "../result"; import invariant from "../invariant"; import { logger } from "../tux"; -import { kill, killtree } from "./proc"; +import { killtree } from "./proc"; let detectBin = async (): Promise => { let pkgManager = detectPackageManager() ?? "npm"; @@ -267,12 +268,14 @@ export let serve = async ( server.listen(options.port); - return new Promise(() => {}).finally(async () => { - state.appServer?.pid && (await kill(state.appServer.pid)); + let cleanup = async () => { + state.appServer?.kill(); websocket.close(); server.close(); await dispose(); - }); + }; + exitHook(cleanup); + return cleanup; }; let clean = (config: RemixConfig) => {