Skip to content

fix(dev): stop the app's services before exiting, so Ctrl-C under npm frees their ports - #424

Merged
wmadden merged 3 commits into
mainfrom
claude/dev-ctrl-c-stops-services-abcbbd
Nov 10, 2026
Merged

wmadden merged 3 commits into
mainfrom
claude/dev-ctrl-c-stops-services-abcbbd

Conversation

@wmadden-electric

@wmadden-electric wmadden-electric commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Run the app that create-prisma makes, press Ctrl-C, then check its port:

npm run dev:composer          # wait for "dev: ready" and "my-app.app: http://localhost:3000"
^C                            # prints "▸ Stopping the app's services — emulators and data stay up"
curl http://localhost:3000    # before this PR: the app still answers
                              # after this PR: connection refused

The decision: while prisma dev runs in the foreground, it registers a process exit hook that stops the app's services. That hook runs even when prisma dev is forced to exit. Today, a forced exit cuts the stop short.

What prisma dev promises

prisma dev runs each of the app's services and databases on local emulators: long-lived background programs on this machine that stand in for Prisma Cloud. The emulators outlive prisma dev, but the app's processes should not. prisma dev --help says: "Ending prisma dev stops its services and database servers; data and ports are kept." When Ctrl-C arrives, prisma dev sends the emulators a stop request and waits for it to finish.

Why the stop was cut short

Pressing Ctrl-C sends SIGINT to every process in the terminal's foreground process group, prisma included. When prisma runs under npm run or npx, npm is in that group too, and npm forwards the SIGINT it received to its child. So prisma gets two SIGINTs about 3 ms apart.

prisma is built on the Prisma CLI engine (@prisma/cli-engine, from prisma/prisma-cli). Its rule is that the first signal asks the command to clean up, and a second signal means "exit now": it calls process.exit(130). Under npm, that second signal arrives before the stop request has reached the emulators. prisma dev prints the stopping step and exits 130, and the app's process (bun bootstrap.js, a child of the compute emulator) keeps its port. In the original report it was still listening 50 seconds later. Sending SIGINT to prisma alone works, because only one signal arrives.

The getting-started nightly found this (prisma/getting-started-eval#32). Its PostgreSQL quickstart script stops npm run dev:composer with Ctrl-C, then starts a plain Node server on port 3000, which failed with EADDRINUSE. Seen with prisma 8.0.0-rc.28, @prisma/composer 0.35.0, macOS and Node 26.8.

How the exit hook stops the app

A process.on('exit') hook runs when process.exit() is called, but it cannot wait for anything asynchronous. The normal stop is a series of HTTP requests, so the hook runs a small program instead, stop-app-main, with spawnSync. That program calls the same stopAppInstances function as the normal stop. prisma exits only after the services and database servers have stopped, so the port is free by the time the shell prompt returns.

  • execute-dev.ts registers the hook once a foreground session is ready. It removes the hook when the normal stop finishes, so an ordinary Ctrl-C stops the app once. A --detach session registers no hook, because it is meant to leave the app running.
  • The interface between the dev command and the target that runs the app locally (LocalTargetAttachment in app-config.ts) gains an optional stopAppBeforeExit(). The Prisma Cloud target implements it in attach.ts.
  • stop-app-main.ts is the program. @prisma/composer-prisma-cloud publishes it as ./local-target/stop-app-main, next to the emulator programs, so an installed app can find it.
  • The hook waits at most 30 seconds, so a stuck emulator cannot stop prisma from exiting.

Testing performed

  • New local-dev-interrupt.integration.ts does what the terminal and npm do:

    • It runs the installed prisma dev against the local-dev fixture in its own process group.
    • It sends SIGINT to the group, waits until the stopping step starts, then sends a second SIGINT to prisma.
    • It then checks for exit 130, that every service process is gone, and that every service port is free.

    Without the hook, the test fails with "service process 9118 is gone". With the hook it passes. Its log shows the stopping step starting but never finishing, so the exit hook did the stop.

  • These passed: pnpm build, typecheck and unit tests for core, cli, local-target, composer-cli and prisma-cloud, pnpm lint:deps, pnpm lint:casts and biome. The one exception is the @internal/prisma-cloud tests that start Postgres. They failed on my machine with shmget: No space left on device, which means the machine had run out of shared memory. CI runs them.

  • I did not rerun the create-prisma app against this branch. The integration test uses the same installed prisma and sends the same two signals.

Alternatives considered

  • Change the engine so a quick second signal does not force an exit. The engine lives in prisma/prisma-cli. On a loaded machine, npm's forwarded signal can arrive as late as a quick second press by a person, so a time limit cannot tell them apart. And a person who really pressed Ctrl-C twice would still be left with a running app.
  • Have the emulators stop an app when its prisma dev process dies. This would also cover kill -9. But the emulators would then need to know which session owns each app. The local-dev design says the session owns no processes and that a killed session leaves the app for prisma dev stop. That is a design change, not a bug fix.
  • Start the stop in a background process and exit at once. The next command could start before the port is free, which is the failure the nightly hit.

Linked issue

n/a. Found by prisma/getting-started-eval#32 and recorded in that repository's README under "Findings to pass on".

Checklist

  • All commits are signed off (git commit -s) per the DCO.
  • I read CONTRIBUTING.md and the change is scoped to one logical concern.
  • The PR title is a conventional commit.
  • Tests are updated.

Agent: neptune-46

🤖 Generated with Claude Code

Ctrl-C in a terminal sends SIGINT to the whole foreground process group.
Under `npm run` or `npx`, npm also forwards the SIGINT it receives to
`prisma`, so `prisma dev` gets a second SIGINT a few milliseconds after
the first. The CLI engine treats a second signal as a request to exit at
once, so `prisma dev` exited 130 before its stop request reached the
emulators, and the app's services kept their ports.

While a foreground session is live, an exit hook now stops the app
before the process exits. An exit hook cannot await, so the local target
runs a small program, stop-app-main, with spawnSync; it does what
stopApp() does. The hook is removed once stop() finishes, so a normal
Ctrl-C stops the app once.

Found by the getting-started nightly (prisma/getting-started-eval#32).

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@prisma-gizmo

prisma-gizmo Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Caution

Gizmo failed to process the last GitHub event for this pull request: This suspended workflow run was already resumed by another caller. Workflow "guardrail-lifecycle" run "1cb3dcb6-87f4-4eef-aa7f-90e7934884dd" moved from "suspended" to "running" before this resume could claim it. Only one resume() call may continue a given suspension; re-read the run state before resuming again.

Open findings: none

Change walkthrough

Under npm run/npx, Ctrl-C delivers SIGINT to prisma dev twice (terminal + npm's forward), and the CLI engine force-exits on the second signal before the in-process stop can finish, leaving the app's services listening. This PR closes that gap: while a foreground session is live, prisma dev registers a process.on('exit') hook that stops the app synchronously, so the port is free when the process exits.

Core interface (app-config.ts) — LocalTargetAttachment gains an optional stopAppBeforeExit(). Keeping it optional means implementors without an exit-safe stop (today only the local target exists) simply fall back to the old behavior.

CLI (execute-dev.ts) — the hook is registered only after the foreground session is fully handed over (the detached early-return path never registers it), iterates all attachments with optional calls, and is removed both when stop() completes and in the catch path. Since 'exit' handlers must be synchronous, the stop runs via spawnSync rather than the awaited stopApp().

Local target — stopAppInstancesBeforeExit (emulator-instances.ts) spawns a small standalone program, stop-app-main, which calls the same stopAppInstances the normal stop path uses, capped at 30 s so a stuck emulator cannot block exit forever. Because the stop is a child process, it works inside an exit handler without the event loop. The program is shipped like the existing daemon programs: a src/exports/ shim per the exports-entrypoints rule, a tsdown entry, and a re-emit entry in @prisma/composer-prisma-cloud's multi-pass build so the bare-specifier resolvePackageEntry used in attach.ts resolves in a published install.

Tests — local-dev-interrupt.integration.ts runs the installed prisma dev in its own process group against the local-dev fixture, reproduces npm's double-SIGINT (group signal, then a second signal to prisma once the stop step starts), and asserts exit 130, dead service pids, and freed ports. It is wired into the test/integration script chain after the other local-dev integration scripts.

Scope — the guides already document the restored contract (Ctrl-C stops the app's services; prisma dev stop covers killed sessions), so no documentation changes accompany the fix.

@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: d1955d90-2fa8-44fc-99ad-cd5bed29cae7
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@pkg-pr-new

pkg-pr-new Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@prisma/composer@424
npm i https://pkg.pr.new/@prisma/composer-cli@424
npm i https://pkg.pr.new/@prisma/composer-prisma-cloud@424

commit: 6ec4fb0

@prisma-gizmo prisma-gizmo Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New findings: 🟡 2 minor · trace

@wmadden
wmadden enabled auto-merge (squash) November 10, 2026 06:48
@wmadden-electric wmadden-electric changed the title fix(dev): Ctrl-C stops the app's services under npm run and npx fix(dev): stop the app's services before exiting, so Ctrl-C under npm frees their ports Nov 10, 2026
…over session in the test

The stop that runs as `prisma dev` exits threw away the result of the
stop-app-main program, so a timeout left the app running with no message.
It now prints "The app could not be stopped: <why>. Run `prisma dev stop`."
when the program fails, times out, or exits non-zero.

The interrupt integration test now ends a `prisma dev` that a failure left
running before it tears down the emulators, so a hung session cannot hold
the fixture's ports for the next run.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden
wmadden merged commit 2025f0b into main Nov 10, 2026
24 checks passed
@wmadden
wmadden deleted the claude/dev-ctrl-c-stops-services-abcbbd branch November 10, 2026 07:06
wmadden-electric added a commit that referenced this pull request Nov 16, 2026
Brings in #424 (prisma dev stops services before exiting), #426 (deploy copies no local value to platform variables), #429 and the v0.39.0 release.

#426 carried the deploy preflight's findings to the Alchemy child in PRISMA_COMPOSER_PREFLIGHT_FINDINGS. Deploy now runs in process, so the findings reach lower() as a value, LowerOptions.findings, and that environment variable and its reader are gone. prisma deploy repeats the list of platform variables with no value at the end of its human output, as the child's report did on main.

This branch's ADR-0064 becomes ADR-0065, since main owns ADR-0064.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants