oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.01k stars 2.66k forks source link

Express.js middlewares are reused when creating a second Express app #7031

Open 1cedsoda opened 10 months ago

1cedsoda commented 10 months ago

What version of Bun is running?

1.0.11+f7f6233ea

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

// bun version 1.0.11+f7f6233ea
// node version v18.18.2

import express from "express";

async function newServer() {
  const app = express();
  const x = Math.random();
  app.use((req, res, next) => {
    console.log("middleware x", x);
    res.send("Hello World");
  });
  return new Promise((resolve, reject) => {
    const listener = app.listen(9999, () => {
      resolve(listener);
    });
  });
}

async function main() {
  const listener = await newServer();
  await fetch("http://localhost:9999");
  await listener.close();

  const listener2 = await newServer();
  await fetch("http://localhost:9999");
  await listener2.close();
}

main();

Running bun x.js

middleware x 0.6306528523654233
middleware x 0.6306528523654233

Expected: numbers to be different Got: numbers are same

Running node x.js

middleware x 0.63468082718277
middleware x 0.16123682051243216

Expected: numbers to be different Got: numbers are different

What is the expected behavior?

Bun: numbers should be different (middlewares are redeclared) Express: numbers should be different (middlewares are redeclared)

What do you see instead?

Bun: numbers are the same (middlewares are reused) Express: numbers should be different (middlewares are redeclared

Findings

In Bun the express middlewares seem to be reused.

Additional information

node version v18.18.2 express version 4.18.2

Electroid commented 10 months ago

This is likely a difference in behaviour for when listening to the same port twice. We will fix it.

1cedsoda commented 9 months ago

Can confirm, it only happens if the same port is used multiple times.

liz3 commented 9 months ago

I can confirm that calling server.stop(true) instead of server.stop() here: https://github.com/oven-sh/bun/blob/main/src/js/node/http.ts#L446 does fix the issue, @Electroid is this something which would be okay to do?

Electroid commented 9 months ago

I can confirm that calling server.stop(true) instead of server.stop() here: https://github.com/oven-sh/bun/blob/main/src/js/node/http.ts#L446 does fix the issue, @Electroid is this something which would be okay to do?

Yep, that should be a good fix.

1cedsoda commented 5 months ago

Still appears in 1.1.0+5903a6141