0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

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: https://github.com/nodejs/node/pull/31600
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Anna Henningsen 2020-02-01 13:33:29 +01:00
parent 234de6f1fd
commit c1da4e4aa4
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 24 additions and 1 deletions

View File

@ -61,7 +61,9 @@ class HandleWrap : public AsyncWrap {
static void HasRef(const v8::FunctionCallbackInfo<v8::Value>& args);
static inline bool IsAlive(const HandleWrap* wrap) {
return wrap != nullptr && wrap->state_ != kClosed;
return wrap != nullptr &&
wrap->IsDoneInitializing() &&
wrap->state_ != kClosed;
}
static inline bool HasRef(const HandleWrap* wrap) {

View File

@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const util = require('util');
const assert = require('assert');
const async_hooks = require('async_hooks');
const { MessageChannel } = require('worker_threads');
// Regression test: Inspecting a `MessagePort` object before it is finished
// constructing does not crash the process.
async_hooks.createHook({
init: common.mustCall((id, type, triggerId, resource) => {
assert.strictEqual(util.inspect(resource),
'MessagePort { active: true, refed: false }');
}, 2)
}).enable();
const { port1 } = new MessageChannel();
const inspection = util.inspect(port1);
assert(inspection.includes('active: true'));
assert(inspection.includes('refed: false'));