Console messages from an earlier navigation lose their stack trace

Open
#3,396 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
devtools, typescript
Domain
devtools

Research direction

Start in src/devtools/DevtoolsUtils.ts at createStackTrace(), where the 1 s script wait's .catch() swallows the timeout, then follow its callers ConsoleFormatter and SymbolizedError, and formatFrame for the 0-based output. Reproduce by navigating a page with a script and listing preserved console messages with stack traces. Done means old frames keep their URL, 1-based line and column, and never resolve against a different page's script with a reused id. A pull request (#3397) is already open for this, so check it before starting.

Written by the indexing model from the issue text.

Description

confirmed p2
Description of the bug

After the page navigates, console messages and uncaught errors from the previous page lose their whole stack trace:

  • list_console_messages with includePreservedMessages: true and includeStackTraces: true lists them without any at ... lines.
  • get_console_message for one of them has no ### Stack trace section.

Before the navigation, the same messages have full stack traces. After it, each of these calls first waits about 1 s for the old page's scripts. A reload does the same: the messages logged before it lose their stack, while the identical messages logged after it keep theirs.

After a navigation to another site, the old messages can instead get the frames of the new page's scripts. In the reproduction below, logFromA from a.js is then printed as a frame of c1.js, a script of the new page.

createStackTrace() (src/devtools/DevtoolsUtils.ts) waits up to 1 s for every script in the stack trace, but the .catch() on that wait has no handler, so it doesn't catch the timeout. DevTools drops the old page's scripts on navigation, so the wait times out as soon as one script id of the stack is not in use on the new page, createStackTrace() throws, and both callers (ConsoleFormatter for console messages, SymbolizedError for uncaught errors) drop the stack.

The wrong frames have another cause: script ids are only unique within a renderer process, and the process of a new site numbers its scripts from the start again. When every script id of an old stack is in use again, nothing times out, and DevTools resolves the old frames against the new page's scripts.

Adding a handler alone isn't enough: frames of scripts that are gone have no UISourceCode, and formatFrame prints their raw 0-based position (line 1, column 10 instead of a.js:2:11 for logFromA in the reproduction below), while the output says line and column numbers are 1-based.

Reproduction

Serve three files:

  • a.html: <!doctype html><title>a</title><script src="/a.js"></script>
  • b.html: <!doctype html><title>b</title>
  • a.js:
function logFromA() {
  console.log("message from A");
}
logFromA();
function throwFromA() {
  throw new Error("uncaught from A");
}
setTimeout(throwFromA, 0);

Then call the tools (output trimmed to the two messages from a.js):

new_page               {"url": "http://127.0.0.1:PORT/a.html"}
list_console_messages  {"pageId": 2, "includeStackTraces": true}
  msgid=1 [log] message from A (1 args)
    at logFromA (a.js:2:11)
    at  (a.js:4:1)
  msgid=2 [error] Uncaught Error: uncaught from A (0 args)
    at throwFromA (a.js:6:9)
    --- setTimeout -------------------------
    at  (a.js:8:1)

navigate_page          {"pageId": 2, "type": "url", "url": "http://127.0.0.1:PORT/b.html"}
list_console_messages  {"pageId": 2, "includePreservedMessages": true, "includeStackTraces": true}
  (took 1003 ms)
  msgid=1 [log] message from A (1 args)
  msgid=2 [error] Uncaught Error: uncaught from A (0 args)

get_console_message    {"pageId": 2, "msgid": 1}
  (took 1004 ms)
  ID: 1
  Message: log> message from A
  ### Arguments
  Arg #0: message from A

get_console_message    {"pageId": 2, "msgid": 2}
  (took 1007 ms)
  ID: 2
  Message: error> Uncaught Error: uncaught from A

With navigate_page {"pageId": 2, "type": "reload"} instead of the navigation to b.html (msgid 3, a Failed to load resource 404, left out):

list_console_messages  {"pageId": 2, "includePreservedMessages": true, "includeStackTraces": true}
  (took 1005 ms)
  msgid=1 [log] message from A (1 args)
  msgid=2 [error] Uncaught Error: uncaught from A (0 args)
  msgid=4 [log] message from A (1 args)
    at logFromA (a.js:2:11)
    at  (a.js:4:1)
  msgid=5 [error] Uncaught Error: uncaught from A (0 args)
    at throwFromA (a.js:6:9)
    --- setTimeout -------------------------
    at  (a.js:8:1)

For another site, serve c.html and four small scripts from the same server and open c.html through localhost instead of 127.0.0.1:

  • c.html: <!doctype html><title>c</title><script src="/c1.js"></script><script src="/c2.js"></script><script src="/c3.js"></script><script src="/c4.js"></script>
  • c1.js to c4.js, the same for each number:
function fromC1() {
  return 1;
}
fromC1();

With navigate_page {"pageId": 2, "type": "url", "url": "http://localhost:PORT/c.html"} instead of the navigation to b.html:

list_console_messages  {"pageId": 2, "includePreservedMessages": true, "includeStackTraces": true}
  (took 3 ms)
  msgid=1 [log] message from A (1 args)
    at logFromA (c1.js:2:11)
    at  (c1.js:4:1)
  msgid=2 [error] Uncaught Error: uncaught from A (0 args)
    at throwFromA (c1.js:6:9)
    --- setTimeout -------------------------
    at  (c1.js:8:1)
Expectation

Messages from an earlier navigation keep their stack traces. Their scripts are gone, so the frames can't be mapped any more, but each frame still has its URL, line and column, so logFromA should print as at logFromA (http://127.0.0.1:PORT/a.js:2:11), 1-based like the rest of the output. For a plain script like a.js that is the same position as before the navigation. No frame should be printed as a frame of a different script that reuses its script id.

MCP configuration

--headless --isolated --no-usage-statistics

Chrome DevTools MCP version

main at 8d009e8 (local build, 1.12.0)

Chrome version

156.0.8078.79

Node version

24.20.0

Operating system

Windows

Extra checklist
  • I want to provide a PR to fix this bug
Dominant language
TypeScript
Stars
59.8k
Forks
4.8k
Avg merge
2d 10h
Merged PRs (30d)
162

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ChromeDevTools/chrome-devtools-mcp

All issues in ChromeDevTools/chrome-devtools-mcp

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.