Skip to content

fix(input): dispose the drag source handle when the target lookup fails - #2441

Closed
ch-bas wants to merge 1 commit into
ChromeDevTools:mainfrom
ch-bas:fix/drag-handle-leak
Closed

ch-bas wants to merge 1 commit into
ChromeDevTools:mainfrom
ch-bas:fix/drag-handle-leak

Conversation

@ch-bas

@ch-bas ch-bas commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #2440

Problem

The drag handler resolved both element handles before its try/finally:

const fromHandle = await request.page.getElementByUid(request.params.from_uid);
const toHandle   = await request.page.getElementByUid(request.params.to_uid); // can throw
try { ...drag... } finally { void fromHandle.dispose(); void toHandle.dispose(); }

If getElementByUid(to_uid) throws — which it does for a stale/invalid to_uid — the try is never entered and fromHandle (a fresh remote object) is never disposed, leaking it for the life of the page's execution context. Same class as the merged #2422; fill_form already handles this correctly, so drag was the only affected input tool.

Fix

Acquire toHandle inside a nested try so fromHandle is disposed on every path:

const fromHandle = await request.page.getElementByUid(request.params.from_uid);
try {
  const toHandle = await request.page.getElementByUid(request.params.to_uid);
  try { ...drag... } finally { void toHandle.dispose(); }
} finally { void fromHandle.dispose(); }

Testing

  • Added disposes the source handle when the target lookup fails: stubs getElementByUid so the target lookup rejects, and asserts the source handle's dispose was still called. Fails on main (dispose called 0 times) and passes with this change.
  • npm run test tests/tools/input.test.ts — all pass.
  • ESLint + Prettier clean.

The drag handler resolved both element handles before the try/finally:
fromHandle, then toHandle. If getElementByUid(to_uid) threw - which it
does for a stale or invalid to_uid - the try was never entered, so
fromHandle was never disposed, leaking a remote object for the life of
the page's execution context.

Acquire toHandle inside a nested try so fromHandle is disposed on every
path. Adds a regression test that fails on the previous behavior.

Fixes ChromeDevTools#2440
pull Bot pushed a commit to Stars1233/chrome-devtools-mcp that referenced this pull request Jul 30, 2026
…#2443)

Re-export the Puppeteer helpers as we have tested those.
This should also be a small performance improvement as we don't await
any of the disposals.
Will also fix memory leaking for some methods.
With the added benefit of make the code look cleaner (no try/catch.)

Also Fixes
ChromeDevTools#2440,
Closes ChromeDevTools#2441
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

drag leaks the source ElementHandle when the target uid lookup fails

1 participant