Skip to content

fix(dev): await worker shutdown before replacing it - #4506

Open
danielroe wants to merge 2 commits into
mainfrom
fix/dev-close-await
Open

fix(dev): await worker shutdown before replacing it#4506
danielroe wants to merge 2 commits into
mainfrom
fix/dev-close-await

Conversation

@danielroe

@danielroe danielroe commented Aug 4, 2026

Copy link
Copy Markdown
Member

🔗 Linked issue

nuxt/nuxt#32928

related: #4088
partly resolve #2735

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 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

we currently don't await when shutting down a worker, meaning tasks or plugins shutdown can overlap with new startup (e.g. database connection). we've implemented waiting (+ timeout) in nuxt/cli for the nuxt dev server (nuxt/cli#1424) but I thought we should probably do the same for nitro workers.

let me know if you don't think this is desired behaviour, or if you want to move it to env-runner instead 🙏

📝 Checklist

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

@danielroe danielroe self-assigned this Aug 4, 2026
@danielroe
danielroe requested a review from pi0 as a code owner August 4, 2026 09:56
@vercel

vercel Bot commented Aug 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nitro.build Ready Ready Preview Aug 8, 2026 7:01am

Request Review

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3f5eb6c3-9d04-4bdd-9764-b79e5866e918

📥 Commits

Reviewing files that changed from the base of the PR and between 41d7d70 and d2b1f9a.

📒 Files selected for processing (1)
  • src/dev/server.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/dev/server.ts

📝 Walkthrough

Walkthrough

The dev server coordinates worker shutdown during close and reload. It waits for worker exit, handles timeout and send failures, and suppresses recovery behavior for intentional worker closure.

Changes

Worker shutdown coordination

Layer / File(s) Summary
Worker shutdown protocol
src/dev/server.ts
The server tracks intentional shutdown state. #shutdownWorker() sends a shutdown message, waits for an exit event, handles send failures, and force-completes after five seconds.
Close and reload integration
src/dev/server.ts
close() and reload shut down the existing worker before releasing resources or starting a replacement runner. Intentional worker closure skips error recording and retry handling. Reload requests are ignored after closing starts.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title follows Conventional Commits and accurately describes awaiting worker shutdown before replacement.
Description check ✅ Passed The description explains the worker shutdown bug, the timeout-based fix, and the related issue.
Linked Issues check ✅ Passed The changes await worker close hooks, protect shutdown work from forced closure, and add timeout handling for incomplete shutdowns [#2735].
Out of Scope Changes check ✅ Passed The changes remain within the scope of reliable asynchronous worker shutdown and replacement.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dev-close-await

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

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Aug 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4506

commit: d2b1f9a

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/dev/server.ts (1)

211-243: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Extract worker lifecycle coordination.

Move the shutdown state and shutdown protocol into an internal module such as src/dev/_worker-lifecycle.ts. This file already exceeds 200 lines, and this change adds a separate lifecycle unit.

As per coding guidelines, “Split logic across files; avoid long single-file modules (>200 LoC).”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/dev/server.ts` around lines 211 - 243, Extract the worker shutdown
coordination logic from the server.ts file into a new internal module such as
src/dev/_worker-lifecycle.ts. Move the `#shutdownWorker` method, the `#shuttingDown`
state field, and related shutdown protocol constants like SHUTDOWN_TIMEOUT into
the new module. Create a class or exported functions in the new module that
encapsulate this lifecycle management, then update the server.ts file to use the
extracted module, removing the original `#shutdownWorker` method and `#shuttingDown`
field while delegating to the new module.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/dev/server.ts`:
- Around line 235-237: Update the catch block around the shutdown message
delivery to capture the send error and issue a warning containing the shutdown
operation context and error message, then continue calling done() as before.
- Line 155: Update close() before the await this.#shutdownWorker() call to set a
permanent closing state, reject both new and already queued reload() operations,
and await any active reload represented by `#reloadPromise` before invoking
`#manager.close`(). Ensure reload cannot call `#manager.reload` after closing
begins, while preserving the existing shutdown and watcher-close sequence.
- Around line 82-84: Update the shutdown flow in `#shutdownWorker`() and local
close() so RunnerManager cleanup occurs before `#shuttingDown` is cleared, or keep
that flag set through manager.close(). Ensure registered onClose listeners
observe the intentional-shutdown state and do not call reload(), while
preserving listener cleanup ordering.

---

Nitpick comments:
In `@src/dev/server.ts`:
- Around line 211-243: Extract the worker shutdown coordination logic from the
server.ts file into a new internal module such as src/dev/_worker-lifecycle.ts.
Move the `#shutdownWorker` method, the `#shuttingDown` state field, and related
shutdown protocol constants like SHUTDOWN_TIMEOUT into the new module. Create a
class or exported functions in the new module that encapsulate this lifecycle
management, then update the server.ts file to use the extracted module, removing
the original `#shutdownWorker` method and `#shuttingDown` field while delegating to
the new module.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 203bb772-0c7f-4261-a187-31c373a3b410

📥 Commits

Reviewing files that changed from the base of the PR and between 52abde8 and 41d7d70.

📒 Files selected for processing (1)
  • src/dev/server.ts

Comment thread src/dev/server.ts
Comment thread src/dev/server.ts
Comment thread src/dev/server.ts Outdated
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.

Close Hook not awaited

1 participant