Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): read a queued pong before failing a sandboxed worker
Yudhi Armyndharis9ce08dee4568
diff --git a/src/core/plugins/sandbox/plugin-worker-host.spec.ts b/src/core/plugins/sandbox/plugin-worker-host.spec.tsindex 00da84a69..12d4dd20d 100644--- a/src/core/plugins/sandbox/plugin-worker-host.spec.ts+++ b/src/core/plugins/sandbox/plugin-worker-host.spec.ts@@ -1,3 +1,4 @@+import { MessageChannel } from 'worker_threads';import { PluginWorkerHost } from './plugin-worker-host';import { PluginWorkerChannel, HostToWorkerMessage, WorkerToHostMessage } from './protocol';import { HookManager } from '../../hooks/hook-manager.service';@@ -917,6 +918,10 @@ describe('PluginWorkerHost', () => {timeoutMs: 500,});+ // The verdict is given one loop turn after the window closes (see armProbe); a fake setImmediate+ // queued while timers are advancing runs on the next timer step.+ const verdict = () => jest.advanceTimersToNextTimer();+beforeEach(() => jest.useFakeTimers());afterEach(() => jest.useRealTimers());@@ -956,6 +961,7 @@ describe('PluginWorkerHost', () => {jest.advanceTimersByTime(500);await pending;jest.advanceTimersByTime(PROBE_MS);+ verdict();expect(onUnresponsive).toHaveBeenCalledTimes(1);const again = hook(host);@@ -982,6 +988,7 @@ describe('PluginWorkerHost', () => {// A full window with no answer at all is still reported.jest.advanceTimersByTime(PROBE_MS);+ verdict();expect(onUnresponsive).toHaveBeenCalledTimes(1);});@@ -995,6 +1002,7 @@ describe('PluginWorkerHost', () => {jest.advanceTimersByTime(PROBE_MS - 100);ch.reply({ kind: 'log', level: 'log', message: 'still spinning' });jest.advanceTimersByTime(100);+ verdict();expect(onUnresponsive).toHaveBeenCalledTimes(1);});@@ -1011,6 +1019,7 @@ describe('PluginWorkerHost', () => {await pending;expect(pings(ch)).toHaveLength(1);jest.advanceTimersByTime(PROBE_MS);+ verdict();expect(onUnresponsive).toHaveBeenCalledTimes(1);}});@@ -1024,6 +1033,50 @@ describe('PluginWorkerHost', () => {expect(pings(ch)).toHaveLength(0);});+ // Real timers and a real MessagePort: the race lives in the event loop's phase order. After a host+ // stall the timers phase runs before the poll phase that delivers the port message, so a pong the+ // worker sent in time was read only after the probe timer had already fired.+ it('reads a pong that was queued while the host was stalled before giving its verdict', async () => {+ jest.useRealTimers();+ const SHORT_PROBE_MS = 50;+ const { port1, port2 } = new MessageChannel();+ const ch = new FakeChannel();+ ch.postMessage = (message: HostToWorkerMessage): void => {+ ch.sent.push(message);+ if (message.kind !== 'ping') return;+ setImmediate(() => {+ port2.postMessage({ kind: 'pong', id: message.id });+ const until = Date.now() + SHORT_PROBE_MS * 2;+ while (Date.now() < until); // the host's own loop is blocked past the probe window+ });+ };+ port1.on('message', (message: WorkerToHostMessage) => ch.reply(message));+ const onUnresponsive = jest.fn();+ const host = new PluginWorkerHost(+ ch,+ undefined,+ undefined,+ undefined,+ undefined,+ undefined,+ undefined,+ undefined,+ undefined,+ undefined,+ SHORT_PROBE_MS,+ onUnresponsive,+ );+ try {+ await host.dispatchHook({ event: 'message:received', data: {}, source: 'Engine', timeoutMs: 10 });+ await new Promise(resolve => setTimeout(resolve, SHORT_PROBE_MS * 4));++ expect(pings(ch)).toHaveLength(1);+ expect(onUnresponsive).not.toHaveBeenCalled();+ } finally {+ port1.close();+ }+ });+it('a worker exit cancels a probe in flight', async () => {const ch = new FakeChannel();const onUnresponsive = jest.fn();diff --git a/src/core/plugins/sandbox/plugin-worker-host.ts b/src/core/plugins/sandbox/plugin-worker-host.tsindex 54d4a0632..9626a9fdc 100644--- a/src/core/plugins/sandbox/plugin-worker-host.ts+++ b/src/core/plugins/sandbox/plugin-worker-host.ts@@ -372,14 +372,22 @@ export class PluginWorkerHost {// (Re)start the probe window. The port is FIFO, so a worker working through a burst of dispatches// reads the ping only after that backlog; every result it sends meanwhile proves its loop is turning// and restarts the window (handleMessage). Only a full window with no answer at all is reported.+ // The verdict waits one loop turn: after a host-side stall the timers phase runs before the poll+ // phase that delivers port messages, so a pong already queued must be read before judging. A pong+ // or a re-arm handled in between replaces this.probe, which the timer comparison detects.private armProbe(id: number): void {if (this.probe) clearTimeout(this.probe.timer);- const timer = setTimeout(() => {- this.probe = undefined;- if (this.dead || this.terminated || this.unresponsiveReported) return;- this.unresponsiveReported = true;- this.onUnresponsive?.();- }, this.livenessTimeoutMs);+ const timer = setTimeout(+ () =>+ setImmediate(() => {+ if (this.probe?.timer !== timer) return;+ this.probe = undefined;+ if (this.dead || this.terminated || this.unresponsiveReported) return;+ this.unresponsiveReported = true;+ this.onUnresponsive?.();+ }),+ this.livenessTimeoutMs,+ );this.probe = { id, timer };}