From 512fc51f3ed28ee94817b277460c7e1f37991d71 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 11 Jul 2026 21:52:52 +0200 Subject: [PATCH] fix(plugins): reap a queued erasure only once the plugin's data is gone The orphan reap deleted every queue row whose plugin had left the registry, but uninstall(deleteData=false) removes the plugins row while deliberately keeping the data dir AND the queued erasure so a same-id reinstall can still honour it. The reap now deletes a row only when the plugin's data dir is actually gone; a deleteData=true uninstall already clears the rows itself. --- .../nest/plugins/plugin-runtime.service.ts | 21 +++++++++++++---- .../plugins/plugin-runtime.test.ts | 23 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/server/src/nest/plugins/plugin-runtime.service.ts b/server/src/nest/plugins/plugin-runtime.service.ts index 3915ac0be..4c3d3a423 100644 --- a/server/src/nest/plugins/plugin-runtime.service.ts +++ b/server/src/nest/plugins/plugin-runtime.service.ts @@ -21,7 +21,7 @@ import { discoverPlugins } from './install/discovery'; import { parseJsonText, parseManifest } from './install/manifest'; import { scanForNativeBinaries } from './install/native-scan'; import { devLinkEnabled, DEV_LINK_SOURCE } from './dev-link'; -import { pluginCodeDir } from './paths'; +import { pluginCodeDir, pluginDataDir } from './paths'; import { PluginRegistryService } from './registry/registry.service'; import { isAddonEnabled } from '../../services/adminService'; import type { PluginDependency } from './install/manifest'; @@ -281,10 +281,21 @@ export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy { private async runDrainOnce(): Promise { if (!pluginsEnabled()) return; try { - // Reap rows whose plugin no longer exists (uninstalled): its data dir was removed - // with it, so the erasure is already satisfied, and the plugin will never reactivate - // to ACK the row — left alone it would linger in the queue forever. - db.prepare('DELETE FROM plugin_user_erasure_queue WHERE plugin_id NOT IN (SELECT id FROM plugins)').run(); + // Reap a queued erasure only when its plugin is gone from the registry AND its data + // dir is actually deleted — then the data (and the obligation) is truly gone. A plugin + // uninstalled with "keep data" (deleteData=false) removes the plugins row but + // DELIBERATELY keeps the data dir and the queue row so a same-id reinstall can still + // honour the erasure (see uninstall()); reaping on registry-absence alone would wipe + // exactly those preserved obligations. A deleteData=true uninstall already clears the + // rows itself, so this only ever needs to catch a truly orphaned data dir. + const orphans = db + .prepare('SELECT DISTINCT plugin_id FROM plugin_user_erasure_queue WHERE plugin_id NOT IN (SELECT id FROM plugins)') + .all() as Array<{ plugin_id: string }>; + for (const { plugin_id } of orphans) { + if (!fs.existsSync(pluginDataDir(plugin_id))) { + db.prepare('DELETE FROM plugin_user_erasure_queue WHERE plugin_id = ?').run(plugin_id); + } + } // Only ACTIVE plugins can be delivered to; scope the window to them so a backlog // of erasures for permanently-inactive plugins can't starve deliverable ones. const active = this.supervisor.activeIds(); diff --git a/server/tests/integration/plugins/plugin-runtime.test.ts b/server/tests/integration/plugins/plugin-runtime.test.ts index a6cbc2e2f..ffa4b284f 100644 --- a/server/tests/integration/plugins/plugin-runtime.test.ts +++ b/server/tests/integration/plugins/plugin-runtime.test.ts @@ -194,6 +194,29 @@ describe('PluginRuntimeService (M2 end-to-end)', () => { await runtime.deactivate('gdpr'); }); + it('the drain reap keeps a queued erasure while the plugin data dir survives (uninstall keep-data), reaping only once the data is gone', async () => { + // uninstall(deleteData=false) removes the plugins row but DELIBERATELY keeps the data + // dir and the queued erasure so a same-id reinstall can still honour it. The orphan + // reap must not delete that row while the data dir is still present. + const id = 'reaptest'; + const dir = path.join(dataRoot, id); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(path.join(dir, 'plugin.db'), 'x'); + testDb.prepare('INSERT INTO plugin_user_erasure_queue (plugin_id, user_id) VALUES (?, ?)').run(id, 9); + // the plugin is gone from the registry (uninstalled) but its data dir remains + expect(testDb.prepare('SELECT id FROM plugins WHERE id = ?').get(id)).toBeUndefined(); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await (runtime as any).drainUserErasures(); + expect((testDb.prepare("SELECT COUNT(*) c FROM plugin_user_erasure_queue WHERE plugin_id='reaptest'").get() as { c: number }).c).toBe(1); + + // now the data is truly gone (deleteData=true / manual removal) → the row is reaped + fs.rmSync(dir, { recursive: true, force: true }); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await (runtime as any).drainUserErasures(); + expect((testDb.prepare("SELECT COUNT(*) c FROM plugin_user_erasure_queue WHERE plugin_id='reaptest'").get() as { c: number }).c).toBe(0); + }); + it('leaves an erasure queued for a plugin that is offline, to run when it is back', async () => { // granted the hook but NOT active → enqueue keeps the row, drain leaves it testDb.prepare("INSERT INTO plugins (id, status, permissions, config) VALUES ('offliner','inactive','[\"db:own\",\"hook:user-data\"]','{}')").run();