0

I have a NodeJS server running in the background that I start with:

NODE_ENV=production npm start  >> stdout.txt 2>> stderr.txt &

When I restart it with:

kill <node process id>; NODE_ENV=production npm start  >> stdout.txt 2>> stderr.txt &

I sometimes see a long error in the logs:

/opt/bitnami/nodejs/bin/.node.bin[8878]: ../src/node.cc:663:void node::ResetStdio(): Assertion `(0) == (err)' failed.
 1: 0x9ef190 node::Abort() [/opt/bitnami/nodejs/bin/.node.bin]
 2: 0x9ef217  [/opt/bitnami/nodejs/bin/.node.bin]
 3: 0x9bd657 node::ResetStdio() [/opt/bitnami/nodejs/bin/.node.bin]
 4: 0x9bd6c0 node::SignalExit(int) [/opt/bitnami/nodejs/bin/.node.bin]
 5: 0x7fca6718e390  [/lib/x86_64-linux-gnu/libpthread.so.0]
 6: 0x7fca66ebaad3 epoll_wait [/lib/x86_64-linux-gnu/libc.so.6]
 7: 0x13200b0  [/opt/bitnami/nodejs/bin/.node.bin]
 8: 0x130e26b uv_run [/opt/bitnami/nodejs/bin/.node.bin]
 9: 0xa31ec3 node::NodeMainInstance::Run() [/opt/bitnami/nodejs/bin/.node.bin]
10: 0x9c1cc8 node::Start(int, char**) [/opt/bitnami/nodejs/bin/.node.bin]
11: 0x7fca66dd3840 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
12: 0x95c085  [/opt/bitnami/nodejs/bin/.node.bin]
Aborted (core dumped)
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! App@0.0.1 start: `node app.js`
npm ERR! Exit status 134
npm ERR! 
npm ERR! Failed at the App@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/bitnami/.npm/_logs/2020-12-17T21_05_41_838Z-debug.log
npm[8862]: ../src/node.cc:663:void node::ResetStdio(): Assertion `(0) == (err)' failed.
 1: 0x9ef190 node::Abort() [npm]
 2: 0x9ef217  [npm]
 3: 0x9bd657 node::ResetStdio() [npm]
 4: 0x7fd18c8c7008  [/lib/x86_64-linux-gnu/libc.so.6]
 5: 0x7fd18c8c7055  [/lib/x86_64-linux-gnu/libc.so.6]
 6: 0x994907  [npm]
 7: 0xbc9a29  [npm]
 8: 0xbcb817 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [npm]
 9: 0x13a72b9  [npm]

and sometimes I see a short error:

Terminated
npm ERR! code ELIFECYCLE
npm ERR! errno 143
npm ERR! App@0.0.1 start: `node app.js`
npm ERR! Exit status 143
npm ERR! 
npm ERR! Failed at the App@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/bitnami/.npm/_logs/2020-12-17T21_06_43_530Z-debug.log

What is the difference between the two, and what is the best way to restart a NodeJS server?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64

1 Answers1

1

don't kill the server (as it might leave bind the best way to shutdown the server, will be to implement a mechanism for gracefully closing the server.

for example, you can do that by catching SIGINT (or any other signal exposed to a process) and handle the server termination.

in nodejs, you can do that by utilizing the process.on()

process.on("SIGINT", () => {
  console.log("SIGINT received");
});

to send a SIGINT to process, do

kill -s SIGINT <node process id>

you might also be interested in this thread

Mr.
  • 9,429
  • 13
  • 58
  • 82
  • Thanks! That answers another question: https://stackoverflow.com/questions/64542392/how-to-avoid-logging-errors-from-restarting-nodejs-server/65357796#65357796 – miguelmorin Dec 18 '20 at 13:21
  • That handles the short error but not the long error. Could it come from the server be busy handling some request? If so, how to wait for the request stack to be empty before terminating? – miguelmorin Dec 20 '20 at 09:23