Skip to content

fix(dev): pass the shutdown timeout to the dev worker - #5137

Open
stantheman0128 wants to merge 3 commits into
nitrojs:mainfrom
stantheman0128:fix/5133-dev-shutdown-timeout
Open

stantheman0128 wants to merge 3 commits into
nitrojs:mainfrom
stantheman0128:fix/5133-dev-shutdown-timeout

Conversation

@stantheman0128

Copy link
Copy Markdown

🔗 Linked issue

Resolves #5133

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

shutdownRunner posted { event: "shutdown" } and then waited 5 seconds for { event: "exit" }. env-runner 0.3.11 only bounds server.close() when that message includes a numeric timeout. Without it, an open response or WebSocket keeps server.close() pending, the host timer force-closes the worker, and ipc.onClose never runs. The warning is:

[nitro]  WARN  Dev worker did not shut down in time, force closing it...

The shutdown message now includes timeout: 5000, the same budget the host already uses. env-runner then stops waiting on the socket after 2.5 seconds and still runs ipc.onClose.

test/unit/dev-shutdown.test.ts failed before the fix with expected [ { event: 'shutdown' } ] and passed after it. test/unit/close-hooks.test.ts also passed.

Written with AI assistance (Cursor, Grok).

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

The dev worker only bounds an open socket when the shutdown message includes a timeout.
An open socket keeps server.close pending unless the shutdown message includes a timeout. The worker then runs close hooks before the host gives up.
@stantheman0128
stantheman0128 requested a review from pi0 as a code owner November 16, 2026 04:41
@vercel

vercel Bot commented Nov 16, 2026

Copy link
Copy Markdown

@stantheman0128 is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

shutdownRunner now includes the 5-second timeout in the shutdown message sent to the dev worker. A unit test checks the timeout value and simulates an exit event to complete the pending shutdown operation.

Changes: Dev worker shutdown

Layer / File(s) Summary
Propagate and test shutdown timeout
src/dev/shutdown.ts, test/unit/dev-shutdown.test.ts
The shutdown message now includes the 5-second timeout. The unit test checks for a 5000 ms timeout and notifies registered listeners with an exit event.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: bug, dev

Merge Risk: Minimal · up to d3a07

The dev worker now receives the shutdown timeout, so it can run close hooks before the host force-closes it. The change is small, covered by a unit test, and easy to revert, so there is no actionable merge risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(dev): pass the shutdown timeout to the dev worker" follows the conventional commits format with a valid type (fix) and scope (dev). It matches the main change: the shutdown message to t…
Description check Passed The description explains the bug, the root cause in env-runner 0.3.11, the fix that adds timeout: 5000 to the shutdown message, and the tests run before and after the change. It is related to the chan…
Linked Issues check Passed Issue #5133 asks that the dev worker shutdown message carry the existing shutdown timeout, so the worker bounds its server close and runs close hooks before the host force-closes it. The whole-PR diff…
Out of Scope Changes check Passed The whole-PR diff has two source-level effects. The first is the one-line timeout addition in src/dev/shutdown.ts. The second is the export of SHUTDOWN_TIMEOUT, which the new test needs to reference t…
Full details: Title check

Explanation

The title "fix(dev): pass the shutdown timeout to the dev worker" follows the conventional commits format with a valid type (fix) and scope (dev). It matches the main change: the shutdown message to the dev worker now includes the timeout.

Full details: Description check

Explanation

The description explains the bug, the root cause in env-runner 0.3.11, the fix that adds timeout: 5000 to the shutdown message, and the tests run before and after the change. It is related to the changeset.

Full details: Linked Issues check

Explanation

Issue #5133 asks that the dev worker shutdown message carry the existing shutdown timeout, so the worker bounds its server close and runs close hooks before the host force-closes it. The whole-PR diff (merge base to head) in src/dev/shutdown.ts sends manager.sendMessage({ event: "shutdown", timeout: SHUTDOWN_TIMEOUT }), which implements the first suggested fix. SHUTDOWN_TIMEOUT is now exported (value 5000), so the host timer and the message share one budget. The change since the previous review only adds the export and test coverage; the functional change is unchanged. The test file test/unit/dev-shutdown.test.ts covers the shutdown message. The worker-side handling of timeout lives in env-runner, outside this repository. This conclusion relies on the issue and PR description for that behavior. The issue's optional alternative (reordering close hooks) is not part of this PR's stated objectives.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff has two source-level effects. The first is the one-line timeout addition in src/dev/shutdown.ts. The second is the export of SHUTDOWN_TIMEOUT, which the new test needs to reference the same constant. Both serve issue #5133. The test file adds coverage for the same change. No unrelated source changes were found in the reviewed evidence.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 48d8cf1b-18ef-4585-a590-5ad0013c826c
Commits

Reviewing files that changed from the base of the PR and between 7e8c6bb and d3a0737.

Files selected for processing: 2

  • src/dev/shutdown.ts
  • test/unit/dev-shutdown.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@Neekoras Neekoras 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.

I traced the env-runner side, since the fix depends on it.

In env-runner 0.3.11, every process-based runner (node-worker, node-process, bun-process, deno-process) handles { event: "shutdown" } by calling shutdownWorker(server, entry, message.timeout). shutdownWorker races server.close() against timeout / 2 only when timeout is a number, and awaits it with no bound otherwise, then runs ipc.onClose. So without the field an open WebSocket keeps close() pending until the host's 5 s timer force-closes the worker, and with timeout: 5000 the worker stops waiting at 2.5 s and still runs onClose. That matches the 5,035 ms and 2,524 ms in #5133. The netlify and vercel runners have no shutdown handler and miniflare shuts down through its own dispose(), so they are unaffected.

Close hooks get the remaining 2.5 s of the host's budget, and env-runner is ^0.3.11 in package.json, so the field is always understood. src/dev/shutdown.ts is also the only place that sends the shutdown message, so nothing else needs the same change.

One optional suggestion: the test hardcodes 5000. Exporting SHUTDOWN_TIMEOUT and asserting against it would keep the test in step if the budget ever changes.

Export SHUTDOWN_TIMEOUT from src/dev/shutdown.ts so the unit test stays in step with the value the dev server sends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stantheman0128

Copy link
Copy Markdown
Author

Thanks for tracing the env-runner side.

On the hardcoded 5000: src/dev/shutdown.ts now exports SHUTDOWN_TIMEOUT, and test/unit/dev-shutdown.test.ts imports it and asserts the shutdown message carries { event: "shutdown", timeout: SHUTDOWN_TIMEOUT }. The value is unchanged. New head is d3a0737.

The constant stays internal. src/builder.ts re-exports only createDevServer and the NitroDevServer type from src/dev/server.ts, and src/dev/shutdown.ts is not a build entry, so nothing new reaches the package exports.

What I checked: I couldn't run vitest, lint or typecheck locally for this commit, since I don't have dependencies installed on this machine. I ran a small Node 24 script against src/dev/shutdown.ts that imports SHUTDOWN_TIMEOUT and shutdownRunner, checks the sent message matches { event: "shutdown", timeout: SHUTDOWN_TIMEOUT } and that the listener is removed after the exit event, and it passed. The CI workflows on the new head are waiting for approval, and they will cover the rest.

Prepared with Claude Code.

This branch has not been deployed

No deployments
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.

vite dev: Dev worker shutdown sends no timeout, opened WebSockets stays on until the 5 s timeout, and close hooks never run

2 participants