From 6099d2d08b3096ac936fa8ac1f969e5eff8fef05 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Wed, 15 Mar 2023 07:27:27 +0800 Subject: [PATCH] inspector: fix session.disconnect crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/46942 Reviewed-By: Juan José Arboleda Reviewed-By: Anna Henningsen --- src/inspector_js_api.cc | 4 +++- .../test-inspector-connect-to-main-thread.js | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-inspector-connect-to-main-thread.js diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index ec42b1a43bcc3c..82c036449e7c6f 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -122,9 +122,11 @@ class JSBindingsConnection : public AsyncWrap { new JSBindingsConnection(env, info.This(), callback); } + // See https://github.com/nodejs/node/pull/46942 void Disconnect() { + BaseObjectPtr strong_ref{this}; session_.reset(); - delete this; + Detach(); } static void Disconnect(const FunctionCallbackInfo& info) { diff --git a/test/parallel/test-inspector-connect-to-main-thread.js b/test/parallel/test-inspector-connect-to-main-thread.js new file mode 100644 index 00000000000000..7254145a2733f0 --- /dev/null +++ b/test/parallel/test-inspector-connect-to-main-thread.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); + +common.skipIfInspectorDisabled(); + +const { Session } = require('inspector'); +const { Worker, isMainThread, workerData } = require('worker_threads'); + +if (!workerData) { + common.skipIfWorker(); +} + +if (isMainThread) { + new Worker(__filename, { workerData: {} }); +} else { + const session = new Session(); + session.connectToMainThread(); + // Do not crash + session.disconnect(); +}