Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
everett1992 opened this issue Jul 23, 2019 · 8 comments · Fixed by #28870
Closed

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

everett1992 opened this issue Jul 23, 2019 · 8 comments · Fixed by #28870

Comments

@everett1992
Copy link
Contributor

everett1992 commented Jul 23, 2019

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.

@eugeneo
Copy link
Contributor

eugeneo commented Jul 24, 2019

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.

targos pushed a commit that referenced this issue Sep 20, 2019
This API is designed to enable worker threads use Inspector protocol
on main thread (and other workers through NodeWorker domain).

Note that worker can cause dead lock by suspending itself. I will
work on a new API that will allow workers to be hidden from the
inspector.

Fixes: #28828
PR-URL: #28870
Reviewed-By: Aleksei Koziatinskii <ak239spb@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
BridgeAR pushed a commit that referenced this issue Sep 25, 2019
This API is designed to enable worker threads use Inspector protocol
on main thread (and other workers through NodeWorker domain).

Note that worker can cause dead lock by suspending itself. I will
work on a new API that will allow workers to be hidden from the
inspector.

Fixes: #28828
PR-URL: #28870
Reviewed-By: Aleksei Koziatinskii <ak239spb@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
@rochdev
Copy link
Contributor

rochdev commented Apr 5, 2020

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?

@eugeneo
Copy link
Contributor

eugeneo commented Apr 6, 2020

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

@rochdev
Copy link
Contributor

rochdev commented Apr 6, 2020

@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.

@eugeneo
Copy link
Contributor

eugeneo commented Apr 6, 2020

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

@sliftist
Copy link

@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.

@richterdennis
Copy link

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.

@eugeneo
Copy link
Contributor

eugeneo commented Jun 3, 2024

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants