Skip to content

Commit 2efa530

Browse files
author
lihao25
committed
worker: unify custom error creation nodejs#33084
1 parent 8a072a9 commit 2efa530

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

src/node_worker.cc

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ class WorkerThreadData {
137137
if (ret != 0) {
138138
char err_buf[128];
139139
uv_err_name_r(ret, err_buf, sizeof(err_buf));
140-
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
141-
w->custom_error_str_ = err_buf;
142-
w->stopped_ = true;
140+
w->Exit(1, "ERR_WORKER_INIT_FAILED", err_buf);
143141
return;
144142
}
145143
loop_init_failed_ = false;
@@ -154,9 +152,9 @@ class WorkerThreadData {
154152

155153
Isolate* isolate = Isolate::Allocate();
156154
if (isolate == nullptr) {
157-
w->custom_error_ = "ERR_WORKER_OUT_OF_MEMORY";
158-
w->custom_error_str_ = "Failed to create new Isolate";
159-
w->stopped_ = true;
155+
// TODO(addaleax): This should be ERR_WORKER_INIT_FAILED,
156+
// ERR_WORKER_OUT_OF_MEMORY is for reaching the per-Worker heap limit.
157+
w->Exit(1, "ERR_WORKER_OUT_OF_MEMORY", "Failed to create new Isolate");
160158
return;
161159
}
162160

@@ -239,9 +237,7 @@ class WorkerThreadData {
239237
size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit,
240238
size_t initial_heap_limit) {
241239
Worker* worker = static_cast<Worker*>(data);
242-
worker->custom_error_ = "ERR_WORKER_OUT_OF_MEMORY";
243-
worker->custom_error_str_ = "JS heap out of memory";
244-
worker->Exit(1);
240+
worker->Exit(1, "ERR_WORKER_OUT_OF_MEMORY", "JS heap out of memory");
245241
// Give the current GC some extra leeway to let it finish rather than
246242
// crash hard. We are not going to perform further allocations anyway.
247243
constexpr size_t kExtraHeapAllowance = 16 * 1024 * 1024;
@@ -306,8 +302,9 @@ void Worker::Run() {
306302
TryCatch try_catch(isolate_);
307303
context = NewContext(isolate_);
308304
if (context.IsEmpty()) {
309-
custom_error_ = "ERR_WORKER_OUT_OF_MEMORY";
310-
custom_error_str_ = "Failed to create new Context";
305+
// TODO(addaleax): This should be ERR_WORKER_INIT_FAILED,
306+
// ERR_WORKER_OUT_OF_MEMORY is for reaching the per-Worker heap limit.
307+
Exit(1, "ERR_WORKER_OUT_OF_MEMORY", "Failed to create new Context");
311308
return;
312309
}
313310
}
@@ -702,9 +699,16 @@ Local<Float64Array> Worker::GetResourceLimits(Isolate* isolate) const {
702699
return Float64Array::New(ab, 0, kTotalResourceLimitCount);
703700
}
704701

705-
void Worker::Exit(int code) {
702+
void Worker::Exit(int code, const char* error_code, const char* error_message) {
706703
Mutex::ScopedLock lock(mutex_);
707-
Debug(this, "Worker %llu called Exit(%d)", thread_id_, code);
704+
Debug(this, "Worker %llu called Exit(%d, %s, %s)",
705+
thread_id_.id, code, error_code, error_message);
706+
707+
if (error_code != nullptr) {
708+
custom_error_ = error_code;
709+
custom_error_str_ = error_message;
710+
}
711+
708712
if (env_ != nullptr) {
709713
exit_code_ = code;
710714
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)