Bun overtakes port (used by rails) without any hesitation

40 views Asked by At

I'm running two servers:

  • bun/elysia server on localhost:3000
  • rails server on localhost:3001

The code below checks for port availability. Bun doesn't notice that port 3001 is already in use. Running my bun server on port 3001 would not result in an error, but bun just overtake/hijack the port (sometimes immediately, sometimes after a couple of refreshes in the browser).

index.ts

const net = require('net');

async function checkPortAvailability(port) {
    return new Promise((resolve, reject) => {
        const tester = net.createServer()
        .once('error', err => (err.code === 'EADDRINUSE' ? resolve(false) : reject(err)))
        .once('listening', () => tester.once('close', () => resolve(true)).close())
        .listen(port);
    });
}

async function portStatus(port) {
    const isAvailable = await checkPortAvailability(port);
    console.log(`Port ${port} is ${isAvailable ? 'available' : 'not available'}`);    
}


await portStatus(3000)
await portStatus(3001)

results in:

Port 3000 is not available
Port 3001 is available

If I instead of a rails server, use another bun server (e.g. the below hello_server.ts) on port 3001 and then try to take it over with my original bun server, and then run my port check script, it will result in both port 3000 and 3001 not being available (as expected).

hello_server.ts

export default {
    port: 3001,
    fetch(request) {
    const host = request.headers.get('Host')
      return new Response(`Hello world is running at ${this.hostname}:${this.port}`, { status: 200 });
    },
  };

Is rails too weak or Bun to strong?

0

There are 0 answers