|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +common.skipIfInspectorDisabled(); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const { Worker } = require('worker_threads'); |
| 9 | +const { Session } = require('inspector'); |
| 10 | + |
| 11 | +const session = new Session(); |
| 12 | + |
| 13 | +let done = false; |
| 14 | + |
| 15 | +session.connect(); |
| 16 | + |
| 17 | +session.on('NodeWorker.attachedToWorker', ({ params: { sessionId } }) => { |
| 18 | + let id = 1; |
| 19 | + function postToWorkerInspector(method, params) { |
| 20 | + session.post('NodeWorker.sendMessageToWorker', { |
| 21 | + sessionId, |
| 22 | + message: JSON.stringify({ id: id++, method, params }) |
| 23 | + }, () => console.log(`Message ${method} received the response`)); |
| 24 | + } |
| 25 | + |
| 26 | + // Wait for the notification |
| 27 | + function onMessageReceived({ params: { message } }) { |
| 28 | + if (!message || |
| 29 | + JSON.parse(message).method !== 'NodeRuntime.waitingForDisconnect') { |
| 30 | + session.once('NodeWorker.receivedMessageFromWorker', onMessageReceived); |
| 31 | + return; |
| 32 | + } |
| 33 | + // Force a call to node::inspector::Agent::ToggleAsyncHook by changing the |
| 34 | + // async call stack depth |
| 35 | + postToWorkerInspector('Debugger.setAsyncCallStackDepth', { maxDepth: 1 }); |
| 36 | + // This is were the original crash happened |
| 37 | + session.post('NodeWorker.detach', { sessionId }, () => { |
| 38 | + done = true; |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + onMessageReceived({ params: { message: null } }); |
| 43 | + // Enable the debugger, otherwise setAsyncCallStackDepth does nothing |
| 44 | + postToWorkerInspector('Debugger.enable'); |
| 45 | + // Start waiting for disconnect notification |
| 46 | + postToWorkerInspector('NodeRuntime.notifyWhenWaitingForDisconnect', |
| 47 | + { enabled: true }); |
| 48 | + // start worker |
| 49 | + postToWorkerInspector('Runtime.runIfWaitingForDebugger'); |
| 50 | +}); |
| 51 | + |
| 52 | +session.post('NodeWorker.enable', { waitForDebuggerOnStart: true }, () => { |
| 53 | + new Worker('console.log("Worker is done")', { eval: true }) |
| 54 | + .once('exit', () => { |
| 55 | + setTimeout(() => { |
| 56 | + assert.strictEqual(done, true); |
| 57 | + console.log('Test is done'); |
| 58 | + }, 0); |
| 59 | + }); |
| 60 | +}); |
0 commit comments