You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Connect inspector.Session to inspector in another process or thread. #28828
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.
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.
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 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 , 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.
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.
Activity
eugeneo commentedon 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.
inspector: new API - Session.connectToMainThread
inspector: new API - Session.connectToMainThread
rochdev commentedon Apr 5, 2020
@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 commentedon Apr 6, 2020
Please take a look at a inspector.Session.connectToMainThread available since 12.11.
rochdev commentedon 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 commentedon 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 commentedon Nov 20, 2020
@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 besession.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.openand 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 commentedon Jun 3, 2024
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:
I can't get a single line to log out in the debugger console of chrome.
eugeneo commentedon Jun 3, 2024
I do not have access to Node atm, but this (bit contrived) test case showcases how
connectToMainThreadis supposed to work: test-inspector-connect-main-thread.js. Specifically, focus onchildMainfunction. 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.inspector: new API - Session.connectToMainThread