From f7f73c625ff97df85aba625f7570315aece49986 Mon Sep 17 00:00:00 2001 From: Ouyang Yadong Date: Wed, 27 Nov 2019 21:04:13 +0800 Subject: [PATCH] fix(worker): allow inspector.close() inside Worker inspector.close() is an alias of process._debugEnd() which is missing inside Worker enviroments. This PR allows calling it inside Worker. --- lib/internal/bootstrap/node.js | 2 + src/node_process_methods.cc | 2 +- test/parallel/test-worker-inspector-open.js | 60 +++++++++++++++++++ .../test-worker-unsupported-things.js | 1 - 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-worker-inspector-open.js diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index c586ddfd5e6f89..acb9b3dcaa224a 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -95,6 +95,8 @@ if (isMainThread) { process.chdir = workerThreadSetup.unavailable('process.chdir()'); process.umask = wrapped.umask; process.cwd = rawMethods.cwd; + + process._debugEnd = rawMethods._debugEnd; } // Set up methods on the process object for all threads diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 7efe8efb9b9e6d..58d3a181433d62 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -446,12 +446,12 @@ static void InitializeProcessMethods(Local target, // define various internal methods if (env->owns_process_state()) { env->SetMethod(target, "_debugProcess", DebugProcess); - env->SetMethod(target, "_debugEnd", DebugEnd); env->SetMethod(target, "abort", Abort); env->SetMethod(target, "causeSegfault", CauseSegfault); env->SetMethod(target, "chdir", Chdir); } + env->SetMethod(target, "_debugEnd", DebugEnd); env->SetMethod( target, "_startProfilerIdleNotifier", StartProfilerIdleNotifier); env->SetMethod(target, "_stopProfilerIdleNotifier", StopProfilerIdleNotifier); diff --git a/test/parallel/test-worker-inspector-open.js b/test/parallel/test-worker-inspector-open.js new file mode 100644 index 00000000000000..cb6da323606f75 --- /dev/null +++ b/test/parallel/test-worker-inspector-open.js @@ -0,0 +1,60 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); + +common.skipIfInspectorDisabled(); + +const net = require('net'); +const { Worker, isMainThread, parentPort } = require('worker_threads'); +const inspector = require('inspector'); +const url = require('url'); + +// This test ensures that inspector.open(), inspector.close(), inspector.url() +// work inside Worker. +function ping(port, callback) { + net.connect(port) + .on('connect', function() { close(this); }) + .on('error', function(err) { close(this, err); }); + + function close(self, err) { + self.end(); + self.on('close', () => callback(err)); + } +} + +function runMainThread() { + const worker = new Worker(__filename); + + worker.on('message', common.mustCall(({ ws }) => { + ping(url.parse(ws).port, common.mustCall((err) => { + assert.ifError(err); + worker.postMessage({ done: true }); + })); + })); +} + +function runChildWorkerThread() { + inspector.open(0); + assert(inspector.url()); + inspector.close(); + assert(!inspector.url()); + + inspector.open(0); + + parentPort.on('message', common.mustCall(({ done }) => { + if (done) { + parentPort.close(); + } + })); + + parentPort.postMessage({ + ws: inspector.url() + }); +} + +if (isMainThread) { + runMainThread(); +} else { + runChildWorkerThread(); +} diff --git a/test/parallel/test-worker-unsupported-things.js b/test/parallel/test-worker-unsupported-things.js index cc9eec4af67760..3ce6f29de25854 100644 --- a/test/parallel/test-worker-unsupported-things.js +++ b/test/parallel/test-worker-unsupported-things.js @@ -48,7 +48,6 @@ if (!process.env.HAS_STARTED_WORKER) { assert.strictEqual('_stopProfilerIdleNotifier' in process, false); assert.strictEqual('_debugProcess' in process, false); assert.strictEqual('_debugPause' in process, false); - assert.strictEqual('_debugEnd' in process, false); parentPort.postMessage(true); }