|
1 |
| -//TODO |
| 1 | +# Using Exit Stack Traces for Debugging |
| 2 | + |
| 3 | +`--trace-exit` CLI option will print a stack trace on every proactive |
| 4 | +`process.exit` invocation. It is convenient to confirm if the abnormal exit of |
| 5 | +the process was initiated from an invocation of `process.exit` or something |
| 6 | +other crashes. |
| 7 | + |
| 8 | +> Caveat: |
| 9 | +> `--trace-exit` CLI option is available since Node.js v13.5.0. |
| 10 | +
|
| 11 | +## How To |
| 12 | + |
| 13 | +There are conditions that we used an third party library that we have trivial |
| 14 | +knowledge of the implementation details. It is hard to determine where the exit |
| 15 | +is trigger in which library in these conditions. On the first step we have to |
| 16 | +determine if it is a proactive `process.exit` invocation and where it is to |
| 17 | +prevent follow up not trivial work of detailed digging the exit reasons. |
| 18 | + |
| 19 | +The simple step is to add an option `--trace-exit` on starting the Node.js |
| 20 | +process. Since then, we can see a stacktrace on each call of `process.exit`. |
| 21 | + |
| 22 | +```bash |
| 23 | + > node --trace-exit src/abnormal-exit.js |
| 24 | +(node:26480) WARNING: Exited the environment with code 0 |
| 25 | + at exit (internal/process/per_thread.js:168:13) |
| 26 | + at /node_modules/some-obscure-library:199:32 # this is the actual location. |
| 27 | + at Module._compile (internal/modules/cjs/loader.js:1139:30) |
| 28 | + at Module._extensions..js (internal/modules/cjs/loader.js:1159:10) |
| 29 | + at Module.load (internal/modules/cjs/loader.js:988:32) |
| 30 | + at Module._load (internal/modules/cjs/loader.js:896:14) |
| 31 | + at executeUserEntryPoint (internal/modules/run_main.js:71:12) |
| 32 | +``` |
| 33 | + |
| 34 | +With the knowledge of where the calls of `process.exit` is, we can check around |
| 35 | +the invocation to understand the reasons and prevent it from abnormal exits. |
| 36 | + |
| 37 | +If the process doesn't output a stack trace on exit, we can come to a |
| 38 | +conclusion that the process exited for other reasons, and we |
| 39 | +can deploy strategy according to the [abnormal termination guides on common symptoms]. |
| 40 | + |
| 41 | +## Useful Links |
| 42 | + |
| 43 | +- https://nodejs.org/docs/latest/api/cli.html#cli_trace_exit |
| 44 | + |
| 45 | +[abnormal termination guides on common symptoms]: ../README.md#Common_Symptoms |
0 commit comments