Skip to content

Commit b6e67fe

Browse files
committed
fix: await plugin event hooks and handle errors in database effects
Plugin event hooks were called without `await`, causing errors to be silently swallowed as unhandled promise rejections. This prevented plugins from reliably receiving bus events like `session.created`. Also adds error handling for database effects (used by Bus.publish) so that async failures in event publishing are logged rather than lost. Closes anomalyco#14808 https://claude.ai/code/session_01FnwVBLHausa5Kmue7hxQAZ
1 parent d961981 commit b6e67fe

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎packages/opencode/src/plugin/index.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,16 @@ export namespace Plugin {
140140
Bus.subscribeAll(async (input) => {
141141
const hooks = await state().then((x) => x.hooks)
142142
for (const hook of hooks) {
143-
hook["event"]?.({
144-
event: input,
145-
})
143+
try {
144+
await hook["event"]?.({
145+
event: input,
146+
})
147+
} catch (err) {
148+
log.error("plugin event hook failed", {
149+
type: input.type,
150+
error: err instanceof Error ? err.message : String(err),
151+
})
152+
}
146153
}
147154
})
148155
}

‎packages/opencode/src/storage/db.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,16 @@ export namespace Database {
138138
if (err instanceof Context.NotFound) {
139139
const effects: (() => void | Promise<void>)[] = []
140140
const result = ctx.provide({ effects, tx: Client() }, () => callback(Client()))
141-
for (const effect of effects) effect()
141+
for (const effect of effects) {
142+
try {
143+
const r = effect()
144+
if (r && typeof r === "object" && "catch" in r) {
145+
;(r as Promise<void>).catch((e) => log.error("database effect failed", { error: e }))
146+
}
147+
} catch (e) {
148+
log.error("database effect failed", { error: e })
149+
}
150+
}
142151
return result
143152
}
144153
throw err
@@ -149,7 +158,14 @@ export namespace Database {
149158
try {
150159
ctx.use().effects.push(fn)
151160
} catch {
152-
fn()
161+
try {
162+
const r = fn()
163+
if (r && typeof r === "object" && "catch" in r) {
164+
;(r as Promise<void>).catch((e) => log.error("database effect failed", { error: e }))
165+
}
166+
} catch (e) {
167+
log.error("database effect failed", { error: e })
168+
}
153169
}
154170
}
155171

@@ -162,7 +178,16 @@ export namespace Database {
162178
const result = (Client().transaction as any)((tx: TxOrDb) => {
163179
return ctx.provide({ tx, effects }, () => callback(tx))
164180
})
165-
for (const effect of effects) effect()
181+
for (const effect of effects) {
182+
try {
183+
const r = effect()
184+
if (r && typeof r === "object" && "catch" in r) {
185+
;(r as Promise<void>).catch((e) => log.error("database effect failed", { error: e }))
186+
}
187+
} catch (e) {
188+
log.error("database effect failed", { error: e })
189+
}
190+
}
166191
return result
167192
}
168193
throw err

0 commit comments

Comments
 (0)