Skip to content

Commit 84bf609

Browse files
committed
async-wrap: don't call init callback unnecessarily
Some calls to ReqWrap would get through the initial check and allow the init callback to run, even though the callback had not been used on the parent. Fix by explicitly checking if the parent has a queue. Also change the name of the check, and internal field of AsyncHooks. Other names were confusing. PR-URL: #1614 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 2ed10f1 commit 84bf609

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/async-wrap-inl.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ inline AsyncWrap::AsyncWrap(Environment* env,
2121
// Check user controlled flag to see if the init callback should run.
2222
if (!env->using_asyncwrap())
2323
return;
24-
if (!env->call_async_init_hook() && parent == nullptr)
24+
25+
// If callback hooks have not been enabled, and there is no parent, return.
26+
if (!env->async_wrap_callbacks_enabled() && parent == nullptr)
27+
return;
28+
29+
// If callback hooks have not been enabled and parent has no queue, return.
30+
if (!env->async_wrap_callbacks_enabled() && !parent->has_async_queue())
2531
return;
2632

2733
// TODO(trevnorris): Until it's verified all passed object's are not weak,

src/env-inl.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ inline int Environment::AsyncHooks::fields_count() const {
6666
return kFieldsCount;
6767
}
6868

69-
inline bool Environment::AsyncHooks::call_init_hook() {
70-
return fields_[kCallInitHook] != 0;
69+
inline bool Environment::AsyncHooks::callbacks_enabled() {
70+
return fields_[kEnableCallbacks] != 0;
71+
}
72+
73+
inline void Environment::AsyncHooks::set_enable_callbacks(uint32_t flag) {
74+
fields_[kEnableCallbacks] = flag;
7175
}
7276

7377
inline Environment::DomainFlag::DomainFlag() {
@@ -214,9 +218,9 @@ inline v8::Isolate* Environment::isolate() const {
214218
return isolate_;
215219
}
216220

217-
inline bool Environment::call_async_init_hook() const {
221+
inline bool Environment::async_wrap_callbacks_enabled() const {
218222
// The const_cast is okay, it doesn't violate conceptual const-ness.
219-
return const_cast<Environment*>(this)->async_hooks()->call_init_hook();
223+
return const_cast<Environment*>(this)->async_hooks()->callbacks_enabled();
220224
}
221225

222226
inline bool Environment::in_domain() const {

src/env.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,16 @@ class Environment {
269269
public:
270270
inline uint32_t* fields();
271271
inline int fields_count() const;
272-
inline bool call_init_hook();
272+
inline bool callbacks_enabled();
273+
inline void set_enable_callbacks(uint32_t flag);
273274

274275
private:
275276
friend class Environment; // So we can call the constructor.
276277
inline AsyncHooks();
277278

278279
enum Fields {
279280
// Set this to not zero if the init hook should be called.
280-
kCallInitHook,
281+
kEnableCallbacks,
281282
kFieldsCount
282283
};
283284

@@ -374,7 +375,7 @@ class Environment {
374375

375376
inline v8::Isolate* isolate() const;
376377
inline uv_loop_t* event_loop() const;
377-
inline bool call_async_init_hook() const;
378+
inline bool async_wrap_callbacks_enabled() const;
378379
inline bool in_domain() const;
379380
inline uint32_t watched_providers() const;
380381

0 commit comments

Comments
 (0)