Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

fix(server): handle kernel signals by including tini #1387

Merged
merged 5 commits into from
Apr 19, 2024

Conversation

PixnBits
Copy link
Contributor

@PixnBits PixnBits commented Apr 13, 2024

Description

Wrap the Node.js process with the tini init wrapper. This enables proper kernel signal handling inside the container, removing the need for forceful shutdowns (e.g. $ docker ps; docker kill <container-id>).

This adds 96kB to the image size.

We may later be able to remove src/server/shutdown.js if the Babel issue is resolved (it's been a number of years). Though, it might serve has a failure handling in the event we miss something that keeps Node.js running (e.g. a setInterval without .unref()).

Motivation and Context

Node.js was 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. ...
You can...include Tini directly in your Dockerfile, ensuring your process is always started with an init wrapper.

https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#handling-kernel-signals

How Has This Been Tested?

I've built the image locally, used it via @americanexpress/one-app-runner (npm start -- --offline --docker-image=sha256:...) and upon CTRL+C the container stops (due to the Node.js process ending).

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (adding or updating documentation)
  • Dependency update
  • Security update

Checklist:

  • My change requires a change to the documentation and I have updated the documentation accordingly.
  • These changes should be applied to a maintenance branch. (fix(server): handle kernel signals by including tini #1388)
  • This change requires cross browser checks.
  • Performance tests should be ran against the server prior to merging.
  • This change impacts caching for client browsers.
  • This change impacts HTTP headers.
  • This change adds additional environment variable requirements for One App users.
  • I have added the Apache 2.0 license header to any new files created.

What is the Impact to Developers Using One App?

Don't have to wait 10,000ms for the shutdown check to run. Should also be more graceful and faster shutdowns in production.

Copy link
Contributor

github-actions bot commented Apr 13, 2024

Size Change: 0 B

Total Size: 232 kB

ℹ️ View Unchanged
Filename Size
./build/app/app.js 38.2 kB
./build/app/app~vendors.js 129 kB
./build/app/runtime.js 5.64 kB
./build/app/service-worker-client.js 5.46 kB
./build/app/vendors.js 54.1 kB

compressed-size-action

Dockerfile Show resolved Hide resolved
Copy link
Member

@10xLaCroixDrinker 10xLaCroixDrinker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the start command when running in the repo.

$ npm start

> @americanexpress/[email protected] start
> scripts/start.sh

/sbin/tini -- node --dns-result-order ipv4first --no-experimental-fetch --require=dotenv/config lib/server/index.js
scripts/start.sh: line 39: /sbin/tini: No such file or directory
scripts/start.sh: line 39: exec: /sbin/tini: cannot execute: No such file or directory

revert changes to the start.sh script
@PixnBits
Copy link
Contributor Author

PixnBits commented Apr 16, 2024

good catch! it's also probably better for tini to be the entrypoint rather than deferred to a script file so it can manage all the child processes
updated in fe13e5a

@PixnBits PixnBits closed this Apr 16, 2024
@PixnBits PixnBits reopened this Apr 16, 2024
@PixnBits PixnBits requested review from 10xLaCroixDrinker and a team April 16, 2024 18:10
@PixnBits PixnBits requested a review from a team April 18, 2024 18:39
@10xLaCroixDrinker 10xLaCroixDrinker merged commit f9b13e5 into main Apr 19, 2024
9 checks passed
@10xLaCroixDrinker 10xLaCroixDrinker deleted the fix/handle-kernel-signals branch April 19, 2024 14:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants