Skip to content

fix(tasks): stop scheduled tasks on runtime close#4416

Open
Prains wants to merge 3 commits into
nitrojs:mainfrom
Prains:fix/scheduled-task-runner-unref
Open

fix(tasks): stop scheduled tasks on runtime close#4416
Prains wants to merge 3 commits into
nitrojs:mainfrom
Prains:fix/scheduled-task-runner-unref

Conversation

@Prains

@Prains Prains commented Jul 10, 2026

Copy link
Copy Markdown

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

  • Keep scheduled task timers referenced while the runtime is active.
  • Retain Croner jobs and stop them from the Nitro close hook.
  • Forward srvx server shutdown to the Nitro runtime close hook for Bun, Node, Node cluster, and Deno server presets.
  • Cover timer ownership, close cleanup, and srvx lifecycle forwarding with unit tests.

Verification

  • pnpm test
  • Rollup: 900 passed, 137 skipped, 13 todo
  • Rolldown: 900 passed, 137 skipped, 13 todo
  • Bun 1.3.14 SIGTERM reproduction: process exits with code 0 after Server closed successfully.

@Prains Prains requested a review from pi0 as a code owner July 10, 2026 08:58
@vercel

vercel Bot commented Jul 10, 2026

Copy link
Copy Markdown

@Prains 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 Jul 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds a Nitro runtime server plugin that runs the "close" hook before server shutdown, registers it across Bun, Deno, and Node presets, and stops scheduled Cron jobs through the same lifecycle hook.

Changes

Runtime shutdown lifecycle

Layer / File(s) Summary
Close-hook server plugin
src/runtime/internal/app.ts, test/unit/runtime-app.test.ts
Adds nitroRuntimeHooksPlugin, which awaits the Nitro "close" hook before invoking the original server close method, with ordering and failure-path tests.
Preset server wiring
src/presets/bun/runtime/bun.ts, src/presets/deno/runtime/deno-server.ts, src/presets/node/runtime/node-server.ts, src/presets/node/runtime/node-cluster.ts
Registers nitroRuntimeHooksPlugin in the server plugin configuration for each runtime preset.
Scheduled task shutdown cleanup
src/runtime/internal/task.ts, test/unit/task.test.ts
Retains scheduled Cron instances, stops them during the Nitro "close" hook, and tests cleanup when a stop operation fails.

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

Possibly related PRs

  • nitrojs/nitro#4076: Both changes modify scheduled-task lifecycle behavior around startScheduleRunner.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #4415 by stopping retained scheduled timers on shutdown so the process can exit normally.
Out of Scope Changes check ✅ Passed The added preset wiring, cleanup logic, and tests all support the shutdown lifecycle fix and stay in scope.
Title check ✅ Passed The title uses conventional commits and clearly summarizes the runtime-close cleanup change.
Description check ✅ Passed The description directly matches the changes to scheduled-task shutdown cleanup and runtime close forwarding.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@pi0 pi0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@Prains Prains changed the title fix(tasks): unref scheduled task timers fix(tasks): stop scheduled tasks on runtime close Jul 11, 2026
@Prains

Prains commented Jul 11, 2026

Copy link
Copy Markdown
Author

@pi0 Updated in 089466b8: scheduled timers remain referenced for CLI-only entries, Croner jobs stop from the Nitro close hook, and srvx server shutdown now forwards to that runtime hook for Bun, Node, Node cluster, and Deno server presets. The Bun SIGTERM reproduction exits cleanly, and the full Rollup/Rolldown suite passes.

@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: 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

📥 Commits

Reviewing files that changed from the base of the PR and between eee0abd and 089466b.

📒 Files selected for processing (8)
  • src/presets/bun/runtime/bun.ts
  • src/presets/deno/runtime/deno-server.ts
  • src/presets/node/runtime/node-cluster.ts
  • src/presets/node/runtime/node-server.ts
  • src/runtime/internal/app.ts
  • src/runtime/internal/task.ts
  • test/unit/runtime-app.test.ts
  • test/unit/task.test.ts

Comment thread src/runtime/internal/app.ts
Comment thread src/runtime/internal/task.ts
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.

Scheduled tasks keep Bun process alive after graceful shutdown

2 participants