Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: set stack size for worker threads #26049

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class WorkerThreadData {
{
Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
isolate->SetStackLimit(w_->stack_base_);

HandleScope handle_scope(isolate);
isolate_data_.reset(CreateIsolateData(isolate,
&loop_,
Expand Down Expand Up @@ -488,8 +490,17 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
static_cast<Worker*>(handle->data)->OnThreadStopped();
}), 0);

CHECK_EQ(uv_thread_create(&w->tid_, [](void* arg) {
uv_thread_options_t thread_options;
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
thread_options.stack_size = kStackSize;
CHECK_EQ(uv_thread_create_ex(&w->tid_, &thread_options, [](void* arg) {
Worker* w = static_cast<Worker*>(arg);
const uintptr_t stack_top = reinterpret_cast<uintptr_t>(&arg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that some compilers decide to take this address from the caller thread's stack as opposed to the current thread's stack? I guess it should not, but I am not up-to-date with compiler optimizations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler doesn’t know whether there are different threads involved here. And if there’s something invalid about this, then V8 already has the same problem in their code (which I assume they don’t).


// Leave a few kilobytes just to make sure we're within limits and have
// some space to do work in C++ land.
w->stack_base_ = stack_top - (kStackSize - kStackBufferSize);

w->Run();

Mutex::ScopedLock lock(w->mutex_);
Expand Down
6 changes: 6 additions & 0 deletions src/node_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class Worker : public AsyncWrap {
bool thread_joined_ = true;
int exit_code_ = 0;
uint64_t thread_id_ = -1;
uintptr_t stack_base_;

// Full size of the thread's stack.
static constexpr size_t kStackSize = 4 * 1024 * 1024;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For platforms where 4MB is not the default stack size, does this prove as an added memory pressure, and if so, should we document this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this prove as an added memory pressure

@gireeshpunathil Not on most platforms in their default configuration, I assume – usually, memory pages are only actually committed when first accessed.

I wouldn’t explicitly document this, as it shouldn’t make much of a difference for users.

// Stack buffer size that is not available to the JS engine.
static constexpr size_t kStackBufferSize = 192 * 1024;

std::unique_ptr<MessagePortData> child_port_data_;

Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-worker-stack-overflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');

const worker = new Worker('function f() { f(); } f();', { eval: true });

worker.on('error', common.mustCall((err) => {
assert.strictEqual(err.constructor, RangeError);
assert.strictEqual(err.message, 'Maximum call stack size exceeded');
}));