Skip to content

Hot reloading of traces run into streaming issues on some browsers #18

Open

Description

@Sneakercopter

Issue Identified

When utilising the recommended debug loop of importing traces onto https://ui.perfetto.dev/ I noticed that doing so via the webUI provided by watch_trace.html by running python watch_trace.py actually throws BrokenPipeError: [Errno 32] Broken pipe when /trace.json is fetched via await fetch(traceUrl);.

Steps to reproduce

Issue reproduced on Chrome Version 143.0.7499.193

  1. Execute python perf_takehome.py Tests.test_kernel_trace to generate a local trace.json
  2. Run python watch_trace.py and wait for a tab of http://localhost:8000/ to open
  3. Select /trace.json and press "Open Perfetto"
  4. Perfetto tab will not open, and BrokenPipeError: [Errno 32] Broken pipe error should appear in your python terminal

Solution proposal

Investigating further, when calling /trace.json directly in the browser actually works fine, when checking the Javascript code responsible for this we see that blob is populated via

const resp = await fetch(traceUrl);
const blob = await resp.blob();

This raises issues in some browsers as our absence of a Content-Length header (which is fine for streaming) is actually causing the browser to abort the stream if the /trace.json is big enough.

Image

To fix this for those browsers, the fetching logic in Javascript can be modified to populate chunks with a reader as they come and then populated into a chunk manually.

const resp = await fetch(traceUrl);
const reader = resp.body.getReader();
const chunks = [];

while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  chunks.push(value);
}
const blob = new Blob(chunks);

Applying this change allows the request to succeed when called within the Javascript in the tab served by python watch_trace.py

Image

Activity

  1. Sneakercopter commented on Jan 23, 2026

    @Sneakercopter
    Author

    I have opened a PR #19 to apply the proposed fix

  2. Fi6uDvGWucFv commented on Jun 20, 2026

    @Fi6uDvGWucFv

    The BrokenPipeError: [Errno 32] seems to be directly linked to the absence of a Content-Length header when the blob is being fetched in the JavaScript method. I suggest implementing a streaming solution in the fetch logic that reads chunks, as this could help prevent the browser from aborting during the fetch of larger trace.json files.

  3. K5BySaUdTH4K commented on Jul 2, 2026

    @K5BySaUdTH4K

    In addition to @Fi6uDvGWucFv's suggestion, consider implementing a ReadableStream in the fetch logic to manage the response body as chunks; this would allow handling larger files without hitting the Content-Length limitation and prevent the BrokenPipeError from occurring.

  4. agatasalathiel commented on Oct 19, 2026

    @agatasalathiel

    Instead of just implementing a ReadableStream, we should update watch_trace.py to explicitly set the Content-Length header by calling os.path.getsize() on the trace.json file before the response is sent. This avoids the overhead of manual chunk management in the frontend while directly resolving the browser's expectation for a fixed-length stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions