Skip to content

Scheduled tasks keep Bun process alive after graceful shutdown #4415

Closed

Description

@Prains

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions