honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
17.3k stars 488 forks source link

(@hono/node-server) I can't close web server #3104

Closed ringo360 closed 2 weeks ago

ringo360 commented 3 weeks ago

What version of Hono are you using?

4.4.11

What runtime/platform is your app running on?

Node.js

What steps can reproduce the bug?

  1. Write code
    
    // utils/Scheduler.ts
    import consola from 'consola';
    import color from 'picocolors';
    import { setTimeout } from 'timers/promises';

const closeListeners: any = [];

export async function shutdown() { consola.start(color.italic('Shutting down...')); try { await Promise.race([ Promise.all(closeListeners.map((closeListener: any) => closeListener())), setTimeout(5000), ]); } catch (e) { console.error(e); } consola.success(color.italic('Goodbye!')); process.exit(0); }

process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown);

/**

// websv.ts
import { serve } from '@hono/node-server';
import { Hono } from 'hono';
import consola from 'consola';
import color from 'picocolors';
import { config } from './utils/CLoader';
import { onShutdown } from './utils/Scheduler';

const app = new Hono();

app.get('/', (c) => c.text('Hello Node.js!'));

const websv = serve({
    fetch: app.fetch,
    port: config.web.port,
});

//prettier-ignore
consola.info(color.green(`[WebSV] Web Server is ready on port ${color.cyan(config.web.port)}`));

onShutdown(() => {
    consola.info(color.cyan('[WebSV] Called shutdown event'));
    websv.close();
    consola.info(color.red('[WebSV] Closed web server'));
});
  1. run
  2. Press ctrl+c or kill task
  3. Program will exit with code 1

What is the expected behavior?

The program should have terminated with code 0 and a graceful shutdown should have been achieved.

What do you see instead?

You can confirm that the code is not terminating as expected: Terminal(VSCode)

When I ran shutdown with the exception of websv.ts, the exit code was 0.

Additional information

No response

yusukebe commented 3 weeks ago

Hi @ringo360

I've tried your code. Below is the behavior on my machine.

https://github.com/honojs/hono/assets/10682/1c1f938b-d03e-4824-bfca-215cef8f4a0d

I think this is expected behavior. If you still have a problem, could you provide a minimal project to reproduce it?

ringo360 commented 2 weeks ago

Ok, I'll prepare a minimum project.

EdamAme-x commented 2 weeks ago

I think this is not a problem of hono image

Sample repo https://github.com/EdamAme-x/hono-issue-3104

ringo360 commented 2 weeks ago

I created repo https://github.com/ringo360/hono-i3104/

yusukebe commented 2 weeks ago

@ringo360 Thanks!

I've tried it, and it will throw status code 130 since I typed Ctrl+C.

https://github.com/honojs/hono/assets/10682/ac7c9b35-719f-4c9e-bf73-e93352832c75

So, the behavior may change depending on the environment. We have to figure out if it's Hono matter or not. Can you try some cases like it without Hono?

import consola from 'consola'
import color from 'picocolors'
import { config } from './utils/CLoader'
import { onShutdown } from './utils/Scheduler'
import http from 'http'

const server = http.createServer((_req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.end('Hello Node.js!')
})

server.listen(config.web.port, () => {
  consola.info(color.green(`[WebSV] Web Server is ready on port ${color.cyan(config.web.port.toString())}`))
})

onShutdown(() => {
  consola.info(color.cyan('[WebSV] Called shutdown event'))
  server.close(() => {
    consola.info(color.red('[WebSV] Closed web server'))
  })
})
ringo360 commented 2 weeks ago

Sorry, this seems to be a problem in another area. It threw status code 1 when I typed Ctrl+C.

https://github.com/honojs/hono/assets/105296365/280b2b90-ad9a-456d-ad0a-664be15cfcfa

yusukebe commented 2 weeks ago

@ringo360

So, is not the issue on the Hono side?

ringo360 commented 2 weeks ago

@yusukebe Probably yes, may I close this issue?

yusukebe commented 2 weeks ago

@ringo360

Okay! I'll close this issue. If you have any problem, feel free to reopen it.

ringo360 commented 2 weeks ago

tysm <3