-
Notifications
You must be signed in to change notification settings - Fork 679
Stop the Blazor admin UI blocking a graceful shutdown #5693
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
Open
timothycoleman
wants to merge
2
commits into
master
Choose a base branch
from
timothycoleman/blazor-shutdown-circuit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/KurrentDB.Components.Tests/BlazorShutdownMiddlewareTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright (c) Kurrent, Inc and/or licensed to Kurrent, Inc under one or more agreements. | ||
| // Kurrent, Inc licenses this file to you under the Kurrent License v1 (see LICENSE.md). | ||
|
|
||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Http.Features; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Xunit; | ||
|
|
||
| namespace KurrentDB.Components.Tests; | ||
|
|
||
| // The middleware keeps a shutting-down server from handing out a new circuit, which would otherwise keep | ||
| // Kestrel draining until HostOptions.ShutdownTimeout expires. See BlazorShutdownMiddleware for the mechanism | ||
| // and https://github.com/dotnet/aspnetcore/issues/58947 for the upstream issue it works around. | ||
| public class BlazorShutdownMiddlewareTests { | ||
| class FakeLifetime : IHostApplicationLifetime, IDisposable { | ||
| readonly CancellationTokenSource _stopping = new(); | ||
|
|
||
| public CancellationToken ApplicationStarted => CancellationToken.None; | ||
| public CancellationToken ApplicationStopping => _stopping.Token; | ||
| public CancellationToken ApplicationStopped => CancellationToken.None; | ||
| public void StopApplication() => _stopping.Cancel(); | ||
| public void Dispose() => _stopping.Dispose(); | ||
| } | ||
|
|
||
| // DefaultHttpContext's own lifetime feature ignores Abort(), so stand in for the one Kestrel provides. | ||
| class RecordingRequestLifetime : IHttpRequestLifetimeFeature, IDisposable { | ||
| readonly CancellationTokenSource _aborted = new(); | ||
|
|
||
| public bool Aborted { get; private set; } | ||
| public CancellationToken RequestAborted { get => _aborted.Token; set { } } | ||
|
|
||
| public void Abort() { | ||
| Aborted = true; | ||
| _aborted.Cancel(); | ||
| } | ||
|
|
||
| public void Dispose() => _aborted.Dispose(); | ||
| } | ||
|
|
||
| static DefaultHttpContext Request(string path) { | ||
| var context = new DefaultHttpContext(); | ||
| context.Request.Path = path; | ||
| return context; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task passes_other_requests_through() { | ||
| using var lifetime = new FakeLifetime(); | ||
| lifetime.StopApplication(); | ||
| var called = false; | ||
| var sut = new BlazorShutdownMiddleware(_ => { | ||
| called = true; | ||
| return Task.CompletedTask; | ||
| }, lifetime); | ||
| var context = Request("/ui/cluster"); | ||
|
|
||
| await sut.Invoke(context); | ||
|
|
||
| Assert.True(called); | ||
| Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode); | ||
| } | ||
|
|
||
| [Theory] | ||
| // refusing the negotiation stops a new circuit being created while the server drains; refusing the | ||
| // transport covers a client that negotiated just before the shutdown began | ||
| [InlineData("/_blazor/negotiate")] | ||
| [InlineData("/_blazor")] | ||
| public async Task rejects_circuit_requests_while_stopping(string path) { | ||
| using var lifetime = new FakeLifetime(); | ||
| lifetime.StopApplication(); | ||
| var called = false; | ||
| var sut = new BlazorShutdownMiddleware(_ => { | ||
| called = true; | ||
| return Task.CompletedTask; | ||
| }, lifetime); | ||
| var context = Request(path); | ||
|
|
||
| await sut.Invoke(context); | ||
|
|
||
| Assert.False(called); | ||
| Assert.Equal(StatusCodes.Status503ServiceUnavailable, context.Response.StatusCode); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task aborts_a_circuit_that_started_before_the_shutdown() { | ||
| using var lifetime = new FakeLifetime(); | ||
| using var requestLifetime = new RecordingRequestLifetime(); | ||
| var context = Request("/_blazor"); | ||
| context.Features.Set<IHttpRequestLifetimeFeature>(requestLifetime); | ||
| var inFlight = new TaskCompletionSource(); | ||
| var sut = new BlazorShutdownMiddleware(circuit => { | ||
| // a circuit request runs for as long as the browser tab lives, so it is still in flight when the | ||
| // server begins stopping. Nothing else ends it, so without the abort the drain would wait for it | ||
| // until the shutdown timeout expires. | ||
| circuit.RequestAborted.Register(inFlight.SetResult); | ||
| return inFlight.Task; | ||
| }, lifetime); | ||
|
|
||
| var pending = sut.Invoke(context); | ||
| Assert.False(pending.IsCompleted); | ||
|
|
||
| lifetime.StopApplication(); | ||
|
|
||
| await pending.WaitAsync(TimeSpan.FromSeconds(5)); | ||
| Assert.True(requestLifetime.Aborted); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright (c) Kurrent, Inc and/or licensed to Kurrent, Inc under one or more agreements. | ||
| // Kurrent, Inc licenses this file to you under the Kurrent License v1 (see LICENSE.md). | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.Hosting; | ||
| using ILogger = Serilog.ILogger; | ||
|
|
||
| namespace KurrentDB; | ||
|
|
||
| // Stops the embedded UI from re-establishing its circuit while the server is shutting down. | ||
| // | ||
| // This is a workaround for an open ASP.NET Core issue — remove it once SignalR stops accepting connections | ||
| // itself: https://github.com/dotnet/aspnetcore/issues/58947 | ||
| // | ||
| // Without it the circuit is re-established and not closed by the shutdown procedure, which times out and | ||
| // transitions to a forced shutdown. | ||
| public sealed class BlazorShutdownMiddleware(RequestDelegate next, IHostApplicationLifetime lifetime) { | ||
| static readonly ILogger Log = Serilog.Log.ForContext<BlazorShutdownMiddleware>(); | ||
|
|
||
| // Covers both /_blazor/negotiate and the /_blazor transport request. | ||
| const string CircuitPath = "/_blazor"; | ||
|
|
||
| public Task Invoke(HttpContext context) { | ||
| if (!context.Request.Path.StartsWithSegments(CircuitPath, StringComparison.OrdinalIgnoreCase)) | ||
| return next(context); | ||
|
|
||
| if (!lifetime.ApplicationStopping.IsCancellationRequested) | ||
| return InvokeCircuit(context); | ||
|
|
||
| Log.Debug("Refusing a circuit request during shutdown"); | ||
| context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| async Task InvokeCircuit(HttpContext context) { | ||
| // Cancelling RequestAborted is not enough | ||
| using var registration = lifetime.ApplicationStopping.Register(() => { | ||
| Log.Debug("Aborting a circuit connection that was open when the shutdown began"); | ||
| context.Abort(); | ||
| }); | ||
| await next(context); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.