Skip to content

Commit 6ad8ca5

Browse files
addaleaxcodebytere
authored andcommittedFeb 17, 2020
src: do not unnecessarily re-assign uv handle data
a555be2 re-assigned `async_.data` to indicate success or failure of the constructor. As the `HandleWrap` implementation uses that field to access the `HandleWrap` instance from the libuv handle, this introduced two issues: - It implicitly assumed that casting `MessagePort*` → `void*` → `HandleWrap*` would be valid. - It made the `HandleWrap::OnClose()` function fail with a `nullptr` dereference if the constructor did fail. In particular, the second issue made test/parallel/test-worker-cleanexit-with-moduleload.js` crash at least once in CI. Since re-assigning `async_.data` isn’t actually necessary here (only a leftover from earlier versions of that commit), fix this by using a local variable instead, and add a `CHECK` that provides better error messages for this type of issue in the future. Refs: #31605 PR-URL: #31696 Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2837788 commit 6ad8ca5

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed
 

‎src/handle_wrap.cc

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ HandleWrap::HandleWrap(Environment* env,
115115

116116

117117
void HandleWrap::OnClose(uv_handle_t* handle) {
118+
CHECK_NOT_NULL(handle->data);
118119
BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
119120
wrap->Detach();
120121

‎src/node_messaging.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,9 @@ MessagePort::MessagePort(Environment* env,
487487
CHECK_EQ(uv_async_init(env->event_loop(),
488488
&async_,
489489
onmessage), 0);
490-
async_.data = nullptr; // Reset later to indicate success of the constructor.
491-
auto cleanup = OnScopeLeave([&]() {
492-
if (async_.data == nullptr) Close();
493-
});
490+
// Reset later to indicate success of the constructor.
491+
bool succeeded = false;
492+
auto cleanup = OnScopeLeave([&]() { if (!succeeded) Close(); });
494493

495494
Local<Value> fn;
496495
if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn))
@@ -507,7 +506,7 @@ MessagePort::MessagePort(Environment* env,
507506
return;
508507
emit_message_fn_.Reset(env->isolate(), emit_message_fn);
509508

510-
async_.data = static_cast<void*>(this);
509+
succeeded = true;
511510
Debug(this, "Created message port");
512511
}
513512

0 commit comments

Comments
 (0)
Please sign in to comment.