Skip to content

Commit dd26ecc

Browse files
committed
Don't deadlock when an onEventsPosted handler processes the queue
The handler runs inside the processing run, so waiting for that run from the handler never finished.
1 parent d265071 commit dd26ecc

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎packages/core/src/queue/DefaultEventQueue.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class DefaultEventQueue implements IEventQueue {
4646
* @private
4747
*/
4848
private _processing: Promise<void> | undefined;
49+
private _notifyingHandlers = false;
4950

5051
/**
5152
* Processes the queue every xx seconds.
@@ -95,8 +96,9 @@ export class DefaultEventQueue implements IEventQueue {
9596
const { log } = this.config.services;
9697

9798
if (this._processing) {
98-
// Callers such as flush() need to wait for the run that is already sending events.
99-
return this._processing;
99+
// Callers such as flush() need to wait for the run that is already sending events. An onEventsPosted
100+
// handler calling back into the queue is part of that run, so it must not wait for it.
101+
return this._notifyingHandlers ? Promise.resolve() : this._processing;
100102
}
101103

102104
log.trace("Processing queue...");
@@ -144,7 +146,12 @@ export class DefaultEventQueue implements IEventQueue {
144146
const events = items.map((i) => i.event);
145147
const response = await this.config.services.submissionClient.submitEvents(events);
146148
const result = await this.processSubmissionResponse(response, items, batchSize);
147-
await this.eventsPosted(events, response);
149+
this._notifyingHandlers = true;
150+
try {
151+
await this.eventsPosted(events, response);
152+
} finally {
153+
this._notifyingHandlers = false;
154+
}
148155

149156
if (result.retryWithBatchSize) {
150157
batchSize = result.retryWithBatchSize;

‎packages/core/test/queue/DefaultEventQueue.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,15 @@ describe("DefaultEventQueue submission", () => {
414414
expect(secondFinished).toBe(true);
415415
expect(await storedQueueKeys()).toHaveLength(0);
416416
});
417+
418+
test("an onEventsPosted handler that processes the queue does not wait for its own run", async () => {
419+
config.services.queue.onEventsPosted(() => config.services.queue.process());
420+
await config.services.queue.enqueue(createEvent("event-0001"));
421+
422+
await config.services.queue.process();
423+
await config.services.queue.enqueue(createEvent("event-0002"));
424+
await config.services.queue.process();
425+
426+
expect(client.submittedReferenceIds).toEqual(["event-0001", "event-0002"]);
427+
});
417428
});

0 commit comments

Comments
 (0)