File tree 7 files changed +45
-17
lines changed
7 files changed +45
-17
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,8 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
184
184
Local<Object> domain;
185
185
bool has_domain = false ;
186
186
187
+ Environment::AsyncCallbackScope callback_scope (env ());
188
+
187
189
if (env ()->using_domains ()) {
188
190
Local<Value> domain_v = context->Get (env ()->domain_string ());
189
191
has_domain = domain_v->IsObject ();
@@ -236,7 +238,7 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
236
238
237
239
Environment::TickInfo* tick_info = env ()->tick_info ();
238
240
239
- if (tick_info-> in_tick ()) {
241
+ if (callback_scope. in_makecallback ()) {
240
242
return ret;
241
243
}
242
244
@@ -249,12 +251,8 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
249
251
return ret;
250
252
}
251
253
252
- tick_info->set_in_tick (true );
253
-
254
254
env ()->tick_callback_function ()->Call (process, 0 , nullptr );
255
255
256
- tick_info->set_in_tick (false );
257
-
258
256
if (try_catch.HasCaught ()) {
259
257
tick_info->set_last_threw (true );
260
258
return Undefined (env ()->isolate ());
Original file line number Diff line number Diff line change @@ -88,6 +88,20 @@ inline void Environment::AsyncHooks::set_enable_callbacks(uint32_t flag) {
88
88
fields_[kEnableCallbacks ] = flag;
89
89
}
90
90
91
+ inline Environment::AsyncCallbackScope::AsyncCallbackScope (Environment* env)
92
+ : env_(env) {
93
+ env_->makecallback_cntr_ ++;
94
+ }
95
+
96
+ inline Environment::AsyncCallbackScope::~AsyncCallbackScope () {
97
+ env_->makecallback_cntr_ --;
98
+ CHECK_GE (env_->makecallback_cntr_ , 0 );
99
+ }
100
+
101
+ inline bool Environment::AsyncCallbackScope::in_makecallback () {
102
+ return env_->makecallback_cntr_ > 1 ;
103
+ }
104
+
91
105
inline Environment::DomainFlag::DomainFlag () {
92
106
for (int i = 0 ; i < kFieldsCount ; ++i) fields_[i] = 0 ;
93
107
}
@@ -210,6 +224,7 @@ inline Environment::Environment(v8::Local<v8::Context> context,
210
224
using_domains_(false ),
211
225
printed_error_(false ),
212
226
trace_sync_io_(false ),
227
+ makecallback_cntr_(0 ),
213
228
async_wrap_uid_(0 ),
214
229
debugger_agent_(this ),
215
230
http_parser_buffer_(nullptr ),
Original file line number Diff line number Diff line change @@ -57,10 +57,10 @@ void Environment::PrintSyncTrace() const {
57
57
}
58
58
59
59
60
- bool Environment::KickNextTick () {
60
+ bool Environment::KickNextTick (Environment::AsyncCallbackScope* scope ) {
61
61
TickInfo* info = tick_info ();
62
62
63
- if (info-> in_tick ()) {
63
+ if (scope-> in_makecallback ()) {
64
64
return true ;
65
65
}
66
66
@@ -73,15 +73,11 @@ bool Environment::KickNextTick() {
73
73
return true ;
74
74
}
75
75
76
- info->set_in_tick (true );
77
-
78
76
// process nextTicks after call
79
77
TryCatch try_catch;
80
78
try_catch.SetVerbose (true );
81
79
tick_callback_function ()->Call (process_object (), 0 , nullptr );
82
80
83
- info->set_in_tick (false );
84
-
85
81
if (try_catch.HasCaught ()) {
86
82
info->set_last_threw (true );
87
83
return false ;
Original file line number Diff line number Diff line change @@ -294,6 +294,19 @@ class Environment {
294
294
DISALLOW_COPY_AND_ASSIGN (AsyncHooks);
295
295
};
296
296
297
+ class AsyncCallbackScope {
298
+ public:
299
+ explicit AsyncCallbackScope (Environment* env);
300
+ ~AsyncCallbackScope ();
301
+
302
+ inline bool in_makecallback ();
303
+
304
+ private:
305
+ Environment* env_;
306
+
307
+ DISALLOW_COPY_AND_ASSIGN (AsyncCallbackScope);
308
+ };
309
+
297
310
class DomainFlag {
298
311
public:
299
312
inline uint32_t * fields ();
@@ -446,7 +459,7 @@ class Environment {
446
459
447
460
inline int64_t get_async_wrap_uid ();
448
461
449
- bool KickNextTick ();
462
+ bool KickNextTick (AsyncCallbackScope* scope );
450
463
451
464
inline uint32_t * heap_statistics_buffer () const ;
452
465
inline void set_heap_statistics_buffer (uint32_t * pointer);
@@ -541,6 +554,7 @@ class Environment {
541
554
bool using_domains_;
542
555
bool printed_error_;
543
556
bool trace_sync_io_;
557
+ size_t makecallback_cntr_;
544
558
int64_t async_wrap_uid_;
545
559
debugger::Agent debugger_agent_;
546
560
Original file line number Diff line number Diff line change @@ -1132,6 +1132,8 @@ Local<Value> MakeCallback(Environment* env,
1132
1132
bool ran_init_callback = false ;
1133
1133
bool has_domain = false ;
1134
1134
1135
+ Environment::AsyncCallbackScope callback_scope (env);
1136
+
1135
1137
// TODO(trevnorris): Adding "_asyncQueue" to the "this" in the init callback
1136
1138
// is a horrible way to detect usage. Rethink how detection should happen.
1137
1139
if (recv->IsObject ()) {
@@ -1192,7 +1194,7 @@ Local<Value> MakeCallback(Environment* env,
1192
1194
}
1193
1195
}
1194
1196
1195
- if (!env->KickNextTick ())
1197
+ if (!env->KickNextTick (&callback_scope ))
1196
1198
return Undefined (env->isolate ());
1197
1199
1198
1200
return ret;
@@ -4100,7 +4102,10 @@ static void StartNodeInstance(void* arg) {
4100
4102
if (instance_data->use_debug_agent ())
4101
4103
StartDebug (env, debug_wait_connect);
4102
4104
4103
- LoadEnvironment (env);
4105
+ {
4106
+ Environment::AsyncCallbackScope callback_scope (env);
4107
+ LoadEnvironment (env);
4108
+ }
4104
4109
4105
4110
env->set_trace_sync_io (trace_sync_io);
4106
4111
Original file line number Diff line number Diff line change @@ -579,6 +579,8 @@ class Parser : public BaseObject {
579
579
if (!cb->IsFunction ())
580
580
return ;
581
581
582
+ Environment::AsyncCallbackScope callback_scope (parser->env ());
583
+
582
584
// Hooks for GetCurrentBuffer
583
585
parser->current_buffer_len_ = nread;
584
586
parser->current_buffer_data_ = buf->base ;
@@ -588,7 +590,7 @@ class Parser : public BaseObject {
588
590
parser->current_buffer_len_ = 0 ;
589
591
parser->current_buffer_data_ = nullptr ;
590
592
591
- parser->env ()->KickNextTick ();
593
+ parser->env ()->KickNextTick (&callback_scope );
592
594
}
593
595
594
596
Original file line number Diff line number Diff line change @@ -69,8 +69,6 @@ v8::Local<v8::Value> MakeCallback(Environment* env,
69
69
int argc = 0 ,
70
70
v8::Local<v8::Value>* argv = nullptr );
71
71
72
- bool KickNextTick ();
73
-
74
72
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
75
73
// Sets address and port properties on the info object and returns it.
76
74
// If |info| is omitted, a new object is returned.
You can’t perform that action at this time.
0 commit comments