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

BackgroundService not stopping gracefully with an open Blazor Server SignalR circuit on dotnet 9 #59655

Closed
1 task done
previato opened this issue Dec 29, 2024 · 2 comments
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@previato
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The BackgroundService stoppingToken is not triggering instantly when there is a browser session open with a Blazor Server interactive page.

The gracefully shutdown is not happening as expected when doing a Ctrl+C on Console.

The same code work as expected on .net 8

If you navigate to a statically rendered page, the issue does not happens.

// Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddHostedService<Job>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseAntiforgery();

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

app.Run();

// Job.cs
public class Job(ILogger<Job> logger) : BackgroundService
{
    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        return Task.Run(async () =>
        {
            stoppingToken.Register(() =>
            {
                // The code here is not calling immediately when Ctrl+C is pressed.
                logger.LogInformation("Job is stopping");
            });

            while (!stoppingToken.IsCancellationRequested)
            {
                logger.LogInformation("Job is running");
                await Task.Delay(1000);
            }
        }, stoppingToken);
    }
}

Expected Behavior

The stoppingToken of all Hosted Service should trigger immediately.

Steps To Reproduce

  • Create a new .net 9.0 Blazor Web App Project, choose Interactive render mode = Server and Interactivity location = Global
  • Create a background job which inherits from BackgroundService and register it on Program.cs
  • Create a while loop on the background service which should stop doing their job when application is closing
  • Execute the program and leave a page open on Browser which create a SignalR Blazor circuit

Exceptions (if any)

No response

.NET Version

9.0.101

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Dec 29, 2024
@BrennanConroy
Copy link
Member

Should be same root cause as #58947 (comment)
You can work around it by modifying the reconnection logic to wait a little before the first reconnect attempt https://learn.microsoft.com/aspnet/core/blazor/fundamentals/signalr?view=aspnetcore-9.0#adjust-the-server-side-reconnection-retry-count-and-interval

@BrennanConroy BrennanConroy added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Jan 2, 2025
Copy link
Contributor

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants