fix(plugin): add dispose lifecycle for full teardown on reload
Plugin created managers, hooks, intervals, and process listeners on every load but had no teardown mechanism. On plugin reload, old instances remained alive causing cumulative memory leaks. - Add createPluginDispose() orchestrating shutdown sequence: backgroundManager.shutdown() → skillMcpManager.disconnectAll() → disposeHooks() - Add disposeHooks() aggregator with safe optional chaining - Wire dispose into index.ts to clean previous instance on reload - Make dispose idempotent (safe to call multiple times) Tests: 4 pass, 8 expects
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
export type PluginDispose = () => Promise<void>
|
||||
|
||||
export function createPluginDispose(args: {
|
||||
backgroundManager: {
|
||||
shutdown: () => void
|
||||
}
|
||||
skillMcpManager: {
|
||||
disconnectAll: () => Promise<void>
|
||||
}
|
||||
disposeHooks: () => void
|
||||
}): PluginDispose {
|
||||
const { backgroundManager, skillMcpManager, disposeHooks } = args
|
||||
let disposePromise: Promise<void> | null = null
|
||||
|
||||
return async (): Promise<void> => {
|
||||
if (disposePromise) {
|
||||
await disposePromise
|
||||
return
|
||||
}
|
||||
|
||||
disposePromise = (async (): Promise<void> => {
|
||||
backgroundManager.shutdown()
|
||||
await skillMcpManager.disconnectAll()
|
||||
disposeHooks()
|
||||
})()
|
||||
|
||||
await disposePromise
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user