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