anomalyco / anomalyco/opencode

[Bug] EISDIR crash on state.json.lock: stale directory prevents prompt sending after process kill

Open
#40,972 0 comments 0 reactions 1 assignee View on GitHub

@Hona is already working on this.

Since Aug 7, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Describe the bug

When an opencode process crashes or is killed abnormally, state.json.lock can be left behind as a directory instead of a file in .opencode/goals/state.json.sessions/<hash>/. On subsequent runs, publishLegacyGuard() calls fs.link(tmpFile, lockPath) to create a hardlink/symlink lock file, but this fails with EISDIR because the target already exists as a directory. The error propagates up to SessionPrompt.prompt()SessionHttpApi.prompt(), resulting in a user-facing "Unexpected server error" / "fail to send prompt" message.

Steps to reproduce

  1. Run opencode on a project on Windows
  2. Force-kill the opencode process while it's actively processing (e.g., taskkill /f /im opencode.exe)
  3. Restart opencode and send a prompt — observe the EISDIR error in logs

Expected behavior

The lock acquisition should gracefully handle the case where state.json.lock is a directory — either by:

  • Removing the stale directory before attempting to create the lock file, or
  • Catching EISDIR and cleaning up automatically

Actual behavior

EISDIR: illegal operation on a directory
link '...\\state.json.lock.guard.XXXX.tmp' -> '...\\state.json.lock'

The error is uncaught (only EEXIST is handled), so it throws up the stack and blocks all prompt sending. The user sees "Unexpected server error. Check server logs for details."

Error observed in logs

level=ERROR run=9e192e72 message=failed ref=err_92997988
error="Error: EISDIR: illegal operation on a directory,
    at async publishLegacyGuard (persistence-lease.js:345:13)
    at async ensureLegacyGuard (persistence-lease.js:385:29)
    at async acquirePersistenceLeaseWithHooks (persistence-lease.js:537:11)
    ...
    at SessionPrompt.prompt
    at SessionHttpApi.prompt"

Root cause (code analysis)

In persistence-lease.js:322-355:

// Line 322: linkGuard defaults to fs.link() (hard link / symlink)
linkGuard = (source, target) => fs.link(source, target)

// Line 345: the link call — fails with EISDIR if target is a directory
await linkGuard(temporaryPath, lockPath)

// Lines 347-355: error handler — only checks EEXIST, NOT EISDIR
} catch (error) {
  const racedGuard = await inspectLegacyGuard(lockPath)
  if (racedGuard.status !== "missing") { ... }
  if (error?.code === "EEXIST") return null      // ← only EEXIST handled
  if (hardLinkUnsupported(error)) throw ...
  throw error                                      // ← EISDIR falls through here
}

Proposed fix

Add EISDIR handling in the catch block — detect the error code and remove the stale directory before retrying:

} catch (error) {
  // New: handle stale directory left from crash
  if (error?.code === "EISDIR") {
    await fs.rm(lockPath, { recursive: true, force: true })
    await linkGuard(temporaryPath, lockPath)
    linked = true
  } else if (error?.code === "EEXIST") {
    return null
  } else if (hardLinkUnsupported(error)) {
    throw persistenceLeaseHardLinkError()
  } else {
    throw error
  }
}

Environment

  • OS: Windows 11 (win32)
  • opencode version: 1.18.14
  • opencode-goal-plugin path: persistence-lease.js:345
  • Affected projects on drives: K:, I: (network/external drives)
  • Filesystem: NTFS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.