oven-sh / bun

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

Unexpected behavior with Bun.serve with --watch mode when exiting #5624

Open fjpedrosa opened 1 year ago

fjpedrosa commented 1 year ago

What version of Bun is running?

v1.0.2

What platform is your computer?

macOS Ventura 13.5.2

What steps can reproduce the bug?

Using this code

// index.ts
import { Errorlike } from 'bun';

const SERVER_PORT = 8080;

const onError = (error: Errorlike) => {
  if (error.syscall !== 'listen') {
    throw error;
  }

  switch (error.code) {
    case 'EADDRINUSE':
      console.error(`${SERVER_PORT} is already in use`);
      server.stop();
      process.exit(1);
    default:
      throw error;
  }
};

const onExit = (message: string) => {
  return async () => {
    console.log(message);
    server.stop();
    console.log('Closed http server');
  };
};

const server = Bun.serve({
  port: SERVER_PORT ?? 8080,
  development: true,
  fetch(req) {
    return new Response('hellow');
  },
  error: onError,
});

console.log(`Server listening on ${server.hostname}:${server.port}`);

process.on('exit', onExit('exit issued'));

1) When executing the server with --watch mode and then finishing the process by using CTRL+C:

Screenshot 2023-09-17 at 16 28 37

2) When executing the server with --watch mode and then modifying the index.ts file and saving it:

Screenshot 2023-09-17 at 16 30 27

What is the expected behavior?

In case 1), when finishing the process I expect to see in console the message "exit issued"

In case 2), when saving the modified file I expect not to have any error.

What do you see instead?

See previous attached images.

Additional information

No response

AlexisWalravens commented 1 year ago

Hi @fjpedrosa I had the same issue and I discovered that you need to add the --watch flag before run

https://bun.sh/docs/cli/run#watch

bun --watch run dev # ✔️ do this
bun run dev --watch # ❌ don't do this
AlexisWalravens commented 1 year ago

Although I just notice that I have to add

process.on('SIGINT', () => {
  process.exit()
})

Otherwise the process is still running on ctrl+c that might be a bug ?

catinrage commented 8 months ago

I was working on win 11 wsl and restarting the wsl fixed the issue.