CAP icon indicating copy to clipboard operation
CAP copied to clipboard

BLAZOR Application just hangs in 8.3.5 and 8.3.4 when awaiting commit transaction

Open Dave-Townsend opened this issue 8 months ago • 9 comments

In 8.3.5 got an Azure sql transaction, then call in CAP to azure service bus all looks good and save asynchronous works but as soon as hit async commit trans in EF Core the application freezes never gets past the commit - no error - but it seems to have done the commit trans its a blazor app been working for over a year. When I downgrade to 8.3.2 all is working so something to do with latest release or the release prior. I send email to author with some code, happy to paste code below as well if needed. Everything is a sync if that helps and also I start transaction, add some rows to sql database, add CAP to azure service bus, finally save but commit will cause the deadlock or hang to occur. It’s recreatable every time.

Dave-Townsend avatar Jun 03 '25 21:06 Dave-Townsend

This may be related to https://github.com/dotnetcore/CAP/pull/1629. Can you provide an example of replication?

@PoteRii Can you help to check this issue?

yang-xiaodong avatar Jun 04 '25 13:06 yang-xiaodong

We have no problems with 8.3.3. I can help with provided example

PoteRii avatar Jun 04 '25 14:06 PoteRii

Hi

This is basically the code.

  • The red arrow on the commit is where it will hang and never come out or enter the catch block.
  • There seems to be no error at all – just hangs is the behaviour
  • If I go back a couple of versions, then all is fine and working (or obviously, if I comment out the CAP Azure Service Bus it save fine to the DB – without CAP)
  • It’s a Blazor server app if that is relevant or not I do not know
  • CAP libraries I use that cause this are:

@.***

@.***

Kinds regards

David Townsend Azure • IaC • DevSecOps Consultant Microsoft Certified: Azure Database Administrator Associate Scrum.org Certified: PSM I • PSD 💬 Teams: @.> • Video @.&withVideo=true> • Audio @.> 📞 Phone: +44 207 845 2440 (Office)teL:02078452440 • +44 7999 654321 (Mobile)tel:07999654321 📧 Email: @.@.***>

From: Giorgi Khvedeliani @.> Sent: 04 June 2025 15:02 To: dotnetcore/CAP @.> Cc: David Townsend (External) @.>; Author @.> Subject: Re: [dotnetcore/CAP] Application just hangs in 8.3.5 when awaiting commit transaction (Issue #1697)

You don't often get email from @.@.>. Learn why this is importanthttps://aka.ms/LearnAboutSenderIdentification

We have no problems with 8.3.3. I can help with provided example

— Reply to this email directly, view it on GitHubhttps://github.com/dotnetcore/CAP/issues/1697#issuecomment-2940164229, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A2CXIAO7X7JW5CGHXSY7GFT3B337LAVCNFSM6AAAAAB6Q3TIFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNBQGE3DIMRSHE. You are receiving this because you authored the thread.Message ID: @.@.>>


Confidentiality Notice: The information in this document and attachments is confidential and may also be legally privileged. It is intended only for the use of the named recipient. Internet communications are not secure and therefore British American Tobacco does not accept legal responsibility for the contents of this message. If you are not the intended recipient, please notify us immediately and then delete this document. Do not disclose the contents of this document to any other person, nor take any copies. Violation of this notice may be unlawful. At BAT we are committed to handling your personal data responsibly and with integrity. For more information on how we process your personal data, please see our Privacy Notice herehttps://www.bat.com/group/sites/uk__9d9kcy.nsf/vwPagesWebLive/DOBB5H9Z.

Dave-Townsend avatar Jun 04 '25 15:06 Dave-Townsend

Where is the code exactly?

PoteRii avatar Jun 04 '25 16:06 PoteRii

maybe adding ConfigureAwait(false); to https://github.com/dotnetcore/CAP/pull/1629/files changes would help? Other than that i do not see any cause

PoteRii avatar Jun 05 '25 09:06 PoteRii

Sorry I removed wrong comment. Here is the code.

  1. Create new blazor app .Net 9 (server app) with samples
  2. If you increment the counter on the counter page that will cause it to hang and never come back from commit
  3. It seems blazor specific - if I do this in console app it will work

Nugets

    <PackageReference Include="DotNetCore.CAP" Version="8.3.5" />
    <PackageReference Include="DotNetCore.CAP.AzureServiceBus" Version="8.3.5" />
    <PackageReference Include="DotNetCore.CAP.SqlServer" Version="8.3.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.5" />

Program.cs

using BlazorApp2;
using BlazorApp2.Components;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();


builder.Services.AddDbContextFactory<AppDbContext>(options =>
    options.UseSqlServer("Server=localhost;Database=Test;Trusted_Connection=True;TrustServerCertificate=True;"));

builder.Services.AddCap(x =>
{
    x.UseEntityFramework<AppDbContext>();
    x.UseAzureServiceBus(opt =>
    {
        opt.ConnectionString = "Endpoint=sb://****-01.servicebus.windows.net/;SharedAccessKeyName=***l;SharedAccessKey=****";
        opt.TopicPath = "*******";
    });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();


app.UseAntiforgery();

app.MapStaticAssets();
app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

app.Run();

AppDbContext.cs

using Microsoft.EntityFrameworkCore;

namespace BlazorApp2;


public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{

    public DbSet<Employee> Employee => Set<Employee>();
}

public class Employee
{
    public int Id { get; set; }
    public int EmployeeId { get; set; }
}

AsbEmail.cs

namespace BlazorApp2;

public record AsbEmail
{
    public string To { get; set; } = "";
    public string From { get; set; } = "[email protected]";
    public string Subject { get; set; } = "";
    public string Body { get; set; } = "";
}

Counter.razor

@page "/counter"
@using DotNetCore.CAP
@using Microsoft.EntityFrameworkCore
@rendermode InteractiveServer
@inject ICapPublisher CapPublisher
@inject IDbContextFactory<AppDbContext> DbFactory

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private async Task IncrementCount()
    {
        currentCount++;

        await using var db = await DbFactory.CreateDbContextAsync();
        await using var trans = await db.Database.BeginTransactionAsync(CapPublisher);

        var asbEmail = new AsbEmail
        {
            To = "[email protected]",
            From = "[email protected]",
            Subject = "Bug test",
            Body = "Hello World"
        };
        await CapPublisher.PublishAsync("email", asbEmail);
        await trans.CommitAsync();  // IT WILL HANG DURING THIS 
        int t = 0;   // NEVER GETS TO HERE 
    }
}

Dave-Townsend avatar Jun 05 '25 09:06 Dave-Townsend

Hello, I couldn't reproduce the issue you're experiencing. I've recorded a video for you to review. I'm using an ASP.NET Core API project, and this issue is likely unrelated to Blazor.

https://github.com/user-attachments/assets/9f88db49-b6ec-4f8c-a987-a60c89d37570

yang-xiaodong avatar Jun 05 '25 14:06 yang-xiaodong

Hi there I would usually agree with you - Blazor should have nothing to do with it - however if I do the same in Console app it will work fine. I enclose a video also the code that I isolated off to recreate the issue in most simple way.

BlazorApp2.zip https://github.com/user-attachments/assets/dd5b4264-2dd4-4f34-ac02-a69ceb886605

And here is a downgrade to 8.3.1 (where it all will work) it should be noted in this example I had to downgrade to 8.3.1 while in production I could downgrade to 8.3.3 but that not fix the issue in this isolated example code. It should also be noted that the COMMIT must work because I do see the entry in the cap.published and I do get the email so it seems to hang in the background process is what I would conclude and never return back to Blazor somehow locking the UI thread,

https://github.com/user-attachments/assets/3b22e867-fb32-49a7-b66f-39b9ad124358

Dave-Townsend avatar Jun 05 '25 15:06 Dave-Townsend

Thanks for your example. As @PoteRii said, The FlushAsync is missing ConfigureAwait(false), because Blazor's call is in the UI thread.

yang-xiaodong avatar Jun 06 '25 07:06 yang-xiaodong

Hi @yang-xiaodong , has the fix been merged with a release version? This issue is still open but it is marked as fixed as well.

bangonkali avatar Jul 09 '25 23:07 bangonkali

Fixed in 8.4.0-preview-270476069

yang-xiaodong avatar Jul 28 '25 12:07 yang-xiaodong

Hi

The issue is present in 8.3.5 (and was not fixed in 8.3.4), but it should be fixed when 8.3.6 comes out. I assume it’s a typo you mean fixed in 8.3.6 preview?

Kidn regards

David

David Townsend Azure • IaC • DevSecOps Consultant Microsoft Certified: Azure Database Administrator Associate Scrum.org Certified: PSM I • PSD 💬 Teams: @.> • Video @.&withVideo=true> • Audio @.> 📞 Phone: +44 207 845 2440 (Office)teL:02078452440 • +44 7999 654321 (Mobile)tel:07999654321 📧 Email: @.@.***>

From: Xiaodong Yang @.> Sent: 28 July 2025 13:22 To: dotnetcore/CAP @.> Cc: David Townsend (External) @.>; Author @.> Subject: Re: [dotnetcore/CAP] BLAZOR Application just hangs in 8.3.5 and 8.3.4 when awaiting commit transaction (Issue #1697)

Fixed in 8.4.0-preview-270476069

— Reply to this email directly, view it on GitHubhttps://github.com/dotnetcore/CAP/issues/1697#issuecomment-3126981734, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A2CXIANULTQDYDFZFRABTST3KYIV7AVCNFSM6AAAAAB6Q3TIFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTCMRWHE4DCNZTGQ. You are receiving this because you authored the thread.Message ID: @.@.>>


Confidentiality Notice: The information in this document and attachments is confidential and may also be legally privileged. It is intended only for the use of the named recipient. Internet communications are not secure and therefore British American Tobacco does not accept legal responsibility for the contents of this message. If you are not the intended recipient, please notify us immediately and then delete this document. Do not disclose the contents of this document to any other person, nor take any copies. Violation of this notice may be unlawful. At BAT we are committed to handling your personal data responsibly and with integrity. For more information on how we process your personal data, please see our Privacy Notice herehttps://www.bat.com/group/sites/uk__9d9kcy.nsf/vwPagesWebLive/DOBB5H9Z.

Dave-Townsend avatar Jul 28 '25 12:07 Dave-Townsend

No version 8.3.6

yang-xiaodong avatar Jul 28 '25 12:07 yang-xiaodong