Skip to content

Commit f938cbd

Browse files
committed
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 0ac04ec commit f938cbd

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/handle_wrap.cc

Lines changed: 1 addition & 0 deletions
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

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

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

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

0 commit comments

Comments
 (0)