Skip to content

Commit ef85bd1

Browse files
committed
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: nodejs#33084 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 11bc30d commit ef85bd1

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/node_worker.cc

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ class WorkerThreadData {
131131
if (ret != 0) {
132132
char err_buf[128];
133133
uv_err_name_r(ret, err_buf, sizeof(err_buf));
134-
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
135-
w->custom_error_str_ = err_buf;
136-
w->stopped_ = true;
134+
w->Exit(1, "ERR_WORKER_INIT_FAILED", err_buf);
137135
return;
138136
}
139137
loop_init_failed_ = false;
@@ -148,9 +146,9 @@ class WorkerThreadData {
148146

149147
Isolate* isolate = Isolate::Allocate();
150148
if (isolate == nullptr) {
151-
w->custom_error_ = "ERR_WORKER_OUT_OF_MEMORY";
152-
w->custom_error_str_ = "Failed to create new Isolate";
153-
w->stopped_ = true;
149+
// TODO(addaleax): This should be ERR_WORKER_INIT_FAILED,
150+
// ERR_WORKER_OUT_OF_MEMORY is for reaching the per-Worker heap limit.
151+
w->Exit(1, "ERR_WORKER_OUT_OF_MEMORY", "Failed to create new Isolate");
154152
return;
155153
}
156154

@@ -233,9 +231,7 @@ class WorkerThreadData {
233231
size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit,
234232
size_t initial_heap_limit) {
235233
Worker* worker = static_cast<Worker*>(data);
236-
worker->custom_error_ = "ERR_WORKER_OUT_OF_MEMORY";
237-
worker->custom_error_str_ = "JS heap out of memory";
238-
worker->Exit(1);
234+
worker->Exit(1, "ERR_WORKER_OUT_OF_MEMORY", "JS heap out of memory");
239235
// Give the current GC some extra leeway to let it finish rather than
240236
// crash hard. We are not going to perform further allocations anyway.
241237
constexpr size_t kExtraHeapAllowance = 16 * 1024 * 1024;
@@ -292,8 +288,9 @@ void Worker::Run() {
292288
TryCatch try_catch(isolate_);
293289
context = NewContext(isolate_);
294290
if (context.IsEmpty()) {
295-
custom_error_ = "ERR_WORKER_OUT_OF_MEMORY";
296-
custom_error_str_ = "Failed to create new Context";
291+
// TODO(addaleax): This should be ERR_WORKER_INIT_FAILED,
292+
// ERR_WORKER_OUT_OF_MEMORY is for reaching the per-Worker heap limit.
293+
Exit(1, "ERR_WORKER_OUT_OF_MEMORY", "Failed to create new Context");
297294
return;
298295
}
299296
}
@@ -682,9 +679,16 @@ Local<Float64Array> Worker::GetResourceLimits(Isolate* isolate) const {
682679
return Float64Array::New(ab, 0, kTotalResourceLimitCount);
683680
}
684681

685-
void Worker::Exit(int code) {
682+
void Worker::Exit(int code, const char* error_code, const char* error_message) {
686683
Mutex::ScopedLock lock(mutex_);
687-
Debug(this, "Worker %llu called Exit(%d)", thread_id_.id, code);
684+
Debug(this, "Worker %llu called Exit(%d, %s, %s)",
685+
thread_id_.id, code, error_code, error_message);
686+
687+
if (error_code != nullptr) {
688+
custom_error_ = error_code;
689+
custom_error_str_ = error_message;
690+
}
691+
688692
if (env_ != nullptr) {
689693
exit_code_ = code;
690694
Stop(env_);

src/node_worker.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ class Worker : public AsyncWrap {
3434
void Run();
3535

3636
// Forcibly exit the thread with a specified exit code. This may be called
37-
// from any thread.
38-
void Exit(int code);
37+
// from any thread. `error_code` and `error_message` can be used to create
38+
// a custom `'error'` event before emitting `'exit'`.
39+
void Exit(int code,
40+
const char* error_code = nullptr,
41+
const char* error_message = nullptr);
3942

4043
// Wait for the worker thread to stop (in a blocking manner).
4144
void JoinThread();

0 commit comments

Comments
 (0)