Skip to content

Commit b0d6ef6

Browse files
author
Roi
committed
fix(core): tolerate file watcher startup failures
Fixes anomalyco#16610 Related anomalyco#26842 Related anomalyco#23190 The file watcher initialization could fail in ways that made the entire TUI unresponsive or crash: - Effect.promise did not catch synchronous throws from @parcel/watcher's subscribe() call; Effect.tryPromise does - The .git watcher setup ran inside the main layer body, so any failure (git.dir timeout, subscribe timeout) killed the entire watcher service including the experimental root watcher; it is now isolated in its own forkScoped with an independent catchCause - The @parcel/watcher callback's error parameter was silently ignored; it is now logged - Unsubscribe errors during cleanup could cause unhandled rejections; they are now suppressed - The outer catchCause log message now includes the directory and explains the consequence (watcher disabled) Reproduction: OpenCode 1.17.8 / Bun 1.3.14 in a Linux dev container with overlayfs crashes with exit code 132 (SIGILL) and Bun panic 'Segmentation fault at address 0x0' when run inside a Git repo. OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER=true avoids the crash.
1 parent 355a0bc commit b0d6ef6

1 file changed

Lines changed: 33 additions & 14 deletions

File tree

‎packages/core/src/filesystem/watcher.ts‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ export const layer = Layer.effect(
8686
const runFork = Effect.runForkWith(context)
8787
const subscriptions: ParcelWatcher.AsyncSubscription[] = []
8888
yield* Effect.addFinalizer(() =>
89-
Effect.promise(() => Promise.allSettled(subscriptions.map((subscription) => subscription.unsubscribe()))),
89+
Effect.promise(() =>
90+
Promise.allSettled(subscriptions.map((subscription) => subscription.unsubscribe().catch(() => {}))),
91+
),
9092
)
9193

92-
const callback: ParcelWatcher.SubscribeCallback = (_error, updates) => {
94+
const callback: ParcelWatcher.SubscribeCallback = (error, updates) => {
95+
if (error) {
96+
runFork(Effect.logError("watcher subscription error", { error: String(error) }))
97+
return
98+
}
9399
for (const update of updates) {
94100
if (update.type === "create") runFork(events.publish(Event.Updated, { file: update.path, event: "add" }))
95101
if (update.type === "update") runFork(events.publish(Event.Updated, { file: update.path, event: "change" }))
@@ -99,7 +105,7 @@ export const layer = Layer.effect(
99105

100106
const subscribe = (directory: string, ignore: string[]) => {
101107
const pending = w.subscribe(directory, callback, { ignore, backend })
102-
return Effect.promise(() => pending).pipe(
108+
return Effect.tryPromise(() => pending).pipe(
103109
Effect.tap((subscription) => Effect.sync(() => subscriptions.push(subscription))),
104110
Effect.timeout(SUBSCRIBE_TIMEOUT_MS),
105111
Effect.catchCause((cause) => {
@@ -119,22 +125,35 @@ export const layer = Layer.effect(
119125
}
120126

121127
if (location.vcs?.type === "git") {
122-
const resolved = yield* git.dir(location.directory)
123-
const vcs = resolved ? yield* fs.realPath(resolved).pipe(Effect.catch(() => Effect.succeed(resolved))) : undefined
124-
if (vcs && !config.includes(".git") && !config.includes(vcs) && (!resolved || !config.includes(resolved))) {
125-
const ignore = (yield* fs.readDirectoryEntries(vcs).pipe(Effect.catch(() => Effect.succeed([])))).flatMap(
126-
(entry) => (entry.name === "HEAD" ? [] : [entry.name]),
127-
)
128-
yield* Effect.forkScoped(subscribe(vcs, ignore))
129-
}
128+
yield* Effect.forkScoped(
129+
Effect.gen(function* () {
130+
const resolved = yield* git.dir(location.directory)
131+
const vcs = resolved
132+
? yield* fs.realPath(resolved).pipe(Effect.catch(() => Effect.succeed(resolved)))
133+
: undefined
134+
if (vcs && !config.includes(".git") && !config.includes(vcs) && (!resolved || !config.includes(resolved))) {
135+
const ignore = (yield* fs.readDirectoryEntries(vcs).pipe(Effect.catch(() => Effect.succeed([])))).flatMap(
136+
(entry) => (entry.name === "HEAD" ? [] : [entry.name]),
137+
)
138+
yield* subscribe(vcs, ignore)
139+
}
140+
}).pipe(
141+
Effect.catchCause((cause) =>
142+
Effect.logWarning("watcher: git directory subscription failed, continuing without git HEAD tracking", {
143+
directory: location.directory,
144+
cause: Cause.pretty(cause),
145+
}),
146+
),
147+
),
148+
)
130149
}
131150

132151
return Service.of({})
133152
}).pipe(
134153
Effect.catchCause((cause) => {
135-
return Effect.logError("failed to init watcher service", { cause: Cause.pretty(cause) }).pipe(
136-
Effect.as(Service.of({})),
137-
)
154+
return Effect.logError("watcher disabled: initialization failed, continuing without file watching", {
155+
cause: Cause.pretty(cause),
156+
}).pipe(Effect.as(Service.of({})))
138157
}),
139158
),
140159
)

0 commit comments

Comments
 (0)