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

inspector: add inspector.waitForConnection method #22867

Closed
wants to merge 1 commit into from
Closed

inspector: add inspector.waitForConnection method #22867

wants to merge 1 commit into from

Conversation

alexkozy
Copy link
Member

@alexkozy alexkozy commented Sep 14, 2018

This method might be useful in case when we need to do something
with inspector.url() before waiting for connection, e.g. save it
to file.

With this method we can do something like:

inspector.open();
fs.writeFileSync(fileName, inspector.url());
inspector.waitForConnection();
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

This method might be useful in case when we need to do something
with `inspector.url()` before waiting for connection, e.g. save it
to file.
With this method we can do something like:
```
inspector.open();
fs.writeFileSync('abc', inspector.url());
inspector.waitForConnection();
```
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Sep 14, 2018
@alexkozy
Copy link
Member Author

@eugeneo please take a look.

@alexkozy alexkozy added inspector Issues and PRs related to the V8 inspector protocol and removed c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Sep 14, 2018
Copy link
Contributor

@eugeneo eugeneo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not believe this should be a public API (and everthing in inspector module is public API) as it has a high chance of causing deadlocks (it prevents the event loop from pumping both native and V8 events). I just found out it is called for open - I would consider that a bug as well...

I believe this should be asynchronous.

@alexkozy
Copy link
Member Author

Blocking here is exactly what I expect. My use case is:

  1. call inspector.open to start inspector.
  2. get inspector.url() and report it to protocol client.
  3. wait until this client is connected to current Node.

It is emulation of --inspect-brk flag using inspector API. I think that as long as we have --inspect-brk flag, there is nothing bad in having the same feature as part of inspector API.

Alternative might be way to pass to node additional flag next to --inspect-brk that will force inspector to report inspector.url() in some way but it will be less flexible.

@eugeneo
Copy link
Contributor

eugeneo commented Sep 14, 2018

Waiting happens before JS begins execution in the case of --inspector-brk...

Is it not possible to report URL before inspector.open? If the issue is with the GUID string generation, then it may be generated beforehand and returned by inspector API.

@alexkozy
Copy link
Member Author

alexkozy commented Sep 14, 2018

Is it possible to return full url including port and guid? It will work for my use case. I will try.

@eugeneo
Copy link
Contributor

eugeneo commented Sep 15, 2018

IP address and/or port may not be available until the server socket binds if either of them is automatic (i.e. IP is set to 0.0.0.0 or ::0 and/or port is set to 0).

@alexkozy
Copy link
Member Author

Unfortunately I start node with port 0 and let it detect port by itself. It sounds like I can not get full url until inspector.open is called, I think I have two options: consider adding another --inspector-something flag and if it is set - stored url somewhere, or merge this PR. Wdyt?

@eugeneo
Copy link
Contributor

eugeneo commented Sep 17, 2018

Can't you just parse stderr since you seem to have access to the command line already (as you can pass the command line parameter)?

@alexkozy
Copy link
Member Author

I pass arguments to child processes using NODE_OPTIONS, so parent process might mute or process stderr of child process by itself. At the same time I would like to be able to connect to any node process that started with environment that includes NODE_OPTIONS, in this case I don't have access to stderr stream.

@@ -49,6 +49,13 @@ and flow control has been passed to the debugger client.

Return the URL of the active inspector, or `undefined` if there is none.

## inspector.waitForConnection()

This method blocks until first client has connection, inspector should be open
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reading of this patch is that this will wait until:

  1. A connection was made on a WS endpoint.
  2. Runtime.runIfWaitingForDebugger command was issued

runIfWaitingForDebugger should be mentioned here to reduce confusion

Environment* env = Environment::GetCurrent(args);
Agent* agent = env->inspector_agent();
if (!agent->IsActive()) {
env->ThrowError("inspector error, inspector.open should be called first");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this check be made from JavaScript code? I believe the guideline was not to throw exceptions from the native code.

@@ -833,7 +833,8 @@ bool Agent::IsActive() {

void Agent::WaitForConnect() {
CHECK_NOT_NULL(client_);
client_->waitForFrontend();
if (!client_->hasConnectedSessions())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this change is needed. If I understand correctly, the goal here is to wait for a specific frontend to connect. This check will subvert this expectation.

@alexkozy
Copy link
Member Author

Unfortunately I lost original branch, so I open another PR and addressed all your comments there, please take a look: #28453

@alexkozy alexkozy closed this Jun 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inspector Issues and PRs related to the V8 inspector protocol
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants