Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server refuses to die in container #5

Open
McSneaky opened this issue Mar 4, 2020 · 5 comments
Open

Server refuses to die in container #5

McSneaky opened this issue Mar 4, 2020 · 5 comments

Comments

@McSneaky
Copy link

McSneaky commented Mar 4, 2020

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

@thetutlage
Copy link
Member

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

@RomainLanz
Copy link
Collaborator

RomainLanz commented Mar 4, 2020

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

@McSneaky
Copy link
Author

McSneaky commented Mar 4, 2020

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 added a commit to McSneaky/core that referenced this issue Mar 7, 2020
…utting down message

This commit will make Ignitor to always listen for SIGINT so server is killable in containers.

adonisjs-community/create-adonis-ts-app#5
McSneaky added a commit to McSneaky/core that referenced this issue Mar 7, 2020
This commit will make Ignitor to always listen for SIGINT so server is killable in containers.

adonisjs-community/create-adonis-ts-app#5
targos added a commit to targos/adonis-core that referenced this issue Feb 18, 2021
If the process is running from a terminal, display a message to the user
telling that they can press CTRL+C again to force an immediate shutdown.

Closes: adonisjs-community/create-adonis-ts-app#5
@targos
Copy link
Contributor

targos commented Feb 18, 2021

Opened adonisjs/core#2273 to try and fix this

@romch007
Copy link
Contributor

romch007 commented Jul 6, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants