Skip to content

Commit 2cdd57a

Browse files
addaleaxcodebytere
authored andcommitted
src: fix inspecting MessagePort from init async hook
During the `init()` async hook, the C++ object is not finished creating yet (i.e. it is an `AsyncWrap`, but not yet a `HandleWrap` or `MessagePort`). Accessing the `handle_` field is not valid in that case. However, the custom inspect function for `MessagePort`s calls `HasRef()` on the object, which would crash when the object is not fully constructed. Fix that by guarding the access of the libuv handle on that condition. PR-URL: #31600 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 753db6a commit 2cdd57a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

‎src/handle_wrap.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ class HandleWrap : public AsyncWrap{
6161
staticvoidHasRef(const v8::FunctionCallbackInfo<v8::Value>& args);
6262

6363
staticinlineboolIsAlive(const HandleWrap* wrap){
64-
return wrap != nullptr && wrap->state_ != kClosed;
64+
return wrap != nullptr &&
65+
wrap->IsDoneInitializing() &&
66+
wrap->state_ != kClosed;
6567
}
6668

6769
staticinlineboolHasRef(const HandleWrap* wrap){
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constutil=require('util');
4+
constassert=require('assert');
5+
constasync_hooks=require('async_hooks');
6+
const{ MessageChannel }=require('worker_threads');
7+
8+
// Regression test: Inspecting a `MessagePort` object before it is finished
9+
// constructing does not crash the process.
10+
11+
async_hooks.createHook({
12+
init: common.mustCall((id,type,triggerId,resource)=>{
13+
assert.strictEqual(util.inspect(resource),
14+
'MessagePort{active: true, refed: false }');
15+
},2)
16+
}).enable();
17+
18+
const{ port1 }=newMessageChannel();
19+
constinspection=util.inspect(port1);
20+
assert(inspection.includes('active: true'));
21+
assert(inspection.includes('refed: false'));

0 commit comments

Comments
(0)