-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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: support extra contexts when connected #13826
Conversation
This enables inspector support for contexts created using the vm module. Fixes: nodejs#7593 Fixes: nodejs#12096 Refs: nodejs#9272
In contextCreated, V8Inspector wraps contexts with InspectedContext, which adds "global" and "console" to each context. This breaks the vm-test-basic tests and may lead to unexpected behaviour outside of inspector sessions. Calling contextCreated only if the inspector is actually connected ensures that this behaviour is limited to active inspector sessions.
Also note that due to the aforementioned GC issue in <5.9, this can't be backported prior releases without significant work (I think). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked with V8 developers, this should not work. Let me test this.
I am still debugging - looks like the contexts are never released (e.g. have you seen the contextDestroyed protocol notification?). I added print statements all over the place and they are only called for the main context when the application runs to a completion. I tried forcing GC, creating large arrays, etc. Also, this code does not seem to work with sessions created through JavaScript because the IO is never started. I don't think there's a need to maintain a separate vector of contexts - inspector already does that if you call context Created/Destroyed. |
Hmm, that's a problem. So the WeakCallback check that used to be in 5.8 but is no longer in 5.9 would have caught these. Any ideas on how to destroy these properly?
I see. I added that to avoid polluting all vm contexts with |
Ok, looks like it is actually Inspector who retains the reference to the Context. So always reporting the context actually prevents it from being garbage collected. Background info: browser has full information when the context will be destroyed (page navigation, etc). Hence it can warn the inspector to stop tracking the context which frees the reference. Unfortunately, this approach does not work with Node as it relies on contexts being GCed. |
That's the problem - to release those you need to call contextDestroyed. But Node is calling contextDestroyed from the GC callback...
I will raise this issue with the V8 developers. |
Makes sense.
OK, and as you said, for this to work we have to always report contexts (rather than only reporting when connected) to ensure sessions created from JS still work. It sounds to me like this feature depends on either upstream changes in V8 or bigger changes around Node's expectation that contexts will be GCed. Is there anything salvageable from this PR, or should we just close it? |
For your ref this is the change that made inspector retain the context - v8/v8@908cd38 Re: this PR - I would suggest you to close it but keep your branch. Thanks! |
OK, thanks for the review & background info! |
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
inspector
This makes the inspector aware of extra contexts, enabling debugging inside
vm.Script
code.It is based on @eugeneo's branch, which used to have an issue with GC crashes due to
InspectedContext
s not being destroyed in the right order, but this is no longer an issue as of V8 5.9.The original approach sent every context to the inspector whether or not it was connected, but wrapping a context in
InspectedContext
has a side-effect of attachingglobal
andconsole
instances to the context, which caused test failures. The approach I took was to only callcontextCreated
if the inspector is actually connected to limit the impact of these side effects.Fixes: #7593
Fixes: #12096
Closes: #9272