Skip to content

Commit 38de40a

Browse files
addaleaxtargos
authored andcommitted
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 2fc72ca commit 38de40a

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
@@ -515,10 +515,9 @@ MessagePort::MessagePort(Environment* env,
515515
CHECK_EQ(uv_async_init(env->event_loop(),
516516
&async_,
517517
onmessage), 0);
518-
async_.data = nullptr; // Reset later to indicate success of the constructor.
519-
auto cleanup = OnScopeLeave([&]() {
520-
if (async_.data == nullptr) Close();
521-
});
518+
// Reset later to indicate success of the constructor.
519+
bool succeeded = false;
520+
auto cleanup = OnScopeLeave([&]() { if (!succeeded) Close(); });
522521

523522
Local<Value> fn;
524523
if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn))
@@ -535,7 +534,7 @@ MessagePort::MessagePort(Environment* env,
535534
return;
536535
emit_message_fn_.Reset(env->isolate(), emit_message_fn);
537536

538-
async_.data = static_cast<void*>(this);
537+
succeeded = true;
539538
Debug(this, "Created message port");
540539
}
541540

0 commit comments

Comments
 (0)