镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Connect inspector.Session to inspector in another process or thread. #28828

Description

@everett1992

Is your feature request related to a problem? Please describe.
I want to use the inspector module in a worker thread to control the inspector in another thread.

const { isMainThread, Worker } = require('worker_threads');
const inspector = require('inspector');

if (isMainThread) {
  new Worker(__filename)
} else {
  // session connects to the worker thread inspector, not the main thread.
  const session = new inspector.Session();
  session.connect();
}

inspector.Session doesn't support connecting to the inspector of a different thread or process.

Describe the solution you'd like

  • add a url parameter to inspector.Session
if (isMainThread) {
  inspector.open(0);
  const url = inspector.url();
  new Worker(__filename, { workerData: { url }})
} else {
  const session = new inspector.Session(workerData.url);
  // ...
}
  • or allow inspector to be transfered
if (isMainThread) {
  new Worker(__filename, { workerData: { inspector }})
} else {
  const session = new new workerData.inspector.Session();
  // ...
}

Describe alternatives you've considered
The v8 inspector API is not complicated and I have written a client using the ws npm module instead of using inspector.Session.


I'm open to writing a CR to implement this, but I would like feedback from a maintainer before I start, especially because both worker_threads and inspector are experimental.

Activity

  1. eugeneo commented on Jul 24, 2019

    @eugeneo
    Contributor

    You should be able to connect to main Inspector service from a worker over the WS 😀

    I tried implementing passing inspector session from main thread to a worker, but currently it is bit involved... I plan to revisit it at some point. This would resolve many issues with the current inspector session JS API - e.g. could be implemented with promises, could do IO, etc. It may even allow us to reimplement WS server in JS instead of current C++ code.

  2. rochdev commented on Apr 5, 2020

    @rochdev
    Contributor

    You should be able to connect to main Inspector service from a worker over the WS

    @eugeneo Could you explain how since this is not documented in the inspector session API and there appears to be no way to configure it to connect to a different process?

  3. eugeneo commented on Apr 6, 2020

    @eugeneo
    Contributor

    Please take a look at a inspector.Session.connectToMainThread available since 12.11.

  4. rochdev commented on Apr 6, 2020

    @rochdev
    Contributor

    @eugeneo This only works for the very specific use case of using the main thread inspector from a worker thread. It doesn't allow connecting to other threads or a different process. Are there any plans for these to be supported? Having to use raw websockets is not very intuitive.

  5. eugeneo commented on Apr 6, 2020

    @eugeneo
    Contributor

    You can connect to other worker threads by using the target domain. You can only debug other processes by using the WebSocket interface.

  6. sliftist commented on Nov 20, 2020

    @sliftist

    @eugeneo , I am running into the same issue as @rochdev , but as this ticket is closed I'm assuming I am just misunderstanding the solution.

    I wish the use the inspector on other worker threads (not only the main thread). It seems like session.connectToMainThread() should be session.connectToThread(threadId: number). Alternatively, inspector.openOnThread(threadId: number, port, host, wait) would accomplish the same result for me.

    Presently my approach is to change all of my entry points to call inspector.open and then send the result to a dedicated debugger thread. This is problematic as it requires me to be able to change all of the entry points of worker threads I wish to debug.

    As worker threads become more common it is more and more likely to encounter libraries which use worker threads, in which case I will not reasonably be able to change their entry points.

  7. richterdennis commented on Jun 3, 2024

    @richterdennis

    5 years later nothing changed I guess. I am looking for a solution to use the inspector console of the main thread and I cant get it working.

    I tried multiple versions of this code inside a worker:

    import inspector from 'node:inspector';
    
    const session = new inspector.Session();
    session.connectToMainThread();
    
    inspector.console.log('Test1');
    process.stdout.write('Test2');
    console.log('Test3');

    I can't get a single line to log out in the debugger console of chrome.

  8. eugeneo commented on Jun 3, 2024

    @eugeneo
    Contributor

    I do not have access to Node atm, but this (bit contrived) test case showcases how connectToMainThread is supposed to work: test-inspector-connect-main-thread.js. Specifically, focus on childMain function. It verifies that only one session can be connected (ignore that in your code) and then sets a breakpoint and makes the main thread hit it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions