Repository navigation
Description
Description
On Windows (desktop 2.0.32, managed opencode-cli.exe), if a grep tool call is blocked on a permission
request that the API cannot list, and that session's working directory is then removed, the managed
service pins one core at ~100% forever (5/5 runs). Nothing recovers it except restarting the app:
no new log lines, no DB writes, no memory/handle growth, and /api/info keeps answering in 1 ms, so the
desktop liveness watchdog never fires.
The two ingredients:
- a tool call blocked on a permission request that is invisible to every client: listing it answers
400with an empty body, and the service logs
schema rejection ... ["data"][0]["metadata"]["include"](the tool part carries"metadata": {}), and - removing that session's working directory while the call is still blocked (deleting the session is
not needed).
Root cause, confirmed by A/B: the location's file watcher whose watched directory disappeared. With
the app started as OPENCODE_DISABLE_FILEWATCHER=true (watcher subscription lines 33 → 0) the identical
recipe does not spin at all (2/2 runs, 0.0% over 20 s), while the blocked request is unchanged. The
subscription is never released afterwards either: in three directory-removal runs the removed paths log
watcher stopped 0 times (97 such lines exist elsewhere in the log).
Measured while spinning:
- exactly one thread burns (tid 4204): ~100% of one core, 92–94% of it kernel time; the other 32
threads of the process are at 0 IO Other Operations/sec≈ 316,000 (control-class, i.e. neither read nor write), read ≈ 0/s,
write ≈ 0/s — i.e. one control-class syscall per iteration- zero new log lines,
opencode.db/-wal+0 bytes, handles/threads/memory completely flat /api/info1 ms,/api/session1 ms (the >6 s liveness watchdog can therefore never fire)
Control: the same sequence with { "permission": { "bash": "ask" } } (whose request is listable,
action=shell) gives 0.3–2.2% CPU, no spin; and creating + deleting an identical throwaway directory
without a blocked tool call does nothing at all (≤3.6%, zero watcher lines).
Related: #56712 (closest symptom: serve --service busy-looping one core, main thread R, no logs, API
alive — but theirs is intermittent/user-space, ours is continuous, kernel-time heavy and a deterministic
repro), #53811 (same subsystem: fs watcher churn), #47505 / #55679 (Windows + embedded @parcel/watcher
crash family, where OPENCODE_DISABLE_FILEWATCHER=1 is already the known workaround).
Plugins
None (only the bundled @opencode-ai/plugin; no plugin key in any opencode.json).
OpenCode version
2.0.32 desktop (managed opencode-cli.exe; log line: cli starting version=2.0.32 channel=latest args=["serve","--service"])
Steps to reproduce
-
mkdir %TEMP%\opencode\oc-repro\work
and write %TEMP%\opencode\oc-repro\work\opencode.json = { "permission": { "grep": "ask" } } -
POST /api/session
{ "location": { "directory": "%TEMP%\opencode\oc-repro\work" }, "title": "delete-me" } -
POST /api/session/{id}/prompt
{ "text": "Use the grep tool to find the word 'repro' in this directory." }
-> the grep tool call starts and then blocks silently. While it is blocked the permission request is
not listable: GET /api/permission/request?location[directory]=... -> 400, empty body, and the log says
WARN message="schema rejection" kind=Body reason="Expected JSON value\n at ["data"][0]["metadata"]["include"]" -
while the call is still blocked, remove that directory (rmdir /s /q %TEMP%\opencode\oc-repro)
-> opencode-cli.exe pins ~100% of one core within ~5 s and never stops.
Only step 4's "the watched directory goes away" matters: deleting just the session (keeping the directory)
gave max 10.8% over 20 s; deleting just the directory with the session still alive gave 100.1% within 5 s;
deleting a throwaway directory without a blocked tool call did nothing (<=3.6%, zero watcher lines).
Waiting does not help: with nothing deleted, 200 s of sampling never exceeded 7.8% and rg.exe was never
spawned (executed:false) - the request never times out.
Screenshot and/or share link
No response
Operating System
Windows 10 IoT Enterprise LTSC 21H2, 10.0.19044, x64, 6 cores / 12 threads.
Terminal
OpenCode Desktop (Electron) app; all API calls above were made with plain HTTP against the managed service.
Activity
helloSpookle commentedon Oct 19, 2026
Measurements, bisect and instrumentation notes for the repro above. Everything is from this machine
(Windows 10 21H2 19044, x64, i5-12600K 6C/12T, no plugins), desktop 2.0.32, managed CLI at
%APPDATA%\ai.opencode.desktop\cli\2.0.32-x64\opencode-cli.exe(cli starting version=2.0.32 channel=latest args=["serve","--service"]), spawned by the Electron app withstdio:['ignore','ignore','pipe'].Raw counters while spinning
Win32_Processdelta over 15.01 s: KernelModeTime +13.75 s (92%), UserModeTime +1.31 s;%priv 84–93/%user 5–12IO Other Operations/sec311k–318k,IO Read ≈ 0/s,IO Write ≈ 0/s; cumulative over the spin: Other 58,722,150 vs Read 7,532 vs Write 1,080Running; the other 32 threads at 0opencode.loglines during the spin;opencode.db/-wal+0 bytes / 12 s; no new sockets/api/info1 ms,/api/session1 ms,/api/permission/request0 ms (deleted session's endpoints 404)The request is real — the API just cannot serialize it
GET /api/session/{id}/messageshows the stuck tool part:{"type":"tool","id":"call-fd09d374-…","name":"grep","executed":false, "state":{"status":"running","input":{"pattern":"repro","path":"C:\\…\\oc-r6-101335\\work"}, "metadata":{}}}While it is pending (200 s window, polled every 2 s):
GET /api/permission/request?location[directory]=<dir>GET /api/session/{id}/permissionGET /api/permission/request(no filter)locationresolves to the home dir →data: []The log for each poll:
217 of the 229
schema rejectionlines in the log point at exactly that path (one was["data"][0]["metadata"]["path"]during another blocked run). So the request exists, but the responsebody fails its own schema — no client can see or answer it, and the turn stays stuck indefinitely.
Bisect: which step actually triggers it
rg.exenever spawned (executed:false)⇒ the decisive ingredient is "the watched directory goes away while a call is blocked"; deleting the
session is incidental, and nothing times out.
Watcher evidence (A/B + lifecycle)
watcher subscribe … type=entriesfor every ancestor directory,type=filefor eachAGENTS.md,backend=node)OPENCODE_DISABLE_FILEWATCHER=trueIn both cases the blocked request was identical (both listings 400/empty, tool part
metadata:{}/executed:false). In three directory-removal runs the subscriptions for the removed paths logwatcher subscribe/watcher started= 4 each andwatcher stopped= 0, while the log contains 97watcher stoppedlines for other paths — i.e. this teardown path does not release the subscription.Instrumentation notes (why there is no .cpuprofile, and what was used instead)
SIGPROFon Windows; the packaged desktop app rejectsNODE_OPTIONS(
node_bindings.cc:492] Most NODE_OPTIONs are not supported in packaged apps); the CLI rejects--inspect/--inspect-portasUnrecognized flag; Bun ignoresNODE_OPTIONS/BUN_OPTIONS.BUN_INSPECT=<host>:<port>/<path>does work (the managed service opens an inspector websocket).The path is mandatory: without it Bun serves a random token path which never surfaces (the app pipes the
child's stderr), and Bun does not implement Node's
/jsondiscovery endpoint.Profilerdomain:{"error":{"code":-32601,"message":"'Profiler' domain was not found"}}. OnlyRuntime,Debugger,Console,HeapandInspectorexist, andDebugger.pauseneither errors nor emitsDebugger.paused.So
cpu-profile.tscannot produce a.cpuprofilehere either.Runtime.evaluateanswers in 1–2 ms, so the JS thread is not stuck (/api/info1 ms throughout).process.getActiveResourcesInfo()→[](Bun has no Node-style resource tracking).Runtime.evaluatewhile samplingper-thread CPU. The looping thread and the spinning thread are the same tid, but the busy loop
burns 0% kernel time while the spin burns 92% — i.e. a JS-driven loop that spends essentially
all of its time in one control-class syscall per iteration and still yields back to the event loop
(which is why the API stays at 1 ms and the watchdog never fires).
wpr.exe(ETW, needs elevation) is available here for a native.etltrace of the kernel side if youwant one; it would contain local paths, so I'd only hand it over on request.
Reproduction scripts
The repro is driven by small PowerShell scripts on my side (create dir/session/prompt, sample CPU once a
second, dump raw status codes). Happy to paste or attach them if that is useful.
opencode-agent commentedon Oct 19, 2026
Thanks for the detailed report and the A/B against
OPENCODE_DISABLE_FILEWATCHER, which was very useful.I tried your recipe on Linux with 2.0.32:
grepset toask, agrepcall left waiting on its permission prompt, then the session's working directory deleted. The service stayed at about 2–5% CPU afterwards, so it didn't spin. That fits your measurements (mostly kernel time, ~316k control-class I/O operations/s), which point at the Windows backend of the file watcher (@parcel/watcher/ReadDirectoryChangesW) after its watched directory disappears. The watcher code also never seems to release a subscription when the watched directory itself goes away, which matches the missingwatcher stoppedlines in your log. A maintainer with Windows will need to confirm it.In the meantime, starting the app with
OPENCODE_DISABLE_FILEWATCHER=trueavoids the spin, as you found.Related issues:
grep/globpermission requests can't be listed because of undefined metadata (your 400 /metadata.includeerror)serve --servicehangs in userspace busy-loop at 100% CPU (v2.0.29) #56712, 2.0 (linux): managed service main thread intermittently busy-loops in user space while idle (~100% of one core, no log/DB output) #51852: the service busy-looping one core with no logs (Linux, user space)@parcel/watcherproblems where disabling the file watcher is the workaroundhelloSpookle commentedon Oct 19, 2026
Thanks — and your Linux run (2–5%, no spin) is exactly what our A/B predicts, so this really does look
Windows-side. Two things from our side that may sharpen it, plus the raw logs.
1. The dangling subscriptions in our runs are
backend=node, notbackend=windows. In our logs thebackend appears to be chosen per subscription kind:
type=directory(the config dirC:\Users\User\.config\opencode)backend=windowstype=entries(every ancestor directory) andtype=file(eachAGENTS.md)backend=nodeThe subscriptions left behind when the directory is removed are all
type=entries→backend=node(e.g.
watcher started path="…\oc-r11-112034" type=entries backend=node, same for…\work), and forthose paths
watcher subscribe/watcher started= 4 each withwatcher stopped= 0 in threeseparate runs (97
watcher stoppedlines exist elsewhere in the log). So if the backend is picked bysubscription kind, the leak/hot-loop may be in the shared (node) path rather than only in the
@parcel/watcher/ReadDirectoryChangesWone — worth checking before someone spends a day in theWindows backend.
2. The 400 half is already covered. We found #37650 / PR #54090 ("drop undefined permission metadata
values before publishing requests"), which is exactly our
metadata.include400 — so please don't spendtime on that part of the report. The new part is only what happens to the location/watcher afterwards.
(Once #54090 lands the first ingredient becomes avoidable, but step 4 — the watched directory
disappearing while the server still holds the location — is the one that needs the fix. I have not yet
tested whether the stuck request can be replaced by something else that keeps a location alive; happy to
run that variant if useful: pin a location with a listable pending request (
bash: ask, never answered),then delete the directory.)
What we can provide on Windows if a maintainer wants it: per-thread sampling (we already have
tid-level data — the spin is on the JS thread, 92–94% of its time in kernel mode, ~316k control-class
syscalls/s, and it still yields to the event loop, which is why
/api/infostays at 1 ms), ETW/.wprtraces of the kernel side (needs elevation), or extra logging in a local build of your choice. The repro
takes ~90 s end to end.
Raw evidence from the verification runs (PowerShell, one-second sampling):
spin-evidence-r11.log— full recipe run (spin starts ~5 s after the directory is removed)spin-evidence-r11b.log,r11b-inspector-evidence.json— live inspector probes while it was spinningspin-evidence-r11c.log— per-thread sampling and the JS-thread identity experimentspin-evidence-r4.log,spin-evidence-r5.log,spin-evidence-r7.log— earlier runs incl. the bisectThey contain local paths of the form
C:\Users\User\…. Above, "run A/B/C/D/E" refers to the bisect tableand "R4…R11c" to these files.
r11b-inspector-evidence.json
r11-inspector-evidence.json
spin-evidence-r4.log
spin-evidence-r5.log
spin-evidence-r7.log
spin-evidence-r11.log
spin-evidence-r11b.log
spin-evidence-r11c.log
aurele-l commentedon Oct 27, 2026
Same fingerprint on a newer build, without the blocked-permission ingredient — the two ingredients in the title are not both required.
Environment
@opencode/cli,opencode.exe serve --service)Measured while spinning (pid 18092, started 09:31:29, replaced 11:56)
IO Other Operations/sec/api/info,/api/sessionWhat the log does have
The only
failed to watch directoryentries in the whole log (5) are a removed worktree's directory:Both are
git worktree removed directories that opencode still tried to arm on during a reload cycle, i.e. the same class as theirtype=entries(backend=node) dangling subscriptions — here the re-arm fails loudly, but nothing releases the subscriber that was armed while the path still existed.Re-creating the 18 missing watched directories (including those two, plus the three
.git/worktrees/*entries) and force-rewriting a watched config file moved nothing: 96.9% → 101.6%. No recovery.Runtime half
I can reproduce the loop without opencode: watch a directory with
fs.watch, delete the directory, and Bun 1.4.4 on Windows delivers ~500 events/s while burning 88–97% of a core, ~90% of it kernel time; re-creating the directory at the same path does not stop it,w.close()stops it instantly. Filed at oven-sh/bun#47068 with the script, and it is the same defect nodejs/node#61398 describes (permanent delete → directory moves to$Extend\$Deleted, handle stays valid,ReadDirectoryChangesWreturns the same record forever; libuv fix libuv/libuv#5013, released in libuv v1.53.0).What clears it
opencode service restart— exit 0 in ~0.5 s, new pid, TUI reconnects, sessions survive. The fresh process: 129 watcher lines, 0 failures, 0.94–3.75% CPU,ioOther33–148/s. A turn in flight dies, which is the only cost.Suggestion
Release the subscription whose target disappeared, and log it, instead of leaving the handle armed: either drop it (the location's watch set is rebuilt per reload cycle anyway) or re-check the path with backoff before re-arming. Currently one removed worktree permanently costs a core until the process is restarted, and nothing in the logs, the API or the metrics reflects it.
aurele-l commentedon Oct 27, 2026
Second run on the same machine and version, this time with a clean causal chain — and it shows that releasing the subscription is not enough.
11:59:35.309watcher subscribe+watcher startedpath="...\worktree\549db2\bump-1.0.0-10" type=entries backend=node~12:04:2xgit worktree remove ...\worktree\549db2\bump-1.0.0-10(worktree teardown, logged as a spawned process)12:04:45.353watcher stoppedpath="...\worktree\549db2\bump-1.0.0-10" type=entries— the release path does run and does logIO Other Operations/sec322k–348k, read/s 0, write/s ~0.So: no permission request involved at all, and
watcher stoppedbeing logged does not stop the loop. The JS-side release happens, the native read loop keeps spinning. Closing a Bunfs.watchwhile its directory still exists stops it instantly (verified), so the state that matters is a handle armed on a directory that was deleted underneath it — it moves to$Extend\$Deleted, the handle stays valid andReadDirectoryChangesWcompletes with the same record forever (nodejs/node#61398, libuv fix libuv/libuv#5013 released in libuv v1.53.0; Bun's own watcher still loops, oven-sh/bun#47068).That reorders the fix, and is the reason I would not close this as "leaked subscription":
type=entriessubscriptions are armed on worktree directories thatworktree_removethen deletes; re-check the path on teardown, or skip arming atype=entrieswatch on a worktree that is about to be removed. A release after the unlink is too late.shishouyuan commentedon Nov 8, 2026
Adding a minimal, permission-free repro on 2.2.6 that lands in the same fingerprint, but with a
type=filesubscription instead oftype=entries.Environment: opencode 2.2.6 (
serve --stdio, bundled with OpenChamber 2.7.0), Windows 11 10.0.26300 x64, embedded Bun 1.4.4, Ryzen 7 7840HS, no plugins on the path used.Repro — no blocked permission, no worktree, no config change:
probe_view\shot1.webp,shot2.webp.FileWatch, feat(core): watch files open in the side panel #57915). The log shows one watch per file:Remove-Item -Recurse -Force probe_viewMeasured on 2.2.6:
IO Other Operations/secRunning, other 32 inWaittype=filesubscriptions never logwatcher stoppedControl: the same delete on a folder with no previewed file does not spin (0.2 s CPU/min afterwards).
Reproduced twice on fresh 2.2.6 processes. The second run is the cleanest: the server had been up for one minute with 4.36 s of CPU, two side-panel watches existed, the directory was removed, and 10 s later the delta over 10 s was +10.02 s (100% of one core) — no ramp-up, no log line. Kernel share 90.0%,
IO Other Operations/sec460k–461k, andwatcher stoppedfor the affected paths: 0.Two things that may narrow it:
type=file→backend=node, i.e. thefs.watch(dirname(target), { recursive: false })path infilesystem/watcher.ts, not@parcel/watcher. Sobackend=windowsis not required for the spin — consistent with @helloSpookle'stype=entriesobservation.Get-Process's cumulativeRead/Write/OtherOperationCountreads 0 during the spin; only PerfMon'sIO Other Operations/secshows the ~465k storm. Anyone checking the cheap counters will wrongly conclude "no I/O".readis not enough — the watch comes from the client opening the file in the side panel. And moving the file away afterwards does not release it either: afterMove-Item b.webpout of the folder,watcher stoppedfor that path stayed 0, i.e. the handle is armed on the directory, not on the file. Deleting the now-empty directory would still be the fatal step.Same libuv/Bun conclusion as yours (nodejs/node#61398, libuv/libuv#5013, oven-sh/bun#47068). On the opencode side, the
FileWatchregistry (#57915) could drop a watch as soon as its directory disappears instead of keeping it until the 24 h expiry, and the watcher could stop re-arming on ENOENT.zhang-xzh commentedon Nov 10, 2026
Independent confirmation on Windows 11 (10.0.26300, 28 logical CPUs), desktop 2.2.6, managed
opencode-cli.exe serve --service. Same subsystem, same signature — but no blocked permission request and nogreptool call was involved, so the "blocked + unlistable request" ingredient in the original recipe is not required.Trigger seen here
Continuously ~100 % of one core ≈ 3 % total CPU in Task Manager while completely idle (10 005 s of thread CPU accumulated on a process 6 h old). It started after worktrees (
M:\…\worktrees\wt-*) were removed while sessions on them were still open; the log has the matchinglocation services evicted/watcher stoppedpairs, but some watchers survive holding handles to directories that no longer exist.Measurements on the live process
Exactly one thread is hot (tid 23320): 4 984 ms CPU per 5 s; the other 42 threads are at 0.
Suspending that thread and sampling RIP/RCX lands almost exclusively in
ntdll!NtNotifyChangeDirectoryFileEx+0x14andntdll!NtQueryInformationFile+0x14— i.e.ReadDirectoryChangesWin a loop.RCX (the handle argument) cycles over a fixed set of directory handles (
0x854 / 0x6B8 / 0xC98 / 0x1098 / 0x1348 / 0xAC8). Enumerating the process handles shows several of them are deleted directories:Probing those handles from outside (
DuplicateHandle+ReadDirectoryChangesW):FALSE,GetLastError() == 5 (ERROR_ACCESS_DENIED), 0 ms — repeated, forever.Control: a freshly started
servewith a cleanHOMEidles at 0.5 % of one core, so this is state-dependent, not an unconditional loop.Minimal OS-level reproduction (no opencode, no blocked tool call)
So a watch handle whose directory disappears makes
ReadDirectoryChangesWfail immediately withERROR_ACCESS_DENIEDforever; the watcher retries with no backoff and the thread spins in kernel mode. That is the same mechanism as described above (92–94 % kernel time), and it needs neither anIO Other Operations/secflood nor a blocked tool call to reach it.Suggested fix
On
ERROR_ACCESS_DENIED/ERROR_INVALID_HANDLE(handle gone or directory deleted), close the watch and drop the subscription instead of retrying; if a retry is wanted, back off (e.g. 1 s → 30 s) — never a zero-delaycontinue.Workaround
Restart the desktop app (or the managed service) — the stale handles are released and CPU returns to ~0.
OPENCODE_DISABLE_FILEWATCHER=1also avoids it entirely (per #62687).Note: 2.2.7 was installed while I was diagnosing, and the service was restarted by the app, which released the stuck handles — I could not test whether 2.2.7 itself changes the retry behaviour. If it recurs on 2.2.7 I will follow up here.