Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[service-bus] batching receiver was removing event listeners too early #12908

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions sdk/servicebus/service-bus/src/core/batchingReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,10 @@ export class BatchingReceiverLite {
let totalWaitTimer: NodeJS.Timer | undefined;

// eslint-disable-next-line prefer-const
let cleanupBeforeResolveOrReject: (
shouldRemoveDrain: "removeDrainHandler" | "leaveDrainHandler"
) => void;
let cleanupBeforeResolveOrReject: () => void;

const onError: OnAmqpEvent = (context: EventContext) => {
cleanupBeforeResolveOrReject("removeDrainHandler");
cleanupBeforeResolveOrReject();

const eventType = context.session?.error != null ? "session_error" : "receiver_error";
let error = context.session?.error || context.receiver?.error;
Expand All @@ -355,7 +353,7 @@ export class BatchingReceiverLite {
};

this._closeHandler = (error?: AmqpError | Error): void => {
cleanupBeforeResolveOrReject("removeDrainHandler");
cleanupBeforeResolveOrReject();

if (
// no error, just closing. Go ahead and return what we have.
Expand All @@ -379,8 +377,6 @@ export class BatchingReceiverLite {
// - maxWaitTime is passed or
// - newMessageWaitTimeoutInSeconds is passed since the last message was received
const finalAction = (): void => {
cleanupBeforeResolveOrReject("leaveDrainHandler");

// Drain any pending credits.
if (receiver.isOpen() && receiver.credit > 0) {
logger.verbose(`${loggingPrefix} Draining leftover credits(${receiver.credit}).`);
Expand All @@ -389,7 +385,7 @@ export class BatchingReceiverLite {
receiver.drain = true;
receiver.addCredit(1);
Comment on lines 385 to 386
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not you..

Maybe just add a comment saying this triggers the drain which would also do the cleanup of removing the listeners.

} else {
receiver.removeListener(ReceiverEvents.receiverDrained, onReceiveDrain);
cleanupBeforeResolveOrReject();

logger.verbose(
`${loggingPrefix} Resolving receiveMessages() with ${brokeredMessages.length} messages.`
Expand Down Expand Up @@ -464,24 +460,20 @@ export class BatchingReceiverLite {
// put into the task queue the same way.
// So this call, while odd, just ensures that we resolve _after_ any already-queued onMessage handlers that may
// be waiting in the task queue.
setTimeout(() => resolve(brokeredMessages));
setTimeout(() => {
cleanupBeforeResolveOrReject();
resolve(brokeredMessages);
});
};

cleanupBeforeResolveOrReject = (
shouldRemoveDrain:
| "removeDrainHandler" // remove drain handler (not waiting or initiating a drain)
| "leaveDrainHandler" // listener for drain is removed when it is determined we dont need to drain or when drain is completed
): void => {
cleanupBeforeResolveOrReject = (): void => {
if (receiver != null) {
receiver.removeListener(ReceiverEvents.receiverError, onError);
receiver.removeListener(ReceiverEvents.message, onReceiveMessage);
receiver.session.removeListener(SessionEvents.sessionError, onError);
receiver.removeListener(ReceiverEvents.receiverClose, onClose);
receiver.session.removeListener(SessionEvents.sessionClose, onClose);

if (shouldRemoveDrain === "removeDrainHandler") {
receiver.removeListener(ReceiverEvents.receiverDrained, onReceiveDrain);
}
receiver.removeListener(ReceiverEvents.receiverDrained, onReceiveDrain);
}

if (totalWaitTimer) {
Expand All @@ -495,7 +487,7 @@ export class BatchingReceiverLite {
};

abortSignalCleanupFunction = checkAndRegisterWithAbortSignal((err) => {
cleanupBeforeResolveOrReject("removeDrainHandler");
cleanupBeforeResolveOrReject();
richardpark-msft marked this conversation as resolved.
Show resolved Hide resolved
reject(err);
}, args.abortSignal);

Expand Down