fix(tasks): stop scheduled tasks on runtime close - #4416
Conversation
|
@Prains is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing (2)
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthrough
ChangesRuntime shutdown lifecycle
Priority: ➖ Normal — Impact reflects medium issue severity. Estimated code review effort: 3 (Moderate) | ~20 minutes Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Scheduled Croner tasks now remain active during server operation and stop on Nitro close, allowing graceful shutdown without lingering timers. No current merge-blocking risk remains. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
pi0
left a comment
There was a problem hiding this comment.
While this is a valid fix for durable servers, if someone makes a custom entry like CLI only app, scheduled tasks should keep running even without server listener ref.
I think better alternative would be to make sure scheduled tasks are destroyed when calling nitro close hook in runtime.
|
@pi0 Updated in |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/runtime/internal/app.ts`:
- Around line 42-50: Make shutdown resilient in nitroRuntimeHooksPlugin at
src/runtime/internal/app.ts lines 42-50 by catching failures from the memoized
close hook and ensuring the original server.close call always proceeds; in
src/runtime/internal/task.ts lines 69-73, wrap each job.stop() invocation in
try/catch so one failing job does not prevent subsequent jobs from stopping or
reject the close hook.
In `@src/runtime/internal/task.ts`:
- Around line 75-89: Update the Cron construction in the scheduledTasks loop to
pass an options object with unref enabled, while preserving the existing
scheduled callback. Update the related unit test expectation to include this
Cron option.
🪄 Autofix (Beta)
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
Run ID: 4cb6a897-b57a-4a6c-85f1-583e8efacb1c
📒 Files selected for processing (8)
src/presets/bun/runtime/bun.tssrc/presets/deno/runtime/deno-server.tssrc/presets/node/runtime/node-cluster.tssrc/presets/node/runtime/node-server.tssrc/runtime/internal/app.tssrc/runtime/internal/task.tstest/unit/runtime-app.test.tstest/unit/task.test.ts
|
I noticed same issue with bun runtime with scheduledTasks. looking to this fix |
|
Rebased this on latest main locally to check it. Notes:
I could not push my rebase from this environment. This comment was written by an AI assistant on behalf of the Nitro maintainers. Please double-check anything that looks wrong. |
Co-Authored-By: Claude Code <noreply@anthropic.com>
92d131a to
7612a1a
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
|
@pi0, updated the branch based on the latest feedback and rebased it onto
Validation: formatting, lint, build, typecheck, and all 7 targeted tests passed. The full Rollup and Rolldown runs each had 1,039 passing tests and one failure in For transparency, this comment was prepared with AI assistance. The feedback was checked against the current code, and the validation results above include the observed test failures. |
…nasked A flat default capped runTask itself, since the socket stays idle for as long as the task runs. listTasks keeps a 30s default because the dev server answers it immediately; runTask now has none and takes one through TaskRunnerOptions. Passing `timeout: undefined` was not equivalent to omitting it: the socket still emitted a `timeout` event when the peer closed the idle connection, and the handler rejected with "after undefinedms". Both the option and the handler are now set only when a timeout is configured. Renames the test file to task-runner.test.ts to leave test/unit/task.test.ts to nitrojs#4416, which covers the runtime scheduler. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
commit: |
Rename leftover unref test, use vi.stubEnv for TEST, and document close-hook cleanup for node_middleware.
Resolves #4415.
Problem
Scheduled Croner timers must stay referenced for custom CLI-only entries, but referenced timers also need to be released during graceful server shutdown.
Change
closehook.closehook for Bun, Node, Node cluster, and Deno server presets.Verification
pnpm testServer closed successfully.