Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): catch a synchronous throw from a sandboxed onConfigChange
Yudhi Armyndharis1c7bd9b9ec47
diff --git a/src/core/plugins/sandbox/plugin-worker.integration.spec.ts b/src/core/plugins/sandbox/plugin-worker.integration.spec.tsindex 353dafe4e..39d689934 100644--- a/src/core/plugins/sandbox/plugin-worker.integration.spec.ts+++ b/src/core/plugins/sandbox/plugin-worker.integration.spec.ts@@ -18,6 +18,7 @@ const CTX_LIFECYCLE_FIXTURE = path.resolve(ROOT, 'test/fixtures/sandbox/ctx-lifeconst SEARCH_FIXTURE = path.resolve(ROOT, 'test/fixtures/sandbox/search-plugin.cjs');const UNLOAD_FIXTURE = path.resolve(ROOT, 'test/fixtures/sandbox/unload-plugin.cjs');const UNCLONEABLE_LOG_FIXTURE = path.resolve(ROOT, 'test/fixtures/sandbox/uncloneable-log-plugin.cjs');+const CONFIG_THROW_FIXTURE = path.resolve(ROOT, 'test/fixtures/sandbox/config-throw-plugin.cjs');const flushAsync = (): Promise<void> => new Promise(resolve => setImmediate(resolve));// Run the TS bootstrap inside the worker via ts-node. The base tsconfig is nodenext; we pin the@@ -347,6 +348,38 @@ describe('plugin worker — real worker_threads round-trip (B1)', () => {}});+ it('keeps the worker alive when a synchronous onConfigChange throws, and logs why', async () => {+ const logs: Array<{ level: string; message: string; meta?: Record<string, unknown> }> = [];+ const onExit = jest.fn();+ const host = new PluginWorkerHost(+ makeChannel(),+ undefined,+ undefined,+ undefined,+ (level, message, meta) => logs.push({ level, message, meta }),+ undefined,+ undefined,+ undefined,+ onExit,+ );+ try {+ await host.load(CONFIG_THROW_FIXTURE, { pluginId: 'config-throw', config: { mode: 'ok' } });+ await host.runLifecycle('onEnable');++ host.sendConfigChange({ mode: 'bad' });++ await expect(host.healthCheck(3000)).resolves.toEqual({ healthy: true, message: 'alive' });+ expect(logs).toContainEqual({+ level: 'error',+ message: 'onConfigChange threw',+ meta: { error: 'unsupported mode: bad' },+ });+ expect(onExit).not.toHaveBeenCalled();+ } finally {+ await host.terminate();+ }+ });+it('delivers healthCheck and onConfigChange to a sandboxed plugin (and refreshes ctx.config)', async () => {const host = new PluginWorkerHost(makeChannel());await host.load(CTX_LIFECYCLE_FIXTURE, { pluginId: 'ctx-lc', config: { a: 1 } });diff --git a/src/core/plugins/sandbox/worker-bootstrap.ts b/src/core/plugins/sandbox/worker-bootstrap.tsindex 6b5d9e41d..bfff8b91e 100644--- a/src/core/plugins/sandbox/worker-bootstrap.ts+++ b/src/core/plugins/sandbox/worker-bootstrap.ts@@ -137,10 +137,12 @@ async function handle(message: HostToWorkerMessage): Promise<void> {if (message.kind === 'config-change') {// Refresh the base config so later (non-hook) reads of ctx.config see the new value, then notify// the plugin (fire-and-forget — onConfigChange returns void, and an ack would race the next op).+ // Called inside the chain so a synchronous throw is caught too: escaping here, it would reject the+ // discarded handle() promise and the unhandled rejection would kill the worker.baseConfig = message.config;- void Promise.resolve(plugin?.onConfigChange?.(context, message.config)).catch(error =>- logger.error('onConfigChange threw', error),- );+ void Promise.resolve()+ .then(() => plugin?.onConfigChange?.(context, message.config))+ .catch(error => logger.error('onConfigChange threw', error));return;}diff --git a/test/fixtures/sandbox/config-throw-plugin.cjs b/test/fixtures/sandbox/config-throw-plugin.cjsnew file mode 100644index 000000000..4cc00d470--- /dev/null+++ b/test/fixtures/sandbox/config-throw-plugin.cjs@@ -0,0 +1,11 @@+// Sandbox fixture: a plain (non-async) onConfigChange that throws on a value it refuses, the shape of a+// plugin validating its new config.+module.exports = class ConfigThrowPlugin {+ onConfigChange(_ctx, config) {+ if (config.mode !== 'ok') throw new Error(`unsupported mode: ${config.mode}`);+ }++ healthCheck() {+ return { healthy: true, message: 'alive' };+ }+};