Skip to content

Commit 536dc02

Browse files
author
anqiang.ex
committed
fix(core): keep the generation swap when a plugin cleanup throws
`Plugin.activate` closes the previous generation before loading the new one, and the close loop had no per-plugin isolation. A plugin whose finalizer throws aborted the whole activation: the old builtins had already been closed, the new generation never loaded, and their transforms were never re-registered. Builtin state then fell back to defaults, so a user plugin's broken cleanup silently re-enabled `compaction.auto` despite `compaction: { auto: false }` (#57089). Each old slot now closes independently, logging `failed to close plugin` with the id and cause. One plugin's teardown failure is reported instead of aborting the swap, so the new generation still loads and builtins keep their registrations. Adds a case with a finalizer that throws only for the revision being replaced, so the surviving generation tears down normally and the assertion covers the swap alone. It fails without the change: the error escapes `Plugin.activate` and the builtin command is gone.
1 parent f394d9d commit 536dc02

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎packages/core/src/plugin.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,16 @@ const layer = Layer.effect(
131131
([id, slot]) =>
132132
Effect.gen(function* () {
133133
active.delete(id)
134+
// One plugin's teardown must not abort the swap. Its registrations are already gone,
135+
// and failing here leaves the new generation unloaded with its builtins unregistered,
136+
// which silently resets their state (#57089).
134137
if (slot.activation && !slot.activation.failure)
135-
yield* Scope.close(slot.activation.scope, Exit.void)
138+
yield* Scope.close(slot.activation.scope, Exit.void).pipe(
139+
Effect.catchCauseIf(
140+
(cause) => !Cause.hasInterrupts(cause),
141+
(cause) => Effect.logError("failed to close plugin", { "plugin.id": id, cause }),
142+
),
143+
)
136144
}),
137145
{ discard: true },
138146
)

‎packages/core/test/plugin-failure.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "bun:test"
2-
import { Deferred, Effect, Exit, Fiber, Schema } from "effect"
2+
import { Cause, Deferred, Effect, Exit, Fiber, Schema } from "effect"
33
import { Bus } from "@opencode/core/bus"
44
import { Command } from "@opencode/core/command"
55
import { Plugin } from "@opencode/core/plugin"
@@ -10,6 +10,42 @@ import { PluginTestLayer } from "./plugin/fixture"
1010

1111
const it = testEffect(PluginTestLayer)
1212

13+
// A plugin whose teardown throws must not abort the generation swap: the old generation is already
14+
// closed by then, so aborting leaves the new one unloaded and its builtin transforms unregistered,
15+
// which silently reset builtin state such as `compaction.auto: false` (#57089).
16+
it.live("keeps the new generation when a plugin cleanup throws", () =>
17+
Effect.gen(function* () {
18+
const plugins = yield* Plugin.Service
19+
const commands = yield* Command.Service
20+
const broken = (revision: string): Plugin.Generation => ({
21+
id: "broken",
22+
revision,
23+
effect: () =>
24+
Effect.gen(function* () {
25+
// Only the revision being replaced has the throwing cleanup, so the generation that survives the
26+
// swap tears down normally and the case fails for the swap alone.
27+
if (revision === "1")
28+
yield* Effect.addFinalizer(() => Effect.die(new ReferenceError("cleanup binding is not defined")))
29+
}),
30+
})
31+
const builtin = (revision: string): Plugin.Generation => ({
32+
id: "builtin",
33+
revision,
34+
// Last in activation order, like `internal.post`, so the reverse close loop reaches it first.
35+
effect: (ctx) =>
36+
ctx.command.transform((editor) => {
37+
editor.add({ name: "builtin", description: revision, execute: () => Effect.void })
38+
}),
39+
})
40+
41+
yield* plugins.activate([broken("1"), builtin("1")])
42+
expect((yield* commands.get("builtin"))?.description).toBe("1")
43+
44+
yield* plugins.activate([broken("2"), builtin("2")])
45+
expect((yield* commands.get("builtin"))?.description).toBe("2")
46+
}),
47+
)
48+
1349
it.live("removes a failed plugin's hooks and RPC handlers without affecting healthy plugins", () =>
1450
Effect.gen(function* () {
1551
const plugins = yield* Plugin.Service
@@ -303,3 +339,18 @@ it.live("settles readiness before shutdown joins a disabled plugin's finalizers"
303339
expect(restarted).toBe(false)
304340
}),
305341
)
342+
343+
// Interruption from a cleanup finalizer is not a plugin failure. Swallowing it would turn a cancel
344+
// during teardown into a successful activation, so it must keep propagating.
345+
it.live("preserves interruption during plugin cleanup", () =>
346+
Effect.gen(function* () {
347+
const plugins = yield* Plugin.Service
348+
yield* plugins.activate([
349+
{ id: "interrupted", revision: "1", effect: () => Effect.addFinalizer(() => Effect.interrupt) },
350+
])
351+
const activation = yield* plugins.activate([]).pipe(Effect.forkChild({ startImmediately: true }))
352+
const exit = yield* Fiber.await(activation)
353+
expect(Exit.isFailure(exit)).toBe(true)
354+
if (Exit.isFailure(exit)) expect(Cause.hasInterrupts(exit.cause)).toBe(true)
355+
}),
356+
)

0 commit comments

Comments
 (0)