summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAiden Cline <[email protected]>2025-12-29 16:16:11 -0600
committerAiden Cline <[email protected]>2025-12-29 16:16:38 -0600
commit76b012139af40c64d8dbd0dc0b1a53242a52df6b (patch)
treea30326429a492ddab531e61129f795b77c3a0ec0
parent02e5a1924298261234504acb4a6450506dd59034 (diff)
downloadopencode-76b012139af40c64d8dbd0dc0b1a53242a52df6b.tar.gz
opencode-76b012139af40c64d8dbd0dc0b1a53242a52df6b.zip
fix: add timeout to filewatcher subscriptions
-rw-r--r--packages/opencode/src/file/watcher.ts35
1 files changed, 23 insertions, 12 deletions
diff --git a/packages/opencode/src/file/watcher.ts b/packages/opencode/src/file/watcher.ts
index b9a85bffb..334b2d264 100644
--- a/packages/opencode/src/file/watcher.ts
+++ b/packages/opencode/src/file/watcher.ts
@@ -9,11 +9,14 @@ import path from "path"
// @ts-ignore
import { createWrapper } from "@parcel/watcher/wrapper"
import { lazy } from "@/util/lazy"
+import { withTimeout } from "@/util/timeout"
import type ParcelWatcher from "@parcel/watcher"
import { $ } from "bun"
import { Flag } from "@/flag/flag"
import { readdir } from "fs/promises"
+const SUBSCRIBE_TIMEOUT_MS = 10_000
+
declare const OPENCODE_LIBC: string | undefined
export namespace FileWatcher {
@@ -64,12 +67,16 @@ export namespace FileWatcher {
const cfgIgnores = cfg.watcher?.ignore ?? []
if (Flag.OPENCODE_EXPERIMENTAL_FILEWATCHER) {
- subs.push(
- await watcher().subscribe(Instance.directory, subscribe, {
- ignore: [...FileIgnore.PATTERNS, ...cfgIgnores],
- backend,
- }),
- )
+ const pending = watcher().subscribe(Instance.directory, subscribe, {
+ ignore: [...FileIgnore.PATTERNS, ...cfgIgnores],
+ backend,
+ })
+ const sub = await withTimeout(pending, SUBSCRIBE_TIMEOUT_MS).catch((err) => {
+ log.error("failed to subscribe to Instance.directory", { error: err })
+ pending.then((s) => s.unsubscribe()).catch(() => {})
+ return undefined
+ })
+ if (sub) subs.push(sub)
}
const vcsDir = await $`git rev-parse --git-dir`
@@ -81,12 +88,16 @@ export namespace FileWatcher {
if (vcsDir && !cfgIgnores.includes(".git") && !cfgIgnores.includes(vcsDir)) {
const gitDirContents = await readdir(vcsDir).catch(() => [])
const ignoreList = gitDirContents.filter((entry) => entry !== "HEAD")
- subs.push(
- await watcher().subscribe(vcsDir, subscribe, {
- ignore: ignoreList,
- backend,
- }),
- )
+ const pending = watcher().subscribe(vcsDir, subscribe, {
+ ignore: ignoreList,
+ backend,
+ })
+ const sub = await withTimeout(pending, SUBSCRIBE_TIMEOUT_MS).catch((err) => {
+ log.error("failed to subscribe to vcsDir", { error: err })
+ pending.then((s) => s.unsubscribe()).catch(() => {})
+ return undefined
+ })
+ if (sub) subs.push(sub)
}
return { subs }