Repository navigation
fix: settle cancelled evaluate_script calls - #3575
JasonOA888 wants to merge 5 commits into
Conversation
OrKoN
left a comment
There was a problem hiding this comment.
Please check the following:
-
src/tools/script.ts,performEvaluation(): ReplacerejectOnAbort()and the page-sidePromise.racewithraceWithSignal(evaluatable.evaluate(...), signal)fromsrc/utils/raceWithSignal.ts, or state in the PR description why the page-side promise is necessary. The task queue is released when the handler settles, so a Node-side race gives the same result with no added protocol round trip, also coversformat: "script"and a synchronous loop, and does not depend on aPromise.racethat the page can replace. -
src/tools/script.ts,rejectOnAbort(): If the page-side promise stays, do not start the mainevaluatable.evaluate()when the signal aborts betweensignal.throwIfAborted()and that call. In that intervalonAbortrejectsabortedbefore a handler exists, so the page gets anunhandledrejectionevent and the user function still starts after the cancellation. -
src/tools/script.ts,rejectOnAbort(): If the page-side promise stays, return beforeevaluatable.evaluateHandle()whensignalisundefined. The current code creates a page object and a promise that no caller can reject. -
tests/tools/script.test.ts: Add a test for a signal that is already aborted whenperformEvaluation()starts, and a test for cancellation withserviceWorkerId. The new test calls only the page path with an abort during the evaluation, so thesignal.throwIfAborted()branch and the service worker call site have no test.
|
1. Node-side race instead of the page-side promise Agreed, and changed. 2. Abort between
3. Page object created when there is no signal No longer applies: with no signal, 4. Tests Added both: |
evaluate_script is not page-scoped, so it runs on the global task queue. When the client cancels a call whose function never settles, ToolHandler stops waiting but the evaluation keeps the queue, and every later tool call waits behind it. Tool handlers now receive the request signal. evaluate_script races the user function against a page-side promise that rejects when the signal aborts, so the protocol call returns and the queue is released. Refs ChromeDevTools#2934
Settle the handler as soon as the MCP request is cancelled instead of rejecting a page-side promise. This also covers format: "script" and synchronous loops, costs no extra protocol round trip and does not depend on page globals. An already cancelled request no longer starts the evaluation.
0dba818 to
b376c7b
Compare
OrKoN
left a comment
There was a problem hiding this comment.
It looks like tests are failing
The slim mode tests from ChromeDevTools#3565 assert the exact handler request, which now includes the MCP request signal.
|
Thanks. The failures were the two slim mode tests in |
Both formats resolve a handle first and send a second Runtime call to run or serialize it. Check the signal in between so a request cancelled during the first call does not start the second one.
OrKoN
left a comment
There was a problem hiding this comment.
Please check the following:
-
In
tests/tools/script.test.ts, replace the real-browser testdoes not start an evaluation for an already cancelled requestwith a mock test that assertssinon.assert.notCalled(page.pptrPage.evaluateHandle). The current test also passes when the firstsignal?.throwIfAborted()inperformEvaluation()is removed, because the check afterevaluateHandle()also stops the function. -
In
tests/tools/script.test.ts, replace the two real-browser testsrejects a pending evaluation when the request is cancelledandrejects a pending service worker evaluation when the request is cancelledwith mock tests that stubevaluatewith a promise that never settles (createHandlerMocks(),createMockWebWorker(),createMockMcpWorker()).AGENTS.mdpermits real-browser tests only for behavior that mocks cannot observe, and the service worker test installs an extension and pollsevaluate_scriptup to 50 times. -
In the two
skips the second ${format} CDP call on aborttests, replacesinon.assert.calledOnce(page.pptrPage.evaluateHandle)withsinon.assert.calledOnceWithExactly(...)and the expected source string. TheAGENTS.mdchecklist requires exact-argument assertions when the handler derives the argument from its input.
Replace the real-browser cancellation tests with mock tests. The already-cancelled test now asserts that no CDP call is sent, which the later abort check alone does not satisfy, and the second-call tests assert the exact source passed to evaluateHandle().
|
Thanks, all three addressed in 094c291. Only tests changed.
All five tests fail on |
Refs #2934
#3079 made cancellation stop waiting, but work that is already running still holds its task queue until it settles.
evaluate_scriptruns on the global queue because it is not page-scoped. So if the client cancels a call whose function never settles, every later tool call (list_pages,take_snapshoton another page, ...) waits behind it. This is Case B in the latest re-test. Today the only things that end it are the protocol timeout or the page going away.Change
ToolHandlerpasses the request'sAbortSignalto tool handlers asrequest.signal.Requestgets an optionalsignal. This is the same field, in the same place, that fix: forward MCP request cancellation to WebMCP tool calls #3411 adds for WebMCP, so whichever PR lands second has a one-line conflict.performEvaluation()races the evaluation with the signal in Node, using the existingraceWithSignal(). On cancellation the handler rejects with the signal'sAbortErrorand the queue is released. The race sits inside the action passed towaitForEventsAfterAction(), so the wait helper still cleans up its listeners. Pages and service workers share this path.performEvaluation()starts throws before anything is sent to the page, so the user's function never runs.evaluateHandle()resolves the function or the script result, thenevaluate()runs or serializes it. The signal is checked again between them, so a request cancelled during the first call does not send the second.Behavior changes
evaluate_scriptnow releases the task queue right away instead of holding it until the evaluation settles. This holds for both formats and for synchronous loops.evaluate_scriptcall whose request is already cancelled no longer runs the function.filePathoutput.Not covered
Runtime.terminateExecution, which also stops unrelated page scripts, so I left it out. A synchronous loop therefore still blocks later tools on the same page, but no longer the global queue.execute_webmcp_tool/execute_3p_developer_tool. That is fix: forward MCP request cancellation to WebMCP tool calls #3411.Testing
tests/tools/script.test.ts: five new mock-based tests, each fails onmainand passes with this change:evaluatenever settles): abort after it starts, expectAbortError;serviceWorkerId;AbortErrorand noevaluateHandle()call. This test also fails if only the earlythrowIfAborted()is removed;evaluateHandle(), asserts its exact source, and checks thatevaluate()is never called.tests/ToolHandler.test.ts: new test that the request signal reaches page, browser and browserless handlers. I updated the existing exact-argument assertions for the new field.npm run formatandnpm run buildpass. Withnpm run test, every failure is in the CLI e2e suites (tests/e2e/chrome-devtools-*.test.ts), and those suites fail the same way on an unmodifiedmainin my environment.