worker: unify custom error creation

Mostly, this introduces a pattern that makes sure that if a custom
error is reported, `stopped_` will be set to `true` correctly in
every cast, which was previously missing for the
`NewContext().IsEmpty()` case (which led to a hard crash from the
`Worker` destructor).

This also leaves TODO comments for a few cases in which
`ERR_WORKER_OUT_OF_MEMORY` was not used in accordance with the
documentation for that error code (or according to its intention).
Fixing that is semver-major.

PR-URL: https://github.com/nodejs/node/pull/33084
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen committed 2020-04-29 05:12:48 +02:00
1 parent 11bc30d5cc
commit ef85bd1908
2 files changed
+22 -15

No files matched your search

+5 -2
View File
@@ -34,8 +34,11 @@ class Worker : public AsyncWrap {
void Run();
// Forcibly exit the thread with a specified exit code. This may be called
// from any thread.
void Exit(int code);
// from any thread. `error_code` and `error_message` can be used to create
// a custom `'error'` event before emitting `'exit'`.
void Exit(int code,
const char* error_code = nullptr,
const char* error_message = nullptr);
// Wait for the worker thread to stop (in a blocking manner).
void JoinThread();