Skip to content

The agent that grows with you

fix(desktop): tolerate a throwing disposer so a failed plugin load cannot wedge the registry

liuhao1024805ddfba5c3c
diff --git a/apps/desktop/src/contrib/runtime-loader.test.ts b/apps/desktop/src/contrib/runtime-loader.test.ts
index fd2b118343b..cd3a62f3300 100644
--- a/apps/desktop/src/contrib/runtime-loader.test.ts
+++ b/apps/desktop/src/contrib/runtime-loader.test.ts
@@ -832,6 +832,85 @@ describe('register() failure isolation', () => {
restore()
}
})
+
+ it('a rollback disposer that throws cannot wedge the registry: the fixed source still reloads (#126338)', async () => {
+ const restore = withBlobReroute()
+ const counters = globalThis as unknown as Record<string, number | undefined>
+ counters.__wedgeFixedRegister = 0
+
+ try {
+ // v1: register() fails midway AND the disposer rolling it back throws
+ // too — the plugin's own cleanup path is buggy.
+ await loadRuntimePlugin(
+ `export default {
+ id: 'wedge-reload',
+ register(ctx) {
+ ctx.onDispose(() => { throw new Error('dispose boom') })
+ throw new Error('register boom')
+ }
+ }`,
+ 'wedge-reload'
+ )
+
+ // The row reports the registration failure, not the rollback's.
+ expect($pluginRecords.get()['wedge-reload']).toMatchObject({ status: 'error', error: 'register boom' })
+
+ // v2: the file is fixed. The reload must reach the fresh register()
+ // instead of dying re-running the previous incarnation's broken
+ // disposer — the wedge that made every edit look inert until an app
+ // restart.
+ const id = await loadRuntimePlugin(
+ `export default { id: 'wedge-reload', register() { globalThis.__wedgeFixedRegister++ } }`,
+ 'wedge-reload'
+ )
+
+ expect(id).toBe('wedge-reload')
+ expect(counters.__wedgeFixedRegister).toBe(1)
+ expect($pluginRecords.get()['wedge-reload']).toMatchObject({ status: 'loaded' })
+ } finally {
+ unloadRuntimePlugin('wedge-reload')
+ delete counters.__wedgeFixedRegister
+ restore()
+ }
+ })
+
+ it('unloadRuntimePlugin releases the registration even when a disposer throws (#126338)', async () => {
+ const restore = withBlobReroute()
+ const counters = globalThis as unknown as Record<string, number | undefined>
+ counters.__throwingDisposerLoads = 0
+
+ try {
+ await loadRuntimePlugin(
+ `export default {
+ id: 'throwing-disposer',
+ register(ctx) {
+ ctx.onDispose(() => { throw new Error('dispose boom') })
+ globalThis.__throwingDisposerLoads++
+ }
+ }`,
+ 'throwing-disposer'
+ )
+
+ expect($pluginRecords.get()['throwing-disposer']).toMatchObject({ status: 'loaded' })
+
+ expect(() => unloadRuntimePlugin('throwing-disposer')).not.toThrow()
+
+ // The registration was released — reloading registers fresh instead of
+ // tripping over the retained disposer list.
+ const id = await loadRuntimePlugin(
+ `export default { id: 'throwing-disposer', register() { globalThis.__throwingDisposerLoads++ } }`,
+ 'throwing-disposer'
+ )
+
+ expect(id).toBe('throwing-disposer')
+ expect(counters.__throwingDisposerLoads).toBe(2)
+ expect($pluginRecords.get()['throwing-disposer']).toMatchObject({ status: 'loaded' })
+ } finally {
+ unloadRuntimePlugin('throwing-disposer')
+ delete counters.__throwingDisposerLoads
+ restore()
+ }
+ })
})
describe('remote static imports are refused (catalog trust)', () => {
diff --git a/apps/desktop/src/contrib/runtime-loader.ts b/apps/desktop/src/contrib/runtime-loader.ts
index 1bd2810c7b1..a72c12d610f 100644
--- a/apps/desktop/src/contrib/runtime-loader.ts
+++ b/apps/desktop/src/contrib/runtime-loader.ts
@@ -357,8 +357,23 @@ function unsupportedImports(source: string): string[] {
}
export function unloadRuntimePlugin(id: string): void {
- loaded.get(id)?.forEach(dispose => dispose())
+ const disposers = loaded.get(id)
+
+ // Released BEFORE the disposers run, and each disposer in its own
+ // try/catch: a disposer with its own bug must not wedge the registry
+ // (#126338). With a bare forEach the throw aborted the loop and stranded
+ // the delete, so every later reload re-ran the same broken disposers and
+ // died before the fresh register() — file edits looked inert until an
+ // app restart.
loaded.delete(id)
+
+ disposers?.forEach(dispose => {
+ try {
+ dispose()
+ } catch (error) {
+ console.error(`[plugins] ${id}: disposer failed during unload`, error)
+ }
+ })
}
/** Evaluate + register one runtime plugin. Returns its id, or null on failure. */
@@ -450,13 +465,16 @@ export async function loadRuntimePlugin(
packageOrigin: options.packageOrigin
}
- const failRegistration = (disposers: (() => void)[], error: unknown) => {
+ const failRegistration = (error: unknown) => {
// Roll back everything register() managed before it failed — a
// half-registered plugin must not leave live contributions/listeners
// nobody can ever dispose — and land the failure on the plugin's OWN
// row so Capabilities → Plugins shows it (the toggle stays usable).
- disposers.forEach(dispose => dispose())
- loaded.delete(plugin.id)
+ // unloadRuntimePlugin() tolerates a throwing disposer, so a cleanup
+ // bug in the rollback can neither strand the remaining disposers nor
+ // hold the registration — the #126338 wedge where every later reload
+ // re-ran the same broken disposers instead of the fixed file.
+ unloadRuntimePlugin(plugin.id)
console.error(`[plugins] ${plugin.id} failed to register (${origin})`, error)
notifyError(error, `Plugin "${record.name}" failed to register`)
publishPlugin({ ...record, status: 'error', error: error instanceof Error ? error.message : String(error) })
@@ -477,7 +495,7 @@ export async function loadRuntimePlugin(
() => plugin.register(createPluginContext(plugin.id, dispose => disposers.push(dispose)))
)
} catch (error) {
- failRegistration(disposers, error)
+ failRegistration(error)
return
}
@@ -489,7 +507,7 @@ export async function loadRuntimePlugin(
if (result && typeof (result as PromiseLike<unknown>).then === 'function') {
void Promise.resolve(result).catch((error: unknown) => {
if (loaded.get(plugin.id) === disposers) {
- failRegistration(disposers, error)
+ failRegistration(error)
}
})
}