inspector: load network tracking modules eagerly - #64379
Conversation
`Network.enable` is dispatched to the main thread through a V8 interrupt that can run in the middle of arbitrary JavaScript execution, including while another module is still being loaded by require(). The network tracking enable()/disable() helpers lazily require()d their modules, which pull in `inspector`, `worker_threads` and `stream`. When the interrupt landed mid-require(), require() returned a half-initialized module and threw (for example "Class extends value undefined" from internal/worker/io, or "require(...).enable is not a function"). The inspector agent treats any exception thrown while toggling network tracking as unrecoverable and aborts the whole process. Load the three network tracking modules eagerly at setup time so that enable()/disable() never call require() from an interrupt. They now run during setupNetworkInspection() at bootstrap, before the inspector can dispatch Network.enable. Fixes: nodejs#64308
|
Review requested:
|
|
@legendecas mind taking a look or kicking off CI when you have a moment? Open ~4 weeks with no CI run yet — small, tested fix for #64308. |
legendecas
left a comment
There was a problem hiding this comment.
Related to #65028, I think this does not fully fix the issue of calling into JS during an interrupt. An alternative could be loading the tracking module when --experimental-network-inspection is set at startup, and enable it immediately.
|
Thanks @legendecas. You're right: #65028 changes the async hook toggle, but not Your alternative works for me: with I can rework the PR this way, or make |
Summary
--experimental-network-inspectioncan abort the whole Node.js process when a debugger attaches. Reported in #64308.Network.enableis delivered to the main thread via a V8 interrupt that can run in the middle of arbitrary JavaScript execution — including while another module is still being loaded byrequire(). The network-trackingenable()/disable()helpers lazilyrequire()d their modules (network_http,network_http2,network_undici), which transitively pull ininspector→worker_threads→internal/worker/io→stream. When the interrupt lands mid-require(),require()returns a half-initialized module and throws — for exampleClass extends value undefinedfrominternal/worker/io, orrequire(...).enable is not a function.Agent::ToggleNetworkTracking(src/inspector_agent.cc) treats any exception thrown while toggling network tracking as unrecoverable and callsUNREACHABLE(), aborting the process.Fix
Load the three network-tracking modules eagerly at module scope, so
enable()/disable()never callrequire()from an interrupt. The requires now run duringsetupNetworkInspection()at bootstrap, before the inspector can dispatchNetwork.enable. This only affects the opt-in--experimental-network-inspectionflag, where an inspector is already active.Verification
Minimal repro from the issue:
node --inspect-wait --experimental-network-inspection -e "require('net')"then attach a debugger and send
Network.enable.Driving the real inspector path locally (attach over WebSocket,
Runtime.runIfWaitingForDebugger, thenNetwork.enablewith jitter to spray the interrupt across the require window):mainbuild: aborts (Cannot toggle network tracking→UNREACHABLE), reproduced at iteration 1Adds
test/parallel/test-inspector-network-tracking-eager-load.js, which asserts the network-tracking modules are loaded at setup and thatenable()/disable()do not lazily load any module.Note
A deeper hardening — not calling
UNREACHABLE()inToggleNetworkTrackingwhen the toggle callback throws — would defend against this class of failure at the C++ layer as well. I kept this PR to the minimal JS change that fixes the reported crash, but I'm happy to add that as a follow-up if maintainers prefer.Fixes: #64308