Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): clear a plugin's hook error when its worker generation starts
Yudhi Armyndharis694502a2b53b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1888b0734..6052a4767 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -90,6 +90,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- **Plugin health no longer reports a hook error from a worker that already died.** The last+ hook-handler error a sandboxed plugin reported is operator context on+ `GET /api/plugins/{id}/health`, and it is scoped to one worker generation — the field's contract was+ "a fresh enable starts from a clean slate". It was cleared only on `disablePlugin`, but a worker+ crash and a failed enable both end a generation without going through disable. The replacement+ worker therefore inherited the dead one's error, and health reported it as current: an operator+ restarting a plugin to clear a fault saw the same fault reported against the healthy worker that+ replaced it.++ The record is now cleared where a generation *starts*, so it holds for every way one can end rather+ than for the single path that happened to be handled.+- **A rolled-back `POST /api/infra/import-data` no longer denies the engines it already stopped.** Theorphan pre-flight runs *before* the transaction opens, and `stopOrphans: true` really destroys thoseengines there — a teardown the rollback cannot undo. Both rollback branches nevertheless returned adiff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.tsindex 0abff0050..4b486aa25 100644--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts@@ -226,6 +226,26 @@ describe('PluginLoaderService — sandbox hook error surfacing', () => {expect(after.message ?? '').not.toContain('last hook error');});+ it('does not carry a dead generation’s hook error into the worker that replaces it', async () => {+ // The record is cleared on disable, but a crash never goes through disable. Without a clear on+ // enable, the next worker inherits the previous one's error and checkPluginHealth reports it as+ // current — the field's own contract says a fresh enable starts from a clean slate.+ const loader = makeLoader();+ const handler = await setupShim(loader, 'message:sent');+ loader.hosts[0].dispatchHook.mockResolvedValue({ continue: true, error: 'boom' });+ jest.spyOn(loggerOf(loader), 'warn').mockImplementation(() => undefined);++ await handler({ data: {}, sessionId: 's1', source: 'Engine' });+ expect((await loader.checkPluginHealth('p1')).message).toContain('last hook error');++ // Worker crashes (intentional=false): the host is dropped without any disable running.+ loader.capturedOnWorkerExit!(1, false);+ await loader.enablePlugin('p1');++ const after = await loader.checkPluginHealth('p1');+ expect(after.message ?? '').not.toContain('last hook error');+ });+it('does not log or record anything when the worker reports no error', async () => {const loader = makeLoader();const handler = await setupShim(loader, 'message:sent');diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.tsindex 8ccbf9679..de3ac3052 100644--- a/src/core/plugins/plugin-loader.service.ts+++ b/src/core/plugins/plugin-loader.service.ts@@ -194,7 +194,9 @@ export class PluginLoaderService implements OnModuleInit, OnApplicationBootstrap// Live worker host per enabled sandboxed (untrusted) plugin. Built-ins are not in here.private readonly sandboxHosts = new Map<string, PluginWorkerHost>();// Last hook-handler error each sandboxed plugin's worker reported, surfaced via checkPluginHealth so a- // hook that keeps throwing is visible to the operator. Cleared on disable (fresh enable = fresh slate).+ // hook that keeps throwing is visible to the operator. Scoped to ONE worker generation: cleared when a+ // generation starts (enableSandboxed) and when one is deliberately ended (disablePlugin). Clearing at+ // the start is what makes it hold for a crash or a failed enable, neither of which runs a disable.private readonly lastSandboxHookError = new Map<string, { event: string; error: string; at: Date }>();// Carries the firing event's sessionId across an in-process hook handler so ctx.config (a getter)// resolves the per-session slice. Per async call tree, so concurrent sessions don't cross over.@@ -1078,6 +1080,12 @@ export class PluginLoaderService implements OnModuleInit, OnApplicationBootstrap* tears the worker back down.*/private async enableSandboxed(pluginId: string, plugin: PluginInstance): Promise<void> {+ // A new worker generation starts from a clean slate. disablePlugin clears this too, but a crash+ // and a failed enable both end a generation WITHOUT going through disable — so clearing only+ // there let the replacement worker inherit a dead one's hook error and report it through+ // checkPluginHealth as current. Enforced here, at the one point every generation begins, rather+ // than repeated on each way a generation can end.+ this.lastSandboxHookError.delete(pluginId);// Containment guard: reject a manifest.main that escapes the plugin dir.const mainPath = resolvePluginMainPath(this.pluginsDir, pluginId, plugin.manifest.main);// The capability dispatcher runs a worker request through the SAME context an in-process plugin