Skip to content

fix(files): count untracked lines without blocking the server (#1584) - #1609

Merged
agegr merged 4 commits into
mainfrom
fix/issue-1584-git-status-async
Nov 12, 2026
Merged

agegr merged 4 commits into
mainfrom
fix/issue-1584-git-status-async

Conversation

@agegr

@agegr agegr commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Fixes #1584.

Git status. getGitStatus() read every untracked file synchronously (lstatSync + readFileSync), so 46k untracked files on Windows held the server's event loop, and every chat stream with it, for 12 s.

  • Untracked files are read with async fs, 4 at a time (libuv's default pool), in 64 KiB chunks; the status entry loop yields every 1000 entries. Counts are unchanged: one per \n, plus one for an unterminated last line; empty, NUL anywhere, over 256 KiB or not a regular file count 0. No cache, no worker, no setting (perf(git): cache the status listing and share one scan between requests #1322 / perf(git): count an untracked file's lines once while it is unchanged #1350 stand).
  • The explorer aborts a replaced status request and the route passes request.signal, so a scan nobody waits for stops instead of piling up behind each tool-end refresh.
  • Measured on macOS, 46k synthetic untracked files (an independent review, old vs new side by side): same additions; worst event-loop block 8.7 s → 25 ms; total time longer (8.8 → 14.2 s at concurrency 8, the price of not blocking). Not measured on Windows.

pi-web status / stop. isServerAnswering() read a 2 s fetch timeout as "no server", so both lied during a stall. Now a timed-out probe counts as running only when a plain TCP connection to the same address then opens. A timeout alone isn't enough: Windows retries a refused loopback connect for ~2 s, and an unreachable address or stuck lookup times out too, which would let stop act on a recycled pid.

Tests: lib/git-changes.test.mjs (exact counts across edge cases, no sync fs on repo files, an aborted request stops the scan), lib/pi-web-cli-commands.test.mjs (busy socket counts; closed port and timeout-without-connect don't).

🤖 Generated with Claude Code

agegr and others added 4 commits November 11, 2026 15:50
/api/git/status read every untracked file with lstatSync/readFileSync in
one synchronous pass. With 46k untracked files on Windows that held the
server's event loop, and every chat stream and request with it, for 12 s.

Untracked files are now read with async fs, 8 at a time, in 64 KiB chunks
up to the size lstat reported, and the status entries are mapped in turns
of 1000. Counts and response shape are unchanged: one line per newline
byte plus an unterminated last line; empty, NUL-holding, over 256 KiB and
non-regular files count 0. No cache, no shared scan: every request still
reads afresh (#1322, #1350).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
pi-web status, stop, open and update list a record only when its URL
answers within 2 s. A server whose event loop a long request held took the
connection but answered nothing in time, so status printed "No pi-web
server is running." and stop did nothing.

A closed port refuses at once, so a probe that times out now counts as a
running server; a refused or failed connection still does not. The pid
was already found alive and ours before the probe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…1584)

A fetch timeout also comes from a slow refusal (Windows retries a
refused loopback connect for about 2 s), an address that drops packets
or a stuck name lookup. Counting those as running could let `stop`
kill a recycled pid's process tree. After a timeout, connect to the
same address: only an accepted connection means a busy server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a time (#1584)

The explorer aborts a replaced status request, and the route passes the
request's signal, so a long scan whose answer nobody waits for stops
instead of piling up behind each tool-end refresh. Four reads at a time
match libuv's default thread pool; more only queued ahead of the server's
other fs work.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@agegr
agegr merged commit 2e8d3b1 into main Nov 12, 2026
2 checks passed
@agegr
agegr deleted the fix/issue-1584-git-status-async branch November 14, 2026 13:45
Hwwwww-dev pushed a commit to Hwwwww-dev/pi-web that referenced this pull request Nov 19, 2026
…1584) (agegr#1609)

* fix(files): count untracked lines without blocking the server (agegr#1584)

/api/git/status read every untracked file with lstatSync/readFileSync in
one synchronous pass. With 46k untracked files on Windows that held the
server's event loop, and every chat stream and request with it, for 12 s.

Untracked files are now read with async fs, 8 at a time, in 64 KiB chunks
up to the size lstat reported, and the status entries are mapped in turns
of 1000. Counts and response shape are unchanged: one line per newline
byte plus an unterminated last line; empty, NUL-holding, over 256 KiB and
non-regular files count 0. No cache, no shared scan: every request still
reads afresh (agegr#1322, agegr#1350).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(cli): count a busy server as running in status and stop (agegr#1584)

pi-web status, stop, open and update list a record only when its URL
answers within 2 s. A server whose event loop a long request held took the
connection but answered nothing in time, so status printed "No pi-web
server is running." and stop did nothing.

A closed port refuses at once, so a probe that times out now counts as a
running server; a refused or failed connection still does not. The pid
was already found alive and ours before the probe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(cli): a timed-out probe counts only when a TCP connection opens (agegr#1584)

A fetch timeout also comes from a slow refusal (Windows retries a
refused loopback connect for about 2 s), an address that drops packets
or a stuck name lookup. Counting those as running could let `stop`
kill a recycled pid's process tree. After a timeout, connect to the
same address: only an accepted connection means a busy server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(files): stop a git status scan its request left, read 4 files at a time (agegr#1584)

The explorer aborts a replaced status request, and the route passes the
request's signal, so a long scan whose answer nobody waits for stops
instead of piling up behind each tool-end refresh. Four reads at a time
match libuv's default thread pool; more only queued ahead of the server's
other fs work.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 2e8d3b1)
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 24, 2026
…1584) (agegr#1609)

* fix(files): count untracked lines without blocking the server (agegr#1584)

/api/git/status read every untracked file with lstatSync/readFileSync in
one synchronous pass. With 46k untracked files on Windows that held the
server's event loop, and every chat stream and request with it, for 12 s.

Untracked files are now read with async fs, 8 at a time, in 64 KiB chunks
up to the size lstat reported, and the status entries are mapped in turns
of 1000. Counts and response shape are unchanged: one line per newline
byte plus an unterminated last line; empty, NUL-holding, over 256 KiB and
non-regular files count 0. No cache, no shared scan: every request still
reads afresh (agegr#1322, agegr#1350).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(cli): count a busy server as running in status and stop (agegr#1584)

pi-web status, stop, open and update list a record only when its URL
answers within 2 s. A server whose event loop a long request held took the
connection but answered nothing in time, so status printed "No pi-web
server is running." and stop did nothing.

A closed port refuses at once, so a probe that times out now counts as a
running server; a refused or failed connection still does not. The pid
was already found alive and ours before the probe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(cli): a timed-out probe counts only when a TCP connection opens (agegr#1584)

A fetch timeout also comes from a slow refusal (Windows retries a
refused loopback connect for about 2 s), an address that drops packets
or a stuck name lookup. Counting those as running could let `stop`
kill a recycled pid's process tree. After a timeout, connect to the
same address: only an accepted connection means a busy server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(files): stop a git status scan its request left, read 4 files at a time (agegr#1584)

The explorer aborts a replaced status request, and the route passes the
request's signal, so a long scan whose answer nobody waits for stops
instead of piling up behind each tool-end refresh. Four reads at a time
match libuv's default thread pool; more only queued ahead of the server's
other fs work.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

/api/git/status blocks the event loop for ~12 s on a real workspace (46k untracked files, Windows) - the UI freezes, it is not just slow badges

1 participant