Skip to content

Windows: opencode serve pins one CPU core after a session worktree is deleted (fs.watch on removed directory spins) #67865

Duplicate

Description

@GuilhermeMoreira

Description

On Windows, opencode serve (desktop app backend) gets stuck using ~100% of one CPU core, mostly in kernel time, after a session worktree directory is removed. It never recovers until the app is restarted.

OpenCode keeps a fs.watch watcher on each worktree directory (log lines message="watcher started" ... type=entries backend=node). When one of those directories is deleted (for example when a session's worktree is removed, or removed and recreated), the watcher on the now-deleted directory spins forever.

Observed in the running server

  • A single thread (the main thread) is at ~100% of one core.
  • ~87% of its CPU time is kernel time.
  • ~450k "other" I/O operations per second (not read or write), with 0 bytes transferred.
  • Nothing is logged while it spins.
  • The start of the spin matches the moment a watched worktree directory was deleted and recreated. In a second occurrence, two watched worktree directories had been deleted.

Minimal reproduction (no OpenCode needed)

// repro.cjs — run with: bun repro.cjs   or   node repro.cjs
const fs = require('fs'), path = require('path'), os = require('os');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'watch-'));
let events = 0;
fs.watch(dir, () => events++).on('error', (e) => console.log('error', e.code));
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
(async () => {
  await sleep(2000);
  fs.rmSync(dir, { recursive: true, force: true }); // delete the watched directory itself
  const c0 = process.cpuUsage(), e0 = events;
  await sleep(5000);
  const c = process.cpuUsage(c0);
  console.log(`CPU ${(((c.user + c.system) / 5e6) * 100).toFixed(0)}% (kernel ${((c.system / 5e6) * 100).toFixed(0)}%), events ${events - e0}`);
  process.exit(0);
})();

Results on Windows 11 x64:

Case Bun 1.4.4 (embedded in opencode-cli 2.4.3) Node 24.18
Watched directory itself deleted ~94% of a core, ~82% kernel, events keep firing ~91% of a core, ~73% kernel, events keep firing
Only a subdirectory deleted 0% 0%
Watched file deleted 0% —
@parcel/watcher (backend=windows), watched dir deleted 0% 0%

So the runtime-level behavior is a fs.watch on a deleted directory spinning on Windows, and OpenCode hits it because it never closes watchers whose directory disappeared.

Expected

When a watched directory is deleted, OpenCode should close (and, if needed, re-create) that watcher instead of leaving it attached to the dead directory. For example, on the first event, check fs.existsSync(dir) and call watcher.close() if it is gone. Unsubscribing watchers for a worktree when the worktree is removed would also cover the main path.

Workaround

Restart the OpenCode app after removing session worktrees.

Environment

  • OpenCode desktop 2.4.3 (opencode-cli serve --service), embedded Bun 1.4.4
  • Windows 11 x64

Activity

  1. opencodejr commented on Dec 2, 2026

    @opencodejr
    Contributor

    I think this is a duplicate of #56945. Comments there report the same spin on 2.2.x: one core pinned in kernel time with no log lines, after session worktrees were removed and with no blocked permission involved. They also trace it to fs.watch on a deleted directory.

    I read packages/core/src/filesystem/watcher.ts on dev. The file and entries watches still use fs.watch directly (backend=node), and its error handler only logs. I found nothing that closes the watch once its directory is gone.

    I'm closing this issue now as a duplicate of #56945. Please add your standalone repro and the 2.4.3 / Bun 1.4.4 results to that issue. Replying here reopens this one.

  2. added
    area/core
    type/bug
    version/v2
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/coreplatform/windowstype/bugversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions