Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): keep the sandbox worker alive on an uncloneable log meta
Yudhi Armyndharisdf9fb68e16a5
diff --git a/src/core/plugins/sandbox/plugin-worker.integration.spec.ts b/src/core/plugins/sandbox/plugin-worker.integration.spec.tsindex 8eff9e4e1..3d0a81c2b 100644--- a/src/core/plugins/sandbox/plugin-worker.integration.spec.ts+++ b/src/core/plugins/sandbox/plugin-worker.integration.spec.ts@@ -17,6 +17,7 @@ const HOOK_ERROR_FIXTURE = path.resolve(ROOT, 'test/fixtures/sandbox/hook-error-const CTX_LIFECYCLE_FIXTURE = path.resolve(ROOT, 'test/fixtures/sandbox/ctx-lifecycle-plugin.cjs');const 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 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@@ -326,6 +327,21 @@ describe('plugin worker — real worker_threads round-trip (B1)', () => {await host.terminate();});+ it('keeps the worker alive when a timer logs a meta the structured clone cannot copy', async () => {+ const logs: Array<{ level: string; message: string; meta?: Record<string, unknown> }> = [];+ const host = new PluginWorkerHost(makeChannel(), undefined, undefined, undefined, (level, message, meta) =>+ logs.push({ level, message, meta }),+ );++ await host.load(UNCLONEABLE_LOG_FIXTURE, { pluginId: 'uncloneable-log', config: {} });+ await host.runLifecycle('onEnable');++ // A DataCloneError thrown from the timer used to be uncaught in the worker, so the worker exited.+ await expect(host.healthCheck(3000)).resolves.toEqual({ healthy: true, message: 'alive' });+ expect(logs).toContainEqual({ level: 'warn', message: 'timer log', meta: undefined });+ 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 a9e3a8300..543a5fc02 100644--- a/src/core/plugins/sandbox/worker-bootstrap.ts+++ b/src/core/plugins/sandbox/worker-bootstrap.ts@@ -1,5 +1,5 @@import { parentPort } from 'worker_threads';-import { HostToWorkerMessage, WorkerToHostMessage } from './protocol';+import { HostToWorkerMessage, PluginLogLevel, WorkerToHostMessage } from './protocol';import { WorkerCapabilityClient, buildSandboxContext } from './worker-capability';import { WorkerHookRegistry, WorkerHookHandler, hookConfigStore } from './worker-hooks';import { WebhookRegistry, WebhookHandler } from './worker-webhooks';@@ -42,18 +42,23 @@ const hookRegistry = new WorkerHookRegistry(send);const webhookRegistry = new WebhookRegistry(send);const searchRegistry = new WorkerSearchRegistry(send);+// A meta the structured clone cannot copy (a function, a fetch Response) makes postMessage throw. From+// a timer that throw is uncaught and kills the worker, so the line goes out again without its meta.+const sendLog = (level: PluginLogLevel, message: string, meta?: Record<string, unknown>): void => {+ try {+ send({ kind: 'log', level, message, meta });+ } catch {+ send({ kind: 'log', level, message: String(message) });+ }+};+// ctx.logger proxy: forwards to the host's per-plugin logger (the same one in-process plugins use).const logger = {- log: (message: string, meta?: Record<string, unknown>) => send({ kind: 'log', level: 'log', message, meta }),- debug: (message: string, meta?: Record<string, unknown>) => send({ kind: 'log', level: 'debug', message, meta }),- warn: (message: string, meta?: Record<string, unknown>) => send({ kind: 'log', level: 'warn', message, meta }),+ log: (message: string, meta?: Record<string, unknown>) => sendLog('log', message, meta),+ debug: (message: string, meta?: Record<string, unknown>) => sendLog('debug', message, meta),+ warn: (message: string, meta?: Record<string, unknown>) => sendLog('warn', message, meta),error: (message: string, error?: unknown, meta?: Record<string, unknown>) =>- send({- kind: 'log',- level: 'error',- message,- meta: error !== undefined ? { ...meta, error: errorMessage(error) } : meta,- }),+ sendLog('error', message, error !== undefined ? { ...meta, error: errorMessage(error) } : meta),};let plugin: LifecyclePlugin | null = null;let context: Record<string, unknown> | null = null;diff --git a/test/fixtures/sandbox/uncloneable-log-plugin.cjs b/test/fixtures/sandbox/uncloneable-log-plugin.cjsnew file mode 100644index 000000000..5ac0af5e5--- /dev/null+++ b/test/fixtures/sandbox/uncloneable-log-plugin.cjs@@ -0,0 +1,17 @@+// Sandbox fixture: logs from a timer with a meta the structured clone cannot copy (a function), the+// shape of a plugin passing a fetch Response as meta. onEnable settles only after the timer has run,+// so a throw from that log call is uncaught in the worker before the lifecycle answers.+module.exports = class UncloneableLogPlugin {+ async onEnable(ctx) {+ await new Promise(resolve =>+ setTimeout(() => {+ ctx.logger.warn('timer log', { body: () => 'not cloneable' });+ resolve();+ }, 0),+ );+ }++ healthCheck() {+ return { healthy: true, message: 'alive' };+ }+};