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

オープン
#2,084 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
80/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, node.js

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
TypeScript
スター
25.6k
フォーク
2.1k
平均マージ
3時間 57分
マージ済み PR(30日)
170

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

EveryInc/compound-engineering-plugin のほかの issue

EveryInc/compound-engineering-plugin の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。