inspector: avoid more JS calls from V8 interrupts #66740
Review requested:
@legendecas @joyeecheung since you reviewed #65397, could you take a look at the re-land when you get a chance? It also needs a node-test-commit-v8-linux run, since that is what caught the futex failure last time. Thanks!
Codecov Report
:x: Patch coverage is
69.44444% with 66 lines in your changes missing coverage. Please review.:white_check_mark: Project coverage is 90.60%. Comparing base (
f2d9356) to head (e466bae).:warning: Report is 1 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/inspector\_agent.cc | 52.94% | 24 Missing and 8 partials :warning: |
| src/node\_webstorage.cc | 70.66% | 14 Missing and 8 partials :warning: |
| src/inspector\_js\_api.cc | 75.00% | 3 Missing and 3 partials :warning: |
| src/inspector/dom\_storage\_agent.cc | 83.87% | 2 Missing and 3 partials :warning: |
| src/node\_options.cc | 83.33% | 0 Missing and 1 partial :warning: |
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #66740 +/- ##
==========================================
- Coverage 92.96% 90.60% -2.36%
==========================================
Files 432 802 +370
Lines 201044 286752 +85708
Branches 30702 55380 +24678
==========================================
+ Hits 186900 259812 +72912
- Misses 13816 17044 +3228
- Partials 328 9896 +9568
| Files with missing lines | Coverage Δ | |
|---|---|---|
| lib/internal/inspector/webstorage.js | 100.00% <100.00%> (ø) | |
| src/inspector\_agent.h | 100.00% <100.00%> (ø) | |
| src/node\_webstorage.h | 87.50% <100.00%> (ø) | |
| src/path.cc | 65.04% <100.00%> (ø) | |
| src/path.h | 100.00% <ø> (ø) | |
| src/node\_options.cc | 88.21% <83.33%> (ø) | |
| src/inspector/dom\_storage\_agent.cc | 84.61% <83.87%> (ø) | |
| src/inspector\_js\_api.cc | 84.26% <75.00%> (ø) | |
| src/node\_webstorage.cc | 77.61% <70.66%> (ø) | |
| src/inspector\_agent.cc | 84.30% <52.94%> (ø) | |
... and 497 files with indirect coverage changes
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Oh another case found,
1. DOMStorageAgent::getWebStorage() (#62145) looks up globalThis.localStorage / globalThis.sessionStorage via lazy JS accessors.
2. When Storage::GetAll() opens a malformed database file (#65879), Storage::Open() creates a JS Error only for DOMStorageAgent::getDOMStorageItems() to catch it with v8::TryCatch and extract the message string.
Rather than deferring DOMStorage.getDOMStorageItems (which would prevent inspecting storage while paused in NodeInspectorClient::runMessageLoopOnPause), could try something like
0001-inspector-avoid-JS-execution-in-DOMStorage.getDOMSto.patch that keeps everything in C++
Note that we are drafting a new policy for updating V8 in Node.js - for V8 commit backports that are justified to be fast tracked and do not have to wait for the regular V8 upgrade, it's better to target release staging branches, instead of main: https://github.com/nodejs/node/pull/66710
Thanks @joyeecheung. I dropped the backport and the commit that enables the check. I'll enable it in a separate PR once a V8 update brings in v8/v8@35bd1582fe0a.
@deepak1556, you're right about the second case: with a code setter on Error.prototype, reading a malformed file from an interrupt aborts, since ERR_INVALID_STATE sets code with Object::Set.
I tried your patch with the check enabled and both cases work. One thing I hit: Storage::New tells the stores apart by location, but --localstorage-file=:memory: is also valid, so if sessionStorage is created first, getDOMStorageItems for sessionStorage returns the localStorage items, even outside an interrupt. Using the third constructor argument that InspectorLocalStorage and InspectorSessionStorage already pass fixes it:
BindingData* binding_data = realm->GetBindingData<BindingData>();
if (binding_data != nullptr && args[2]->IsBoolean()) {
auto& slot = args[2]->IsTrue() ? binding_data->local_storage
: binding_data->session_storage;
slot = BaseObjectWeakPtr<Storage>(storage);
}
If you're OK with that change, I'll add your patch here with you as the author, plus regression tests.
Thats a better check, thanks for catching it.
If you're OK with that change, I'll add your patch here with you as the author, plus regression tests.
SGTM
Thanks @deepak1556, I added it as the second commit with the args[2] change, and the test is in the third one. I also made the untouched-store path skip ToNamespacedPath() for :memory:, like Storage::New does, since on Windows it would resolve :memory: against the cwd and open it as a file.
One thing: your patch didn't have a Signed-off-by, and the guidelines ask for one from each author, including the commit author. Could you reply with yours so I can amend it in?
Signed-off-by: deepak1556 <hop2deep@gmail.com>, thanks!
Thanks, added.
@joyeecheung, could you add request-ci when you get a chance? Thanks!
When would we need this fallback? If we are adding the binding data it seems better to just stick to that and if that's not available because the initialization is not yet done, just ignore it?
Good point, removed it. An empty slot now takes the same path as during an interrupt, which still reads localStorage from the file so items from an earlier run show up. Thanks!
@joyeecheung @legendecas, could you take a look when you get a chance? Thanks!
In FlushPendingMessages() I think it should guard if (pending_messages_.empty()) return; more now since this comes from a microtask, not a native immediate now, i.e. it should be if (pending_messages_.empty() || !env()->can_call_into_js()) return;
This is now resolved multiple times in different locations, if any chdir happens in between this can introduce inconsistencies. I think it would be better to resolve it once (maybe during options parsing time, or when the Environment is constructed) and store it in the Environment, the JS side should also read the resolved value instead of resolving it independently in getStorageKey in inspector/webstorage.js. Then you don't need that many changes in src/path.cc, only the namespacing part may be necessary (although, even today I think on main we don't take care of it in this case..)
This doesn't seem very reliable on slow machines, maybe replace it with globalThis.interval = setInterval(() => {}, 1000);, and then to shut it down, send a Runtime.evaluate to clear it?
Instead of using spawnSync and then asserting the status directly, which means when the test fail we are only to see very little useful information in the CI, can you move the helper into a test/common file, then split the test cases into their own files, each using spawnSyncAndAssert from test/common/child_process which shows more information when the child process does not operate as expected? See https://github.com/nodejs/node/blob/main/doc/contributing/writing-tests.md#how-to-write-a-good-test
Done, thanks.
Done. The helper is now in test/common/inspector-dom-storage.js, with a separate file for each case. Only the file case has a setup process, and it uses spawnSyncAndExitWithoutError now. The reads still need NodeInstance, since the parent talks to the child while it's busy, but it already prints the child's stderr.
With only an interval, the test doesn't guarantee that the reads arrive during a V8 interrupt. I kept the loop to exercise that path and added your interval so the child stays alive if the loop times out. In that case the reads can come from the event loop, but the counters still catch calls to the storage getters and the Error.prototype.code setter.
It's made absolute once now, while the options are parsed, and localStorage, getStorageKey() and both agents read that value. getStorageKey() keeps path.resolve() to normalize it, so ./storage.db still gets the same key as Storage.getStorageKey. I left path.cc as it was, since it only has the namespacing part. On main, Storage::New already namespaces the path on Windows, and the direct read has to match it to open the same file. A chdir before the first use of localStorage no longer moves the file, which I documented under --localstorage-file.
The two storage keys still differ in some cases, though. getStorageKey() builds the URL by concatenating the path, while Storage.getStorageKey uses FromFilePath() on the canonical path, so a space, a non-ASCII character or a symlink in the path gives the event and the query different keys. That's the same on main. Would it be OK to fix that in a separate PR?
Something to prepare for once this lands, https://chromium-review.googlesource.com/c/v8/v8/+/8775587 will now disallow non pausing cdp commands during an interrupt, would need to separate the request queue based on an allowlist similar to blink https://chromium-review.googlesource.com/c/chromium/src/+/8775568
@deepak1556 thanks for the heads-up. I can add the allowlist in the follow-up that enables the check, unless you're already on it.
@joyeecheung could you take another look when you get a chance? Thanks!
I want to address it eventually for electron where the feature is enabled but no immediate plans.
I rebased to pick up the lint fix from #67904, nothing else changed.
joyeecheung
christianaurichzm
This lands the Node.js side of #65397, which was reverted in #65907, without enabling the V8 check.
The first commit is @deepak1556's
inspector: defer JS execution from V8 interruptsfrom #65397, unchanged apart from a conflict with #65877 and acan_call_into_js()check inFlushPendingMessages(). As before, messages for an in-processinspector.Sessionthat arrive while Node.js handles another client's message in an interrupt are delivered at the next microtask checkpoint instead of synchronously.The second commit is @deepak1556's patch from the discussion below.
DOMStorage.getDOMStorageItemsnow reads the stores without calling into JavaScript: it no longer goes through theglobalThis.localStorageandglobalThis.sessionStoragegetters, and a store that cannot be opened is reported without creating a JavaScript error. The third commit adds thetest-inspector-dom-storage-interrupt-*tests, which read both stores from an interrupt and fail if any JavaScript runs there. They count calls to the storage getters and to anError.prototype.codesetter, so they don't depend on the V8 check. Enabling the check needs v8/v8@35bd1582fe0a, whichdeps/v8has since the V8 15.8 update, so that will be a separate PR.The fourth commit makes a relative
--localstorage-fileabsolute once, when options are parsed, and documents it. It used to be resolved against the cwd at each use, so aprocess.chdir()could makelocalStorage, its storage key and the inspector's direct read point at different files. A chdir beforelocalStorageis first used no longer moves the file.Each commit stands on its own, so this can land with
commit-queue-rebaseinstead of being squashed.Refs: https://github.com/nodejs/node/pull/65397
Refs: https://github.com/nodejs/node/pull/65907
Refs: https://github.com/nodejs/node/pull/66710