nitrojs / nitrojs/nitro

Scheduled tasks keep Bun process alive after graceful shutdown

Open Beginner friendly
#4,415 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

v3
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Environment

  • Nitro: current main (eee0abd9fc4f729445f5b1a4da06fa78daf7a3da) and 3.0.260610-beta
  • Preset: bun
  • Bun: 1.3.14
  • OS: macOS arm64

Reproduction

Configure one scheduled task:

export default defineNitroConfig({
  preset: "bun",
  experimental: {
    tasks: true,
  },
  scheduledTasks: {
    "*/5 * * * *": ["test"],
  },
});

Build and start the server, then send SIGTERM:

pnpm nitro build
bun .output/server/index.mjs &
pid=$!
sleep 1
kill -TERM "$pid"
sleep 10
kill -0 "$pid"

The HTTP server logs Server closed successfully., but the process remains alive and kill -0 succeeds. The behavior reproduces without Redis or other external connections.

Description

startScheduleRunner() creates a Croner instance with the default unref: false option and discards the instance:

new Cron(schedule.cron, callback);

After the HTTP listener closes, the referenced Croner timeout keeps the Bun event loop alive. Containers therefore require a forced kill during shutdown.

Passing { unref: true } to Croner allows the process to exit after the server closes without changing task scheduling while the server is running.

I will submit a focused PR with a regression test.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at startScheduleRunner() and inspect the Croner construction described in the issue. Verify the shutdown behavior with the provided Bun reproduction, then add the requested regression test and confirm that the process exits after the HTTP server closes while scheduled tasks still run normally.

Written by the indexing model from the issue text.

Assessment

Tech stack
bun, typescript
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.