Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): hand takeovers back before an uninstall removes anything

Yudhi Armyndharisef1ae7359de5
diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.ts
index 18ded31e3..ab5aa2e17 100644
--- a/src/core/plugins/plugin-loader.service.ts
+++ b/src/core/plugins/plugin-loader.service.ts
@@ -17,6 +17,7 @@ import {
} from './plugin.interfaces';
import { PluginStorageService } from './plugin-storage.service';
import { PluginHostServices } from './plugin-host-services';
+import type { PluginConversationMappingPort } from './plugin-host-ports';
import { PluginCapabilityContext } from './plugin-capability-context';
import { PluginSandboxBridge } from './plugin-sandbox-bridge';
import { PluginPackageScanner } from './plugin-package-scanner';
@@ -240,7 +241,7 @@ export class PluginLoaderService implements OnModuleInit, OnApplicationBootstrap
this.pluginsDir,
pluginId => this.lifecycle.unloadPlugin(pluginId),
this.legacyPluginsDir,
- pluginId => this.hostServices.getConversationMappingPort().releaseTakeovers(pluginId),
+ pluginId => this.releaseTakeovers(pluginId),
);
}
@@ -248,6 +249,17 @@ export class PluginLoaderService implements OnModuleInit, OnApplicationBootstrap
this.scanner.scanAtBoot();
}
+ /** Hand back the chats a plugin took over. A host without the conversation store has none to hand back. */
+ private releaseTakeovers(pluginId: string): Promise<void> {
+ let port: PluginConversationMappingPort;
+ try {
+ port = this.hostServices.getConversationMappingPort();
+ } catch {
+ return Promise.resolve();
+ }
+ return port.releaseTakeovers(pluginId);
+ }
+
/**
* Re-enable the plugins the operator had enabled (#856). `status` cannot carry that across a restart
* — it describes the runtime, and loading never runs a plugin — so the decision is read from the
diff --git a/src/core/plugins/plugin-uninstaller.spec.ts b/src/core/plugins/plugin-uninstaller.spec.ts
index 5d22b1a08..969792198 100644
--- a/src/core/plugins/plugin-uninstaller.spec.ts
+++ b/src/core/plugins/plugin-uninstaller.spec.ts
@@ -168,28 +168,37 @@ describe('PluginUninstaller — uninstall', () => {
});
// A takeover the plugin held would keep every other plugin silent on that chat for good.
- it('hands the takeovers of the plugin back once it is gone, and survives that failing', async () => {
+ it('hands the takeovers of the plugin back before it removes anything', async () => {
const dir = writeUserPlugin('relay');
seedEntry(storage, 'relay');
- const release = jest.fn().mockResolvedValue(undefined);
const config = { get: (k: string) => (k === 'plugins.dir' ? pluginsDir : tmpDir) } as unknown as ConfigService;
- const withRelease = makeUninstaller(config, storage, pluginsDir, release);
+ const onDisk: boolean[] = [];
+ const withRelease = makeUninstaller(config, storage, pluginsDir, () => {
+ onDisk.push(fs.existsSync(dir) && storage.getPluginEntry('relay') !== undefined);
+ return Promise.resolve();
+ });
withRelease.plugins.set('relay', loadedUserPlugin('relay', dir));
await withRelease.uninstaller.uninstallPlugin('relay');
- expect(release).toHaveBeenCalledWith('relay');
+ expect(onDisk).toEqual([true]);
expect(fs.existsSync(dir)).toBe(false);
+ });
- const dir2 = writeUserPlugin('relay2');
- seedEntry(storage, 'relay2');
- const failing = makeUninstaller(config, storage, pluginsDir, () => {
- throw new Error('mapping store unavailable');
- });
- failing.plugins.set('relay2', loadedUserPlugin('relay2', dir2));
+ it('keeps the plugin installed when its takeovers cannot be handed back, so the call can be repeated', async () => {
+ const dir = writeUserPlugin('relay');
+ seedEntry(storage, 'relay');
+ const config = { get: (k: string) => (k === 'plugins.dir' ? pluginsDir : tmpDir) } as unknown as ConfigService;
+ const failing = makeUninstaller(config, storage, pluginsDir, () =>
+ Promise.reject(new Error('mapping store unavailable')),
+ );
+ failing.plugins.set('relay', loadedUserPlugin('relay', dir));
+
+ await expect(failing.uninstaller.uninstallPlugin('relay')).rejects.toThrow('mapping store unavailable');
- await expect(failing.uninstaller.uninstallPlugin('relay2')).resolves.toBeUndefined();
- expect(fs.existsSync(dir2)).toBe(false);
+ expect(fs.existsSync(dir)).toBe(true);
+ expect(storage.getPluginEntry('relay')).toBeDefined();
+ expect(failing.plugins.has('relay')).toBe(true);
});
});
diff --git a/src/core/plugins/plugin-uninstaller.ts b/src/core/plugins/plugin-uninstaller.ts
index 78514e8da..b8c3ab8ee 100644
--- a/src/core/plugins/plugin-uninstaller.ts
+++ b/src/core/plugins/plugin-uninstaller.ts
@@ -52,6 +52,11 @@ export class PluginUninstaller {
throw new Error(`Cannot uninstall built-in plugin ${pluginId}`);
}
+ // A takeover the plugin held keeps every other plugin silent on that chat, and nothing is left to
+ // hand it back once the plugin is gone. Done first, while the registry entry still exists: a store
+ // that cannot take it fails the uninstall, which the operator can then repeat.
+ await this.releaseTakeovers?.(pluginId);
+
// Read the package location BEFORE unloading: unloadPlugin drops the runtime record that holds
// it, and for a plugin loaded from the legacy directory the configured root is the wrong tree —
// deleting there removes the ctx.storage directory and leaves the code, so the plugin comes
@@ -87,18 +92,6 @@ export class PluginUninstaller {
// Best-effort, and strictly that one plugin's directory.
this.pluginStorage.deletePluginData(pluginId);
- // A takeover the plugin held keeps every other plugin silent on that chat, and nothing is left to
- // hand it back. Best-effort: the plugin is gone either way.
- try {
- await this.releaseTakeovers?.(pluginId);
- } catch (error) {
- this.logger.warn(`Could not release the takeovers of uninstalled plugin ${pluginId}`, {
- pluginId,
- action: 'plugin_takeover_release_failed',
- error: error instanceof Error ? error.message : String(error),
- });
- }
-
this.logger.log(`Plugin uninstalled: ${pluginId}`, { pluginId, action: 'plugin_uninstalled' });
}
}