fastify / fastify

Fast and low overhead web framework, for Node.js

Home Page:https://www.fastify.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using fastify with Phusion Passenger fails with EADDRINUSE

pnpavlov opened this issue · comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.26.2

Plugin version

No response

Node.js version

20.10.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Linux Red Hat Enterprise Linux 7

Description

I'd like to try out using fastify instead of Express with Phusion Passenger. According to the official guide here it works like that: "Passenger implements this on the Node.js side by hooking into the first http.Server object in your app that calls listen(). That http.Server object will be selected as the server object to receive requests. When run in Passenger, the port number that you pass to listen() is irrelevant and has no effect. Passenger always makes the app listen on a random Unix domain socket." [1]

I followed the example on their page about Express and it worked as described - by setting app.listen('passenger').
After testing that passenger works as expected, I tried to switch from express to fastify and got "Error: listen EADDRINUSE: address already in use passenger"

I belied that I am facing issue similar to #4278 and https://stackoverflow.com/questions/73563025/how-to-reverse-port-biding-phusion-passenger-and-fastify and tried changing the app.listen() from { path: 'passenger' } to { path: 'passenger', host: '127.0.0.1' } , but got exactly the same error.

Happy to collect additional logs or try out stuff if someone has an idea, as I would really be interested to try out fastify... Thanks!

Also open to take care of contacting the Passenger Library folks in case it works - maybe they could add it to the examples list to save time for other users. If that's not possible for obvious tech reasons, could someone share briefly why that happens, could be useful in the future...

package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.19.2",
    "fastify": "^4.26.2"
  }
}

server.js

if (typeof(PhusionPassenger) !== 'undefined') {
    PhusionPassenger.configure({ autoInstall: false });
    console.log("Phusion Passenger found");
}

// express version
// var express = require('express');
// var app = express();

var fastify = require('fastify');
var app = fastify({ logger: true });

app.get('/', function(req, res) {
    console.log("Request received for /");
    res.end('Home');
});

if (typeof(PhusionPassenger) !== 'undefined') {
    // app.listen('passenger');  // works for express
   // app.listen({ path: 'passenger'}); // fails with EADDRINUSE: address already in use passenger
    app.listen({ path: 'passenger', host: '127.0.0.1' }); // fails with same EADDRINUSE
    console.log("App started with passenger");
} else {
    app.listen({ port: 3000 });
    console.log("App started standalone");
}

logs

App 3836 output: stdin: is not a tty
App 3836 output: Phusion Passenger found
App 3836 output: App started with passenger
App 3836 output: node:internal/errors:563
App 3836 output:     ErrorCaptureStackTrace(err);
App 3836 output:     ^
App 3836 output: Error: listen EADDRINUSE: address already in use passenger
App 3836 output:     at Server.setupListenHandle [as _listen2] (node:net:1855:21)
App 3836 output:     at listenInCluster (node:net:1920:12)
App 3836 output:     at Server.listen (node:net:2019:5)
App 3836 output:     at Server.listenAndMaybeInstall [as listen] (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:357:30)
App 3836 output:     at /home/.../app/20/lib/node_modules/fastify/lib/server.js:266:14
App 3836 output:     at new Promise (<anonymous>)
App 3836 output:     at /home/../app/20/lib/node_modules/fastify/lib/server.js:265:20
App 3836 output:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
App 3836 output:   code: 'EADDRINUSE',
App 3836 output:   errno: -98,
App 3836 output:   syscall: 'listen',
App 3836 output:   address: 'passenger',
App 3836 output:   port: -1
App 3836 output: }
App 3836 output: Node.js v20.10.0

Steps to Reproduce

  1. npm init
  2. npm install fastify
  3. implement and adjust the example from express-js-solution-example
  4. run the app with Phusion Passenger (or contact me to run it for you and provide logs)

Expected Behavior

I would have expected the behaviour to be similar between different frameworks.

I recommend you to contact Phusion Passenger support. They offer professional support with much deeper expertise on their product that we can possibly offer.