Skip to content

Commit 76b0121

Browse files
committed
fix: add timeout to filewatcher subscriptions
1 parent 02e5a19 commit 76b0121

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

‎packages/opencode/src/file/watcher.ts‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import path from "path"
99
// @ts-ignore
1010
import { createWrapper } from "@parcel/watcher/wrapper"
1111
import { lazy } from "@/util/lazy"
12+
import { withTimeout } from "@/util/timeout"
1213
import type ParcelWatcher from "@parcel/watcher"
1314
import { $ } from "bun"
1415
import { Flag } from "@/flag/flag"
1516
import { readdir } from "fs/promises"
1617

18+
const SUBSCRIBE_TIMEOUT_MS = 10_000
19+
1720
declare const OPENCODE_LIBC: string | undefined
1821

1922
export namespace FileWatcher {
@@ -64,12 +67,16 @@ export namespace FileWatcher {
6467
const cfgIgnores = cfg.watcher?.ignore ?? []
6568

6669
if (Flag.OPENCODE_EXPERIMENTAL_FILEWATCHER) {
67-
subs.push(
68-
await watcher().subscribe(Instance.directory, subscribe, {
69-
ignore: [...FileIgnore.PATTERNS, ...cfgIgnores],
70-
backend,
71-
}),
72-
)
70+
const pending = watcher().subscribe(Instance.directory, subscribe, {
71+
ignore: [...FileIgnore.PATTERNS, ...cfgIgnores],
72+
backend,
73+
})
74+
const sub = await withTimeout(pending, SUBSCRIBE_TIMEOUT_MS).catch((err) => {
75+
log.error("failed to subscribe to Instance.directory", { error: err })
76+
pending.then((s) => s.unsubscribe()).catch(() => {})
77+
return undefined
78+
})
79+
if (sub) subs.push(sub)
7380
}
7481

7582
const vcsDir = await $`git rev-parse --git-dir`
@@ -81,12 +88,16 @@ export namespace FileWatcher {
8188
if (vcsDir && !cfgIgnores.includes(".git") && !cfgIgnores.includes(vcsDir)) {
8289
const gitDirContents = await readdir(vcsDir).catch(() => [])
8390
const ignoreList = gitDirContents.filter((entry) => entry !== "HEAD")
84-
subs.push(
85-
await watcher().subscribe(vcsDir, subscribe, {
86-
ignore: ignoreList,
87-
backend,
88-
}),
89-
)
91+
const pending = watcher().subscribe(vcsDir, subscribe, {
92+
ignore: ignoreList,
93+
backend,
94+
})
95+
const sub = await withTimeout(pending, SUBSCRIBE_TIMEOUT_MS).catch((err) => {
96+
log.error("failed to subscribe to vcsDir", { error: err })
97+
pending.then((s) => s.unsubscribe()).catch(() => {})
98+
return undefined
99+
})
100+
if (sub) subs.push(sub)
90101
}
91102

92103
return { subs }

0 commit comments

Comments
 (0)