Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(hooks): end an in-flight scope when its call settles

Yudhi Armyndharis9a5a075ca0b5
diff --git a/src/core/hooks/hook-manager-lifetime.spec.ts b/src/core/hooks/hook-manager-lifetime.spec.ts
index 88c04634f..177edf1b7 100644
--- a/src/core/hooks/hook-manager-lifetime.spec.ts
+++ b/src/core/hooks/hook-manager-lifetime.spec.ts
@@ -1,6 +1,7 @@
import { HookManager } from './hook-manager.service';
+import { applySendingGate } from './sending-gate';
-describe('HookManager registration handles', () => {
+describe('HookManager registration handles and in-flight scopes', () => {
afterEach(() => jest.restoreAllMocks());
it('gives registrations made in the same millisecond separate handles', async () => {
@@ -26,4 +27,81 @@ describe('HookManager registration handles', () => {
await manager.execute('message:received', {}, { source: 'test' });
expect(seen).toEqual(['first']);
});
+
+ it('runs every message:sending handler for a send a finished handler left scheduled', async () => {
+ const manager = new HookManager();
+ let release!: () => void;
+ const released = new Promise<void>(resolve => (release = resolve));
+ let later: Promise<unknown> | undefined;
+ const seen: string[] = [];
+ manager.register(
+ 'scheduler',
+ 'message:sending',
+ ctx => {
+ later ??= released.then(() => applySendingGate(manager, 's1', 'text', { text: 'later' }, 'test'));
+ return Promise.resolve({ continue: true, data: ctx.data });
+ },
+ 1,
+ );
+ manager.register(
+ 'veto',
+ 'message:sending',
+ ctx => {
+ const text = (ctx.data as { input: { text: string } }).input.text;
+ seen.push(text);
+ return Promise.resolve({ continue: text !== 'later' });
+ },
+ 2,
+ );
+
+ await applySendingGate(manager, 's1', 'text', { text: 'initial' }, 'test');
+ const outcome = later!.then(
+ () => 'sent',
+ (error: { status?: number }) => error.status,
+ );
+ release();
+
+ expect(await outcome).toBe(400);
+ expect(seen).toEqual(['initial', 'later']);
+ });
+
+ it('ends an in-flight scope when the call settles, however it ends', async () => {
+ const manager = new HookManager();
+ const probes: Array<Promise<boolean>> = [];
+ const probe = (): void => {
+ probes.push(
+ new Promise<void>(resolve => setImmediate(resolve)).then(() => manager.isInFlight('message:sending')),
+ );
+ };
+
+ await manager.runInFlight(['message:sending'], () => {
+ probe();
+ expect(manager.currentInFlight()).toEqual(['message:sending']);
+ return Promise.resolve(7);
+ });
+ await manager
+ .runInFlight(['message:sending'], () => {
+ probe();
+ return Promise.reject(new Error('failed'));
+ })
+ .catch(() => undefined);
+ expect(manager.runInFlight(['message:sending'], () => (probe(), 7))).toBe(7);
+ expect(() =>
+ manager.runInFlight(['message:sending'], () => {
+ probe();
+ throw new Error('failed');
+ }),
+ ).toThrow('failed');
+
+ expect(await Promise.all(probes)).toEqual([false, false, false, false]);
+ expect(manager.currentInFlight()).toEqual([]);
+ });
+
+ it('keeps an enclosing scope in flight for work nested inside it', async () => {
+ const manager = new HookManager();
+ const inner = await manager.runInFlight(['message:received'], () =>
+ manager.runInFlight(['message:sending'], () => Promise.resolve(manager.currentInFlight().sort())),
+ );
+ expect(inner).toEqual(['message:received', 'message:sending']);
+ });
});
diff --git a/src/core/hooks/hook-manager.service.ts b/src/core/hooks/hook-manager.service.ts
index 1ccc716cc..170519fc8 100644
--- a/src/core/hooks/hook-manager.service.ts
+++ b/src/core/hooks/hook-manager.service.ts
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { createLogger } from '../../common/services/logger.service';
import { AsyncLocalStorage } from 'node:async_hooks';
+import { types } from 'node:util';
import { HookEvent, HookHandler, HookContext, HookRegistration } from './hook.interfaces';
/**
@@ -29,7 +30,9 @@ export class HookManager {
// (e.g. a message:sending handler that sends) is short-circuited instead of recursing.
// NOTE: the context does not span the async engine `message_create` echo, so this guards
// synchronous re-entry only (the async message:sent echo loop is documented, deferred).
- private readonly inFlightEvents = new AsyncLocalStorage<Set<HookEvent>>();
+ // Each scope is the event set of one execute()/runInFlight() call. It is emptied when that call settles,
+ // because the store also reaches every timer and detached promise the call's handlers start.
+ private readonly inFlightEvents = new AsyncLocalStorage<ReadonlyArray<Set<HookEvent>>>();
private nextHookId = 0;
/**
@@ -118,17 +121,14 @@ export class HookManager {
* Returns: { continue: boolean, data: T }
*/
async execute<T>(event: HookEvent, data: T, options: HookExecuteOptions<T>): Promise<{ continue: boolean; data: T }> {
- const inFlight = this.inFlightEvents.getStore();
- if (inFlight?.has(event)) {
+ if (this.isInFlight(event)) {
this.logger.warn(
`Hook re-entrancy blocked: ${event} re-fired by a handler of the same event (source: ${options.source})`,
);
return { continue: true, data };
}
- const nextInFlight = new Set<HookEvent>(inFlight);
- nextInFlight.add(event);
- return this.inFlightEvents.run(nextInFlight, () => this.runHandlers(event, data, options));
+ return this.runInFlight([event], () => this.runHandlers(event, data, options));
}
/**
@@ -138,25 +138,45 @@ export class HookManager {
* returns to the host on a fresh async context, where `getStore()` is empty. Wrapping that
* round-trip in this method re-establishes the in-flight set so a capability that re-fires the same
* in-flight event is short-circuited exactly as an in-process handler would be.
+ * The events stay in flight until `fn` settles (a native Promise) or returns; work it leaves running
+ * afterwards is no longer treated as re-entrant.
*/
runInFlight<T>(events: Iterable<HookEvent>, fn: () => T): T {
- const merged = new Set<HookEvent>(this.inFlightEvents.getStore());
- for (const event of events) merged.add(event);
- return this.inFlightEvents.run(merged, fn);
+ const scope = new Set(events);
+ const scopes = [...(this.inFlightEvents.getStore() ?? []).filter(parent => parent.size > 0), scope];
+ return this.inFlightEvents.run(scopes, () => {
+ let result: T;
+ try {
+ result = fn();
+ } catch (error) {
+ scope.clear();
+ throw error;
+ }
+ if (types.isPromise(result)) {
+ void Promise.prototype.then.call(
+ result,
+ () => scope.clear(),
+ () => scope.clear(),
+ );
+ } else {
+ scope.clear();
+ }
+ return result;
+ });
}
/** True if `event` is already in-flight on the active async context (an ancestor handler is running
* it). Lets a caller wrap a capability in {@link runInFlight} ONLY for genuine re-entrancy, instead
* of unconditionally seeding the event and suppressing it for unrelated observers on a top-level call. */
isInFlight(event: HookEvent): boolean {
- return this.inFlightEvents.getStore()?.has(event) ?? false;
+ return this.inFlightEvents.getStore()?.some(scope => scope.has(event)) ?? false;
}
/** Every event in flight on the active async context, for a caller that must carry the chain across a
* boundary AsyncLocalStorage does not span (the sandbox worker IPC) and re-establish it with
* {@link runInFlight} on the way back. */
currentInFlight(): HookEvent[] {
- return [...(this.inFlightEvents.getStore() ?? [])];
+ return [...new Set((this.inFlightEvents.getStore() ?? []).flatMap(scope => [...scope]))];
}
private async runHandlers<T>(