-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
24 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
test/parallel/test-worker-message-port-inspect-during-init-hook.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, port2 } = new MessageChannel(); | ||
const inspection = util.inspect(port1); | ||
assert(inspection.includes('active: true')); | ||
assert(inspection.includes('refed: false')); |