`light-webserver` reads each annotated screen synchronously inside the HTTP handler, stalling every other request

Open
#2,084 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
80/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Read skills/ce-brainstorm/scripts/light-webserver.js around line 1135 and compare the matching branch in skills/ce-prototype/scripts/light-webserver.js. Start by tracing the annotate document request path and how it reads and processes the HTML; reproduce the delay with the provided 32 MiB screen and concurrent /version requests. Done means the screen is served while unrelated requests stay responsive, with both copies addressed.

Written by the indexing model from the issue text.

Description

Environment

  • EveryInc/compound-engineering-plugin at commit 122f9f90330df0b54da865e12cbecb81ffd0ae53
  • Node.js 24, Windows/macOS/Linux
  • skills/ce-brainstorm/scripts/light-webserver.js and its ce-prototype copy (same file content)

Summary

In annotate mode, the light-webserver reads a navigated HTML screen with fs.readFileSync from inside the async http.createServer handler, then runs annotateScreen over the full string before writing a byte. Nothing is awaited first, so the read and the string walk both run on the event-loop thread. While a large screen is being read and annotated, every other socket waits: a 3 ms /version request that normally answers in about 3 ms takes as long as the navigation's time-to-first-byte. At 32 MiB the stall is about 1.5 s; at 128 MiB about 3.3 s.

Root cause

// light-webserver.js:1129-1141, inside `if (options.annotate)` in async handleRequest()
if (req.method === "GET") {
  touch()
  const filePath = resolveContainedFile(options.screensDir, req, res)
  if (!filePath) return
  if (contentType(filePath) === CONTENT_TYPES[".html"] && isDocumentNavigation(req)) {
    const renderedKey = screensChangeKey(options)
    serveAnnotateDocument(req, res, annotateScreen(fs.readFileSync(filePath, "utf8"), requestOrigin(req), urlPath), renderedKey)   // line 1135
    return
  }
  ...

handleRequest is async and invoked from the http.createServer callback, but there is no await before line 1135. The synchronous read and the subsequent annotateScreen pass (html.replace, isFullDocument with toLowerCase()) are linear in the screen size and block the single event loop shared by /version, /events, /wait and /annotation.

Affected code

skills/ce-brainstorm/scripts/light-webserver.js, line 1135 (annotate document branch). The same code exists at skills/ce-prototype/scripts/light-webserver.js:1135; both files are one blob at the audited commit.

Reproduction

  1. Create a full HTML document of the target size, for example head -c 33554432 /dev/zero | tr '\0' 'a' wrapped in <!doctype html><html><body>...</body></html>, and place it at screens/big.html under the server root.
  2. Start the real server in annotate mode with a fixed port:
node skills/ce-brainstorm/scripts/light-webserver.js serve \
  --root <run>/root --host 127.0.0.1 --port 4180 --annotate
  1. Send one document navigation for /big.html (Accept: text/html, Sec-Fetch-Dest: document) and, from the same client, four /version requests 3 ms apart on independent sockets.
  2. Repeat the four requests with no navigation in flight to get the control.

Observed with Node 24 on Windows 11:

screen navigation TTFB request max requests over 50 ms control request max
1 KiB 4.4 ms 2.7 ms 0 / 4 4.3 ms
8 MiB 94.7 ms 71.6 ms 2 / 4 3.5 ms
32 MiB 1508.9 ms 1528.5 ms 4 / 4 5.2 ms
128 MiB 3342.7 ms 3351.8 ms 4 / 4 4.3 ms

At 32 MiB the four requests issued at +20/+40/+60/+80 ms all complete at about 1.5 s, in a single burst at the moment the handler returns; the same endpoint answers in 3鈥? ms when no navigation is in flight. A repeat run measured 1280.9 ms and 3309.8 ms TTFB for 32/128 MiB.

Expected behavior

A slow screen read should not stop unrelated clients. The handler should read asynchronously (await fs.promises.readFile) and, if the annotation pass is expensive, yield or perform it off the request path, so /version, /events and other sockets stay responsive while a page is being prepared.

Actual behavior

The handler performs the whole read and annotation synchronously before writing the first body byte. All other requests are suspended for the duration; the delay grows linearly with the screen size and the same 3 ms endpoint becomes roughly 500x slower at 32 MiB.

Suggested fix

const raw = await fs.promises.readFile(filePath, "utf8");
const body = annotateScreen(raw, requestOrigin(req), urlPath);
serveAnnotateDocument(req, res, body, renderedKey);

If annotateScreen remains a long synchronous string pass, consider chunking it or moving it to a worker for large documents.

Dominant language
TypeScript
Stars
25.6k
Forks
2.1k
Avg merge
3h 57m
Merged PRs (30d)
170

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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 EveryInc/compound-engineering-plugin

All issues in EveryInc/compound-engineering-plugin

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.