Repository navigation
Closed as not planned
Description
Version
19.8.1
Platform
Darwin MacBook-Pro-2.guest.corp.microsoft.com 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64 x86_64
Subsystem
inspector
What steps will reproduce the bug?
Run the following script:
const { Session } = require('node:inspector');
const session = new Session();
const executionContexts = new Map();
session.on('Runtime.executionContextCreated', call => {
console.log(call);
executionContexts.set(call.params.context.id, call.params.context);
});
session.on('Debugger.scriptParsed', call => {
console.log(call);
if (!executionContexts.has(call.params.executionContextId)) {
console.error(`Got script in execution context ${call.params.executionContextId}, but have only ${[...executionContexts.keys()].join()}`);
process.exit(1);
}
});
session.connect();
session.post('Runtime.enable', () => {
console.log('Runtime enabled')
session.post('Debugger.enable', () => {
console.log('Debugger enabled')
});
});
How often does it reproduce? Is there a required condition?
100% of the time
What is the expected behavior? Why is that the expected behavior?
The script should run successfully. All execution contexts should come in Runtime.executionContextCreated events before Runtime.enable returns.
What do you see instead?
{
method: 'Runtime.executionContextCreated',
params: {
context: {
id: 1,
origin: '',
name: '/Users/connor/.asdf/installs/nodejs/19.8.1/bin/node[90017]',
uniqueId: '-8333824095165494686.-3638729329792817601',
auxData: [Object]
}
}
}
Runtime enabled
{
method: 'Debugger.scriptParsed',
params: {
scriptId: '6',
url: 'node:internal/per_context/primordials',
startLine: 0,
startColumn: 0,
endLine: 737,
endColumn: 0,
executionContextId: 0,
hash: 'bc07b5754e60d05f0e36aee626f46b4561cc1d342c7c84e1655eca736753f7f8',
isLiveEdit: false,
sourceMapURL: '',
hasSourceURL: false,
isModule: false,
length: 21457,
stackTrace: { callFrames: [Array] },
scriptLanguage: 'JavaScript',
embedderName: 'node:internal/per_context/primordials'
}
}
Got script in execution context 0, but have only 1
Additional information
Later scripts are in the execution context ID of 1, but earlier ones are in the non-existent 0 context.
Originally came up in vscode-js-debug as a result of adding more intended correctness around how execution contexts are handled microsoft/vscode-js-debug#1643
Activity
fix: cannot read createScope of undefined
fix: cannot read createScope of undefined (#1644)
bnoordhuis commentedon Apr 6, 2023
Pull request welcome. What probably needs to happen is a call to
env->inspector_agent()->ContextCreated()right after the V8 context is created withContext::New()orContext::FromSnapshot()so the context gets assigned an execution context ID by the V8 inspector.node::Environment::AssignToContext()usually takes care of that but it's possible the inspector agent hasn't been started yet. There's also no inspector agent when snapshotting, which may be the root cause here.See also
node::CreateEnvironment()andnode::Environment::InitializeInspector(). Grep forshould_create_inspectorandkNoCreateInspector.debadree25 commentedon Apr 12, 2023
@bnoordhuis
I think this indeed could be the issue here, I think this is a relevant comment I found while looking through the code:
node/src/api/embed_helpers.cc
Lines 180 to 183 in 67fdb74
note that running test code only shows that the code involving primordials doesnt have correct
executionId? which I believe is snapshotted while building? Seems kind of like a dead end 😅 any pointers on where else one could proceed could be helpful!Thank You!
bnoordhuis commentedon Apr 12, 2023
So, I'm not 100% sure, but I think it can be fixed by calling ContextCreated() after the snapshot has been deserialized with
Context::FromSnapshot().debadree25 commentedon Apr 13, 2023
IIUC ContextCreated() already seems to be called no?
in
node/src/api/environment.cc
Lines 452 to 469 in 67fdb74
here after creating context from snapshot we call
InitializeMainContextwhich in turn callsnode/src/env.cc
Line 818 in 67fdb74
which should call contextCreated? is there any other place we should call it again?
bnoordhuis commentedon Apr 13, 2023
It calls it too early. CreateEnvironment() calls InitializeMainContext() - which calls AssignToContext() - before the inspector agent is started.
debadree25 commentedon Apr 13, 2023
ok I tried something like this
(some couts to track things)
the output now looks like this
One more executionId in the mix somehow?
debadree25 commentedon Apr 14, 2023
So far no success, tried to debug why #47438 (comment), overall can't really understand the flow additional contexts are coming out of and somehow the original '0' executionId also comes in 😕😓
connor4312 commentedon Dec 5, 2023
I got a report of a behavior that I can't work around and may have the same root cause: #51067
deepak1556 commentedon Dec 6, 2023
Look into this today, seems the
executionContextIdof0is expected for scripts embedded in the startup snapshots. Traced it back to https://bugs.chromium.org/p/v8/issues/detail?id=6274, this is what I seeI don't see a way to associate the main context id to the snapshot scripts with whatever API is available today in V8. Seems like it needs to be a feature request in V8 ?
deepak1556 commentedon Dec 6, 2023
Also, #51067 is indeed related to the issue. Without being able to associate the main context execution id to the snapshot scripts the value of the mappings to retrieve the relevant
InspectedContextis messed, basically V8InspectorImpl::getContext does not return valid context and hence debug session breaks.deepak1556 commentedon Dec 7, 2023
Seems like the case for debugging scripts in embedded snapshots was discussed https://bugs.chromium.org/p/v8/issues/detail?id=9029 and is still an open issue. I think there is possibility to fix this in upstream.
github-actions commentedon Jun 5, 2026
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
github-actions commentedon Sep 11, 2026
This issue has been marked as stale due to 90 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
github-actions commentedon Oct 11, 2026
This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.