Skip to content

Commit 9c274e3

Browse files
lanceevanlucas
authored andcommitted
doc: Warn against uncaughtException dependency.
State in the documentation that `uncaughtException` is not a reliable way to restart a crashed application, and clarify that an application may crash in ways that do not trigger this event. Use a documented synchronous function in example code. Fixes: #6223 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> PR-URL: #6378
1 parent fc4df0d commit 9c274e3

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

doc/api/process.md

+17-12
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ most convenient for scripts).
149149
added: v0.1.18
150150
-->
151151

152-
The `'uncaughtException'` event is emitted when an exception bubbles all the
153-
way back to the event loop. By default, Node.js handles such exceptions by
154-
printing the stack trace to `stderr` and exiting. Adding a handler for the
155-
`'uncaughtException'` event overrides this default behavior.
152+
The `'uncaughtException'` event is emitted when an uncaught JavaScript
153+
exception bubbles all the way back to the event loop. By default, Node.js
154+
handles such exceptions by printing the stack trace to `stderr` and exiting.
155+
Adding a handler for the `'uncaughtException'` event overrides this default
156+
behavior.
156157

157158
The listener function is called with the `Error` object passed as the only
158159
argument.
@@ -161,7 +162,7 @@ For example:
161162

162163
```js
163164
process.on('uncaughtException', (err) => {
164-
console.log(`Caught exception: ${err}`);
165+
fs.writeSync(1, `Caught exception: ${err}`);
165166
});
166167

167168
setTimeout(() => {
@@ -192,20 +193,24 @@ times nothing happens - but the 10th time, the system becomes corrupted.
192193

193194
The correct use of `'uncaughtException'` is to perform synchronous cleanup
194195
of allocated resources (e.g. file descriptors, handles, etc) before shutting
195-
down the process. It is not safe to resume normal operation after
196-
`'uncaughtException'`.
196+
down the process. **It is not safe to resume normal operation after
197+
`'uncaughtException'`.**
198+
199+
To restart a crashed application in a more reliable way, whether `uncaughtException`
200+
is emitted or not, an external monitor should be employed in a separate process
201+
to detect application failures and recover or restart as needed.
197202

198203
### Event: 'unhandledRejection'
199204
<!-- YAML
200205
added: v1.4.1
201206
-->
202207

203208
The `'unhandledRejection`' event is emitted whenever a `Promise` is rejected and
204-
no error handler is attached to the promise within a turn of the event loop.
209+
no error handler is attached to the promise within a turn of the event loop.
205210
When programming with Promises, exceptions are encapsulated as "rejected
206211
promises". Rejections can be caught and handled using [`promise.catch()`][] and
207212
are propagated through a `Promise` chain. The `'unhandledRejection'` event is
208-
useful for detecting and keeping track of promises that were rejected whose
213+
useful for detecting and keeping track of promises that were rejected whose
209214
rejections have not yet been handled.
210215

211216
The listener function is called with the following arguments:
@@ -637,7 +642,7 @@ An example of this object looks like:
637642
SHLVL: '1',
638643
HOME: '/Users/maciej',
639644
LOGNAME: 'maciej',
640-
_: '/usr/local/bin/node'
645+
_: '/usr/local/bin/node'
641646
}
642647
```
643648

@@ -1108,10 +1113,10 @@ console.log(util.inspect(process.memoryUsage()));
11081113
Will generate:
11091114

11101115
```js
1111-
{
1116+
{
11121117
rss: 4935680,
11131118
heapTotal: 1826816,
1114-
heapUsed: 650472
1119+
heapUsed: 650472
11151120
}
11161121
```
11171122

0 commit comments

Comments
 (0)