mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 18:15:18 +00:00

Main thread (the one that WS endpoint connects to) should be able to report all workers. PR-URL: https://github.com/nodejs/node/pull/28872 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
64 lines
2.0 KiB
JavaScript
64 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
common.skipIfInspectorDisabled();
|
|
common.skipIfWorker();
|
|
|
|
const assert = require('assert');
|
|
const { Worker } = require('worker_threads');
|
|
const { Session } = require('inspector');
|
|
|
|
const session = new Session();
|
|
|
|
let done = false;
|
|
|
|
function onAttachToWorker({ params: { sessionId } }) {
|
|
let id = 1;
|
|
function postToWorkerInspector(method, params) {
|
|
session.post('NodeWorker.sendMessageToWorker', {
|
|
sessionId,
|
|
message: JSON.stringify({ id: id++, method, params })
|
|
}, () => console.log(`Message ${method} received the response`));
|
|
}
|
|
|
|
// Wait for the notification
|
|
function onMessageReceived({ params: { message } }) {
|
|
if (!message ||
|
|
JSON.parse(message).method !== 'NodeRuntime.waitingForDisconnect') {
|
|
session.once('NodeWorker.receivedMessageFromWorker', onMessageReceived);
|
|
return;
|
|
}
|
|
// Force a call to node::inspector::Agent::ToggleAsyncHook by changing the
|
|
// async call stack depth
|
|
postToWorkerInspector('Debugger.setAsyncCallStackDepth', { maxDepth: 1 });
|
|
// This is were the original crash happened
|
|
session.post('NodeWorker.detach', { sessionId }, () => {
|
|
done = true;
|
|
});
|
|
}
|
|
|
|
onMessageReceived({ params: { message: null } });
|
|
// Enable the debugger, otherwise setAsyncCallStackDepth does nothing
|
|
postToWorkerInspector('Debugger.enable');
|
|
// Start waiting for disconnect notification
|
|
postToWorkerInspector('NodeRuntime.notifyWhenWaitingForDisconnect',
|
|
{ enabled: true });
|
|
// start worker
|
|
postToWorkerInspector('Runtime.runIfWaitingForDebugger');
|
|
}
|
|
|
|
session.connect();
|
|
|
|
session.on('NodeWorker.attachedToWorker', common.mustCall(onAttachToWorker));
|
|
|
|
session.post('NodeWorker.enable', { waitForDebuggerOnStart: true }, () => {
|
|
new Worker('console.log("Worker is done")', { eval: true })
|
|
.once('exit', () => {
|
|
setTimeout(() => {
|
|
assert.strictEqual(done, true);
|
|
console.log('Test is done');
|
|
}, 0);
|
|
});
|
|
});
|