Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): let start yield to a stop over a stale mark

Yudhi Armyndharis9c6e66edc0d5
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index d883ea57c..5afda0ddd 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -42,6 +42,30 @@ import {
/** A start refused because an operator stopped the session. Same 409 on the wire as any conflict. */
export class SessionStoppedException extends ConflictException {}
+/**
+ * The stop marks. Each add() stamps the id afresh, even one already marked, so start() can tell a mark
+ * set while it waited from one that was already there when it began.
+ */
+export class StopMarks extends Set<string> {
+ private seq = 0;
+ private readonly stamps = new Map<string, number>();
+
+ override add(id: string): this {
+ this.stamps.set(id, ++this.seq);
+ return super.add(id);
+ }
+
+ override delete(id: string): boolean {
+ this.stamps.delete(id);
+ return super.delete(id);
+ }
+
+ /** The stamp of the id's current mark, undefined when it has none. */
+ stamp(id: string): number | undefined {
+ return this.stamps.get(id);
+ }
+}
+
/** Caller callbacks for stop()/delete(), run around the session read that opens each verb. */
export interface StopHooks {
/** stop() only: runs once the session read succeeds and before any teardown; records the stop. */
@@ -85,7 +109,7 @@ export interface SessionEngineControlsHost {
updateStatus(id: string, status: SessionStatus): Promise<void>;
/** Ownership gate, same contract as SessionEngineWiringHost.ownsSession. */
ownsSession(id: string): boolean;
- stoppingSessions: Set<string>;
+ stoppingSessions: StopMarks;
/** The engine each operator-initiated teardown is retiring; see the lifecycle field of the same name. */
operatorTeardowns: Map<string, IWhatsAppEngine>;
reconnectStates: Map<string, ReconnectState>;
@@ -118,7 +142,7 @@ export class SessionEngineControls {
private readonly logger: ReturnType<typeof createLogger>;
private readonly fences: SessionLifecycleFences;
private readonly broadcaster: SessionStatusBroadcaster;
- private readonly stoppingSessions: Set<string>;
+ private readonly stoppingSessions: StopMarks;
private readonly operatorTeardowns: Map<string, IWhatsAppEngine>;
private readonly reconnectStates: Map<string, ReconnectState>;
private readonly stuckAuthRecoveryUsed: Set<string>;
@@ -186,8 +210,9 @@ export class SessionEngineControls {
this.initializingSessions.add(id);
// A stop mark already set when this start began is stale (a start is how one is cleared). One set
// while it waits below comes from a retirement that saw this start in flight, stopOrphanEngines
- // above all, which writes nothing else this start could read and counts on it aborting.
- const markedAtEntry = this.stoppingSessions.has(id);
+ // above all, which writes nothing else this start could read and counts on it aborting. That
+ // includes a mark set again on top of a stale one, which only its stamp tells apart.
+ const markAtEntry = this.stoppingSessions.stamp(id);
try {
const session = await this.requireSession(id);
@@ -233,7 +258,7 @@ export class SessionEngineControls {
// A fresh start intentionally (re-)creates the engine — clear any stale stop/delete mark, but
// yield to one set after this start began, leaving it in place.
- if (this.stoppingSessions.has(id) && !markedAtEntry) {
+ if (this.stoppingSessions.has(id) && this.stoppingSessions.stamp(id) !== markAtEntry) {
throw new SessionStoppedException(`Session ${id} was stopped`);
}
this.stoppingSessions.delete(id);
diff --git a/src/modules/session/session-engine-lifecycle-races.spec.ts b/src/modules/session/session-engine-lifecycle-races.spec.ts
index 74002e950..f1b5ae8e4 100644
--- a/src/modules/session/session-engine-lifecycle-races.spec.ts
+++ b/src/modules/session/session-engine-lifecycle-races.spec.ts
@@ -193,6 +193,47 @@ describe('SessionEngineLifecycle races', () => {
expect(internals.engines.has(ID)).toBe(true);
});
+
+ it('retires the start when an older stop mark was already present', async () => {
+ await lifecycle.stopOrphanEngines([ID]);
+ const read = deferred<Session>();
+ repository.findOne.mockReturnValueOnce(read.promise);
+
+ const outcome = lifecycle.start(ID).then(
+ () => undefined,
+ (e: unknown) => e,
+ );
+ await flush();
+ await expect(lifecycle.stopOrphanEngines([ID])).resolves.toEqual({
+ stopped: [],
+ notRunning: [ID],
+ failed: [],
+ });
+ read.resolve(session());
+
+ expect(await outcome).toBeInstanceOf(SessionStoppedException);
+ expect(engineFactory.create).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('stop against an explicit start still reading its row', () => {
+ it('yields to a stop that lands while it waits, after an earlier stop', async () => {
+ repository.findOne.mockResolvedValue(session({ desiredState: 'stopped' }));
+ await lifecycle.stop(ID);
+ const read = deferred<Session>();
+ repository.findOne.mockReturnValueOnce(read.promise);
+
+ const outcome = lifecycle.start(ID, { explicit: true }).then(
+ () => undefined,
+ (e: unknown) => e,
+ );
+ await flush();
+ await lifecycle.stop(ID);
+ read.resolve(session({ desiredState: 'stopped' }));
+
+ expect(await outcome).toBeInstanceOf(SessionStoppedException);
+ expect(internals.engines.has(ID)).toBe(false);
+ });
});
describe('rejectRebind', () => {
diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.ts
index f3e1484cd..2d28db742 100644
--- a/src/modules/session/session-engine-lifecycle.service.ts
+++ b/src/modules/session/session-engine-lifecycle.service.ts
@@ -34,7 +34,7 @@ import {
SessionEngineEventWiring,
SessionEngineWiringHost,
} from './session-engine-event-wiring';
-import { SessionEngineControls, type StopHooks } from './session-engine-controls';
+import { SessionEngineControls, StopMarks, type StopHooks } from './session-engine-controls';
import { SessionOwnershipService, nodeOwnsSession } from './session-ownership.service';
/**
@@ -240,7 +240,7 @@ export class SessionEngineLifecycle {
// engine init, so a stop/delete during that window could re-register an engine AFTER
// teardown (orphan). stop()/delete() add the id here; executeReconnect checks it after its
// awaits and destroys any engine it just created; start() clears it (intentional restart).
- private stoppingSessions: Set<string> = new Set();
+ private stoppingSessions = new StopMarks();
// Sessions whose engine is mid-initialization (a start() is in flight). Reserved synchronously
// in start() so a near-simultaneous second start() can't pass the engines.has() check during the