From f6ec81dc0516446da8c3d16e1126f29e4ab40e9d 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 1602faf1bd66fa..b62bdea9c5e5e1 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -123,9 +123,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(); +}