File tree 3 files changed +29
-1
lines changed
3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,8 @@ class WorkerThreadData {
118
118
{
119
119
Locker locker (isolate);
120
120
Isolate::Scope isolate_scope (isolate);
121
+ isolate->SetStackLimit (w_->stack_base_ );
122
+
121
123
HandleScope handle_scope (isolate);
122
124
isolate_data_.reset (CreateIsolateData (isolate,
123
125
&loop_,
@@ -488,8 +490,17 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
488
490
static_cast <Worker*>(handle->data )->OnThreadStopped ();
489
491
}), 0 );
490
492
491
- CHECK_EQ (uv_thread_create (&w->tid_ , [](void * arg) {
493
+ uv_thread_options_t thread_options;
494
+ thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
495
+ thread_options.stack_size = kStackSize ;
496
+ CHECK_EQ (uv_thread_create_ex (&w->tid_ , &thread_options, [](void * arg) {
492
497
Worker* w = static_cast <Worker*>(arg);
498
+ const uintptr_t stack_top = reinterpret_cast <uintptr_t >(&arg);
499
+
500
+ // Leave a few kilobytes just to make sure we're within limits and have
501
+ // some space to do work in C++ land.
502
+ w->stack_base_ = stack_top - (kStackSize - kStackBufferSize );
503
+
493
504
w->Run ();
494
505
495
506
Mutex::ScopedLock lock (w->mutex_ );
Original file line number Diff line number Diff line change @@ -80,6 +80,12 @@ class Worker : public AsyncWrap {
80
80
bool thread_joined_ = true ;
81
81
int exit_code_ = 0 ;
82
82
uint64_t thread_id_ = -1 ;
83
+ uintptr_t stack_base_;
84
+
85
+ // Full size of the thread's stack.
86
+ static constexpr size_t kStackSize = 4 * 1024 * 1024 ;
87
+ // Stack buffer size that is not available to the JS engine.
88
+ static constexpr size_t kStackBufferSize = 192 * 1024 ;
83
89
84
90
std::unique_ptr<MessagePortData> child_port_data_;
85
91
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const common = require ( '../common' ) ;
3
+ const assert = require ( 'assert' ) ;
4
+ const { Worker } = require ( 'worker_threads' ) ;
5
+
6
+ const worker = new Worker ( 'function f() { f(); } f();' , { eval : true } ) ;
7
+
8
+ worker . on ( 'error' , common . mustCall ( ( err ) => {
9
+ assert . strictEqual ( err . constructor , RangeError ) ;
10
+ assert . strictEqual ( err . message , 'Maximum call stack size exceeded' ) ;
11
+ } ) ) ;
You can’t perform that action at this time.
0 commit comments