Repository navigation
Description
What happened
On a large project root (1.6M files / 216k directories on a virtiofs mount), an instance rooted at that folder makes the server stop answering HTTP for minutes. GET /global/health times out (30 s client limit) for five minutes and more, session message reads time out too, and the process grows from 676 MB to 3.2 GB RSS. It recovers on its own after roughly 10-12 minutes, or immediately on restart.
Two separate costs are involved, and the second one is what turns a slow index into an outage:
- The file index is incomplete and silent.
File.state()primes arg --files --hiddenlisting of the whole instance root at bootstrap and keeps it for the life of the instance (src/file/index.ts,src/project/bootstrap.tswarm()).Ripgrep.files()passesmaxBuffer: 20 MBtoBun.spawn; Bun terminates the child at that limit without raising, so on this root (1,638,457 files and 216,763 directories) the server keeps roughly 340,000 entries. Directories that exist on disk (ansible_seimel, and a file inside it) return no matches, and nothing is logged. - The watcher subscribes recursively to the same root (
src/file/watcher.ts,@parcel/watcher), adding one inotify watch per directory. This VM allows 124,437 watches per user (fs.inotify.max_user_watches), the root has 216,763 directories, so the subscription can never complete.addRoot()abandons the promise after 10 s but does not cancel the native walk, and every later instance retries it:
+10001ms service=file.watcher root=/mnt/projects owner=project error=Operation timed out after 10000ms failed to subscribe to file root
With two or three instances like this live at once (the SPA mints one per session directory and per folder selection), the walk plus the index exhausts memory: swap fills, the kernel drops the virtiofs metadata cache, and the single Bun event loop gets starved. The main thread sits in epoll_wait at under 1 % CPU while connections sit in the accept backlog, so it looks like a hang rather than a busy loop.
Steps to reproduce
- On Linux, mount a large tree (here: 1.6M files, 216k directories, virtiofs).
- Start
openscience serve --port 4096. - Send three concurrent file searches against three large roots, which creates three instances and starts three index/watcher warmups:
curl "http://127.0.0.1:4096/find/file?query=README&type=file&limit=10&directory=/mnt/projects/fairphone5"
curl "http://127.0.0.1:4096/find/file?query=README&type=file&limit=10&directory=/mnt/projects/ansible_seimel.wt"
curl "http://127.0.0.1:4096/find/file?query=README&type=file&limit=10&directory=/mnt/projects/hermes"
- Poll
GET /global/healthevery second.
Expected: health keeps answering, the searches finish.
Actual: none of the three requests returns within five minutes; every health probe times out; host load average 9.6 on 4 vCPUs, memory 13.2/16 GB used, swap 4/4 GB used, process RSS 3.2 GB.
With OPENSCIENCE_EXPERIMENTAL_DISABLE_FILEWATCHER=1 the same three requests return 200 in 7.2 s, 10.7 s and 17.8 s, and the worst health sample is 0.65 s. So the watcher subscription, not the index alone, is what pushes this over.
Suggested fixes
- Do not let the spawn buffer decide how much of the tree is indexed: stream the listing with an explicit entry limit, detect the child exit code, and log or surface the truncation instead of returning partial results as if they were complete.
- Give the index an abort path tied to the request, and (ideally) build it off the request path instead of priming it in warmup.
- Apply the same ignore set that
FileIgnoreandSEARCH_NOISEalready define to theRipgrep.files()call, so dependency and cache trees never enter the index (they are filtered out of results later today). - Cancel the watcher walk when the subscribe timeout fires, and skip the recursive subscribe when the root's directory count exceeds the inotify budget, with one log line instead of a retry per instance.
- For the empty-query file search, the code sorts the entire listing synchronously (
[...dirs, ...files].toSorted(shallow)); sorting the top N with a bounded heap would avoid seconds of blocked event loop.
How did you install OpenScience?
Other (say which in the description) - npm install --prefix /opt/openscience @synsci/[email protected], run as a systemd service, browser over a reverse proxy.
OpenScience version
2.0.153
Provider and model
Not relevant to this bug (subscription provider, no model call happens during the stall).
Operating system
Linux 6.8 (Ubuntu 24.04 guest, virtiofs project share)
Activity