Skip to content

Commit

Permalink
fix: Explicitly close HTTP server to avoid process.exit
Browse files Browse the repository at this point in the history
Using `process.exit` directly can cause output to stderr/stdout to be
truncated, since it
[does not wait for output to streams to be sent](https://nodejs.org/api/process.html#process_process_exit_code).

This probably isn't an issue here for the PIP service, but it's worth
avoiding it since explicitly closing the HTTP server created by express
is easy, and will cause the process to quit "naturally", as nothing else
is running.

We did actually run into this issue in the [fuzzy-tester](pelias/fuzzy-tester#44)
and it's a pain to track down.
  • Loading branch information
orangejulius committed Dec 1, 2017
1 parent 4d77c7d commit f1f2cf2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const app = require('./app')();

try {
const app = require('./app')();
const port = ( parseInt(process.env.PORT) || 3102 );

app.listen(port, () => {
Expand All @@ -8,6 +9,8 @@ try {

} catch (err) {
console.error(err);
process.exit(1);

// use exitCode to exit safely: https://nodejs.org/api/process.html#process_process_exit_code
process.exitCode = 1;
app.close();
}

0 comments on commit f1f2cf2

Please sign in to comment.