diff --git a/sdk/servicebus/service-bus/CHANGELOG.md b/sdk/servicebus/service-bus/CHANGELOG.md index d2dcee4ce41c..e262f242fa3b 100644 --- a/sdk/servicebus/service-bus/CHANGELOG.md +++ b/sdk/servicebus/service-bus/CHANGELOG.md @@ -8,6 +8,8 @@ ### Bugs Fixed +- Fix an issue where we don't respect user request to close the receiver if the connection is disconnected when using the `subscribe()` method. [PR #20427](https://github.com/Azure/azure-sdk-for-js/pull/20427) + ### Other Changes ## 7.5.0 (2022-02-14) diff --git a/sdk/servicebus/service-bus/src/core/streamingReceiver.ts b/sdk/servicebus/service-bus/src/core/streamingReceiver.ts index 520a91bd88d1..7b75a6bdc411 100644 --- a/sdk/servicebus/service-bus/src/core/streamingReceiver.ts +++ b/sdk/servicebus/service-bus/src/core/streamingReceiver.ts @@ -576,17 +576,23 @@ export class StreamingReceiver extends MessageReceiver { * @param catchAndReportError - A function and reports an error but does not throw it. */ private async _initAndAddCreditOperation(caller: "detach" | "subscribe"): Promise { - throwErrorIfConnectionClosed(this._context); - - await this._messageHandlers().preInitialize(); - if (this._receiverHelper.isSuspended()) { // user has suspended us while we were initializing // the connection. Abort this attempt - if they attempt // resubscribe we'll just reinitialize. + // This checks should happen before throwErrorIfConnectionClosed(); otherwise + // we won't be able to break out of the retry-for-ever loops when user suspend us. throw new AbortError("Receiver was suspended during initialization."); } + throwErrorIfConnectionClosed(this._context); + + await this._messageHandlers().preInitialize(); + + if (this._receiverHelper.isSuspended()) { + // Need to check again as user can suspend us in preInitialize() + throw new AbortError("Receiver was suspended during initialization."); + } await this._init( this._createReceiverOptions(caller === "detach", this._getHandlers()), this._subscribeOptions?.abortSignal diff --git a/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts b/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts index 7514a6e41f93..c98c75ec9bb4 100644 --- a/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts +++ b/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts @@ -321,10 +321,15 @@ export async function retryForever( if (!config.retryOptions) { config.retryOptions = {}; } - if (!config.retryOptions.retryDelayInMs || config.retryOptions.retryDelayInMs < 0) { + // eslint-disable-next-line eqeqeq + if (config.retryOptions.retryDelayInMs == undefined || config.retryOptions.retryDelayInMs < 0) { config.retryOptions.retryDelayInMs = Constants.defaultDelayBetweenOperationRetriesInMs; } - if (!config.retryOptions.maxRetryDelayInMs || config.retryOptions.maxRetryDelayInMs < 0) { + if ( + // eslint-disable-next-line eqeqeq + config.retryOptions.maxRetryDelayInMs == undefined || + config.retryOptions.maxRetryDelayInMs < 0 + ) { config.retryOptions.maxRetryDelayInMs = Constants.defaultMaxDelayForExponentialRetryInMs; } if (!config.retryOptions.mode) {