create-adonis-ts-app icon indicating copy to clipboard operation
create-adonis-ts-app copied to clipboard

Server refuses to die in container

Open McSneaky opened this issue 5 years ago • 5 comments

SIGINT seems to be ignored by server. HTTP server exposes .kill() and .close() but default listener for SIGINT seems to be missing

Package version

Adonis packages

    "@adonisjs/assembler": "^1.3.4",
    "@adonisjs/ace": "^6.8.4",
    "@adonisjs/core": "^5.0.0-preview.4",
    "@adonisjs/fold": "^6.3.4",

Node.js and npm version

NodeJS: 13.9.0 NPM: 6.13.7

Sample Code (to reproduce the issue)

When building container and setting start command as CMD [ "node", "./build/server.js" ] Container does not die gracefully with SIGINT since SIGINT is sent to process, which just ignores it

Fix is to add process.on('SIGINT') yourself to some part of application, server.ts for example

// server.ts (original)
new Ignitor(__dirname)
  .httpServer()
  .start()
  .catch(console.error)

Workaround is to chop it into multiple pieces and adding SIGINT listener

// server.ts (workaround)
const server = new Ignitor(__dirname)
  .httpServer()

server.start()
  .catch(console.error)

// Without it process won't die in container
process.on('SIGINT', () => {
  server.kill(10)
})

BONUS (a sample repo to reproduce the issue)

https://gitlab.com/McSneaky/youtube-downloader

McSneaky avatar Mar 04 '20 02:03 McSneaky

We listen for SIGINT but I believe only when process is started using pm2 https://github.com/adonisjs/core/blob/develop/src/Ignitor/SignalsListener/index.ts#L36.

So is there any way to detect that app is inside a Docker container, so that we can listen for this event?

Why not always listen for it?

Because the terminal sends SIGINT when pressing Ctrl + C and doing a graceful shutdown may have some delay, so the user will be under impression that Ctrl + C was never pressed

thetutlage avatar Mar 04 '20 08:03 thetutlage

Could't we always listen for SIGINT and display a message when Ctrl + C is pressed so they know something is happening?

RomainLanz avatar Mar 04 '20 09:03 RomainLanz

I think @RomainLanz has good idea.

Could log some message like: Server is shutting down when SIGINT comes in

Also it seems it turned out to be issue in core? Perhaps should move?

McSneaky avatar Mar 04 '20 10:03 McSneaky

Opened https://github.com/adonisjs/core/pull/2273 to try and fix this

targos avatar Feb 18 '21 11:02 targos

The problem might be that nodejs is not designed to run as PID 1 which leads to unexpected behaviour when running inside of Docker. For example, a Node.js process running as PID 1 will not respond to SIGINT (CTRL-C) and similar signals. I suggest you use a lightweight init system (like tini) to wrap you nodejs process.

romch007 avatar Jul 06 '22 18:07 romch007