pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

v6.0.5 Cluster Mode not working with Bun

Open dienstbereit opened this issue 10 months ago • 9 comments

I’m experiencing an issue where PM2 version 6.0.5 does not seem to work in Cluster Mode with Bun, even though the release notes mention that Bun should be compatible with PM2 now.

  1. Create a simple Bun script (index.js):
const server = Bun.serve({
  port: 3000,
  fetch(req) {
    return new Response("Bun!");
  },
});
  1. Set up the PM2 ecosystem configuration (ecosystem.config.js):
module.exports = {
  name: "Cluster Test",
  script: "index.js",
  instances: "max",
  exec_mode: "cluster",
  watch: false,
};
  1. Start the application with PM2 using the configuration above.
  2. Check the PM2 logs.

Expected Behavior: The application should run in Cluster Mode, with instances of Bun being spawned as specified in the ecosystem.config.js file.

Actual Behavior: There are no errors in the PM2 logs, but the application does not seem to function as expected in Cluster Mode. The cluster instances are not being spawned, or the application is not responding properly.

Environment: PM2 Version: 6.0.5 Bun Version: v1.2.5 Node.js Version: v23.6.0 OS: Linux Ubuntu x86_64 x86_64 x86_64 GNU/Linux

Additional Context: PM2 does not show any error messages in the logs. The issue only occurs in Cluster Mode, and switching to fork mode seems to work fine with Bun.

Image

dienstbereit avatar Mar 16 '25 15:03 dienstbereit

Thank you for this quality report. I think that it's because PM2 use the node.js engine. To be able for the PM2 cluster to works it needs to be run with Bun. Could you run a 'pm2 report' commands and tell me if it's the case ?

Unitech avatar Mar 17 '25 10:03 Unitech

@Unitech thank you very much for your quick feedback. This report is from my local machine. I hope my setup is correct and I have not misunderstood something about the PM2/BUN concept šŸ¤·ā€ā™‚ļø

--- PM2 report ----------------------------------------------------------------
Date                 : Mon Mar 17 2025 13:32:52 GMT+0100 (Central European Standard Time)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 6.0.5
node version         : 23.6.0
node path            : /opt/homebrew/bin/pm2
argv                 : /opt/homebrew/Cellar/node/23.6.0/bin/node,/opt/homebrew/lib/node_modules/pm2/lib/Daemon.js
argv0                : /opt/homebrew/Cellar/node/23.6.0/bin/node
user                 : user
uid                  : 501
gid                  : 20
uptime               : 124min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 6.0.5
node version         : 23.6.0
node path            : /opt/homebrew/bin/pm2
argv                 : /opt/homebrew/Cellar/node/23.6.0/bin/node,/opt/homebrew/bin/pm2,report
argv0                : node
user                 : user
uid                  : 501
gid                  : 20
===============================================================================
--- System info --------------------------------------------
arch                 : arm64
platform             : darwin
type                 : Darwin
cpus                 : Apple M4 Pro
cpus nb              : 14
freemem              : 327008256
totalmem             : 25769803776
home                 : /Users/user
===============================================================================
--- PM2 list -----------------------------------------------
ā”Œā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│ id │ name            │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
ā”œā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│ 0  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 1  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 2  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 3  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 4  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 5  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 6  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 7  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user    │ disabled │
│ 8  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 9  │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 10 │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 11 │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 12 │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
│ 13 │ Cluster Test    │ default     │ 1.0.0   │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ user     │ disabled │
ā””ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
===============================================================================
--- Daemon logs --------------------------------------------
/Users/user/.pm2/pm2.log last 20 lines:
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:11] exited with code [0] via signal [SIGINT]
PM2        | 2025-03-17T13:32:49: PM2 log: Script /Users/user/Sites/localhost/pm2-bun-cluster-test/index.js had too many unstable restarts (16). Stopped. "errored"
PM2        | 2025-03-17T13:32:49: PM2 log: App name:Cluster Test id:9 disconnected
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:9] exited with code [0] via signal [SIGINT]
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:9] starting in -cluster mode-
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:10] online
PM2        | 2025-03-17T13:32:49: PM2 log: App name:Cluster Test id:13 disconnected
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:13] exited with code [0] via signal [SIGINT]
PM2        | 2025-03-17T13:32:49: PM2 log: Script /Users/user/Sites/localhost/pm2-bun-cluster-test/index.js had too many unstable restarts (16). Stopped. "errored"
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:12] online
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:9] online
PM2        | 2025-03-17T13:32:49: PM2 log: App name:Cluster Test id:10 disconnected
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:10] exited with code [0] via signal [SIGINT]
PM2        | 2025-03-17T13:32:49: PM2 log: Script /Users/user/Sites/localhost/pm2-bun-cluster-test/index.js had too many unstable restarts (16). Stopped. "errored"
PM2        | 2025-03-17T13:32:49: PM2 log: App name:Cluster Test id:12 disconnected
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:12] exited with code [0] via signal [SIGINT]
PM2        | 2025-03-17T13:32:49: PM2 log: Script /Users/user/Sites/localhost/pm2-bun-cluster-test/index.js had too many unstable restarts (16). Stopped. "errored"
PM2        | 2025-03-17T13:32:49: PM2 log: App name:Cluster Test id:9 disconnected
PM2        | 2025-03-17T13:32:49: PM2 log: App [Cluster Test:9] exited with code [0] via signal [SIGINT]
PM2        | 2025-03-17T13:32:49: PM2 log: Script /Users/user/Sites/localhost/pm2-bun-cluster-test/index.js had too many unstable restarts (16). Stopped. "errored"

dienstbereit avatar Mar 17 '25 12:03 dienstbereit

Could you run a 'pm2 report' commands and tell me if it's the case ?

@Unitech have you been able to take a closer look at the problem? Or is the problem with my implementation?

dienstbereit avatar Mar 30 '25 19:03 dienstbereit

Yes this is caused by https://github.com/Unitech/pm2/blob/master/bin/pm2#L1 The shebang calls node, and in my knowledge, there is currently no way in bash, while keeping windows support to call bun instead of node. I will create a specific pm2 "binary" that will call Bun instead

Unitech avatar Apr 01 '25 06:04 Unitech

Hi there, from what I know you can force shebang forced apps to be called with bun using the --bun param. So for example, bunx --bun pm2 start should work here.

Kuuzoo avatar Apr 01 '25 13:04 Kuuzoo

Also, I've just tried to run your exact script using bun v1.2.8 on the a mac with the same cores and chip without any troubles.

By using either of the following commands: bunx --bun pm2 start & pm2 start (pm2 globally installed via bun).

Image

Kuuzoo avatar Apr 01 '25 13:04 Kuuzoo

unfortunately upgrading to v6 didn't work for me. bunx --bun or just bun yield the same result:

module.exports = {
  apps: [
    {
      name: `myapp`,
      interpreter: "~/.bun/bin/bun",
      script: "server/index.ts",
      instances: "4",
      watch: false,
      exec_mode: "cluster",
      env: {
        NODE_ENV: "production",
      },
    },
  ],
};
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/ec2-user/myapp/server/index.ts

@Unitech any luck with the bun specific binary?

luisrudge avatar Jun 07 '25 01:06 luisrudge

I use command bun x pm2 to run. Try this.

adarshmadrecha avatar Sep 03 '25 16:09 adarshmadrecha

@adarshmadrecha what's your pm2 config?

luisrudge avatar Sep 03 '25 16:09 luisrudge