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

Azure Service Bus Messages Marked as Completed Even When Persistence Fails #1583

Open
oussama-smida opened this issue Sep 17, 2024 · 1 comment

Comments

@oussama-smida
Copy link
Contributor

Description:
In CAP 8.2, when using Azure Service Bus and SQL Server, messages are marked as completed even if the system fails to persist the received message to the database.
This issue has been observed as a rare occurrence but poses a considerable risk to the reliability of the system, as it can lead to message loss.

Analysis:
The issue occurs because the default setting for AutoCompleteMessages in CAP is true. When an exception occurs during message persistence (e.g., SQL timeout), CAP attempts to call the Reject method. However, the current implementation of Reject for Azure Service Bus is a no-op, resulting in the message being automatically completed despite the failure.

Trace Log:

An exception occurred when processing the received message. Message:'{0}'.
Source context: DotNetCore.CAP.Internal.ConsumerRegister

Exception:

Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
 ---> System.ComponentModel.Win32Exception (258): Unknown error 258
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
...

Sql Command:

INSERT INTO cap.Received([Id],[Version],[Name],[Group],[Content],[Retries],[Added],[ExpiresAt],[StatusName]) VALUES(@Id,'v1',@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);

Relevant source code
DotNetCore.CAP.Internal.ConsumerRegister

try
{
...
}
catch (Exception e)
{
    _logger.LogError(e, "An exception occurred when process received message. Message:'{0}'.",
        transportMessage);

    client.Reject(sender); // NO-OP in the Azure Service Bus IConsumerClient implementation

    TracingError(tracingTimestamp, transportMessage, client.BrokerAddress, e);
}

Proposed Fix:
DotNetCore.CAP.AzureServiceBus.AzureServiceBusConsumerClient

public void Reject(object? sender)
{
    var commitInput = (AzureServiceBusConsumerCommitInput)sender!;
    commitInput.AbandonMessageAsync().GetAwaiter().GetResult();
}
oussama-smida pushed a commit to oussama-smida/CAP that referenced this issue Sep 17, 2024
…essage when persistence fails to avoid message loss
yang-xiaodong pushed a commit that referenced this issue Sep 18, 2024
…n persistence fails to avoid message loss (#1584)

Co-authored-by: FC\oussama.smida <[email protected]>
@yang-xiaodong
Copy link
Member

Fixed in version 8.3.0-preview-243613753

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants