Skip to content

Commit 3f73a0f

Browse files
authored
Ignore RequestAborted on Session commit #3479 (#11858)
1 parent 33eb8ba commit 3f73a0f

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

src/Middleware/Session/Session.slnf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"solution": {
3+
"path": "..\\Middleware.sln",
4+
"projects": [
5+
"..\\DataProtection\\Abstractions\\src\\Microsoft.AspNetCore.DataProtection.Abstractions.csproj",
6+
"..\\DataProtection\\Cryptography.Internal\\src\\Microsoft.AspNetCore.Cryptography.Internal.csproj",
7+
"..\\DataProtection\\DataProtection\\src\\Microsoft.AspNetCore.DataProtection.csproj",
8+
"..\\DefaultBuilder\\src\\Microsoft.AspNetCore.csproj",
9+
"..\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",
10+
"..\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj",
11+
"..\\Hosting\\Server.Abstractions\\src\\Microsoft.AspNetCore.Hosting.Server.Abstractions.csproj",
12+
"..\\Hosting\\Server.IntegrationTesting\\src\\Microsoft.AspNetCore.Server.IntegrationTesting.csproj",
13+
"..\\Hosting\\TestHost\\src\\Microsoft.AspNetCore.TestHost.csproj",
14+
"..\\Http\\Authentication.Abstractions\\src\\Microsoft.AspNetCore.Authentication.Abstractions.csproj",
15+
"..\\Http\\Authentication.Core\\src\\Microsoft.AspNetCore.Authentication.Core.csproj",
16+
"..\\Http\\Http.Abstractions\\src\\Microsoft.AspNetCore.Http.Abstractions.csproj",
17+
"..\\Http\\Http.Extensions\\src\\Microsoft.AspNetCore.Http.Extensions.csproj",
18+
"..\\Http\\Http.Features\\src\\Microsoft.AspNetCore.Http.Features.csproj",
19+
"..\\Http\\Metadata\\src\\Microsoft.AspNetCore.Metadata.csproj",
20+
"..\\Http\\Routing.Abstractions\\src\\Microsoft.AspNetCore.Routing.Abstractions.csproj",
21+
"..\\Http\\WebUtilities\\src\\Microsoft.AspNetCore.WebUtilities.csproj",
22+
"..\\Security\\Authorization\\Core\\src\\Microsoft.AspNetCore.Authorization.csproj",
23+
"..\\Servers\\Connections.Abstractions\\src\\Microsoft.AspNetCore.Connections.Abstractions.csproj",
24+
"..\\Servers\\HttpSys\\src\\Microsoft.AspNetCore.Server.HttpSys.csproj",
25+
"..\\Servers\\IIS\\IISIntegration\\src\\Microsoft.AspNetCore.Server.IISIntegration.csproj",
26+
"..\\Servers\\IIS\\IIS\\src\\Microsoft.AspNetCore.Server.IIS.csproj",
27+
"..\\Servers\\Kestrel\\Core\\src\\Microsoft.AspNetCore.Server.Kestrel.Core.csproj",
28+
"..\\Servers\\Kestrel\\Kestrel\\src\\Microsoft.AspNetCore.Server.Kestrel.csproj",
29+
"..\\Servers\\Kestrel\\Transport.Sockets\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj",
30+
"..\\http\\Headers\\src\\Microsoft.Net.Http.Headers.csproj",
31+
"..\\http\\Routing\\src\\Microsoft.AspNetCore.Routing.csproj",
32+
"..\\http\\http\\src\\Microsoft.AspNetCore.Http.csproj",
33+
"Diagnostics.Abstractions\\src\\Microsoft.AspNetCore.Diagnostics.Abstractions.csproj",
34+
"Diagnostics\\src\\Microsoft.AspNetCore.Diagnostics.csproj",
35+
"HostFiltering\\src\\Microsoft.AspNetCore.HostFiltering.csproj",
36+
"HttpOverrides\\src\\Microsoft.AspNetCore.HttpOverrides.csproj",
37+
"Session\\samples\\SessionSample.csproj",
38+
"Session\\src\\Microsoft.AspNetCore.Session.csproj",
39+
"Session\\test\\Microsoft.AspNetCore.Session.Tests.csproj"
40+
]
41+
}
42+
}

src/Middleware/Session/src/SessionMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public async Task Invoke(HttpContext context)
115115
{
116116
try
117117
{
118-
await feature.Session.CommitAsync(context.RequestAborted);
118+
await feature.Session.CommitAsync();
119119
}
120120
catch (OperationCanceledException)
121121
{

src/Middleware/Session/test/SessionTests.cs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,64 @@ public async Task SessionLogsCacheCommitCanceledException()
855855
var token = cts.Token;
856856
cts.Cancel();
857857
await Assert.ThrowsAsync<OperationCanceledException>(() => context.Session.CommitAsync(token));
858+
});
859+
})
860+
.ConfigureServices(services =>
861+
{
862+
services.AddSingleton(typeof(ILoggerFactory), loggerFactory);
863+
services.AddSingleton<IDistributedCache>(new UnreliableCache(new MemoryCache(new MemoryCacheOptions()))
864+
{
865+
DelaySetAsync = true
866+
});
867+
services.AddSession();
868+
});
869+
870+
using (var server = new TestServer(builder))
871+
{
872+
var client = server.CreateClient();
873+
var response = await client.GetAsync(string.Empty);
874+
response.EnsureSuccessStatusCode();
875+
}
876+
877+
// The session is automatically committed on unwind even after the manual commit was canceled.
878+
var sessionLogMessages = sink.Writes.Where(message => message.LoggerName.Equals(typeof(DistributedSession).FullName, StringComparison.Ordinal)).ToList();
879+
880+
Assert.Contains("Session started", sessionLogMessages[0].State.ToString());
881+
Assert.Equal(LogLevel.Information, sessionLogMessages[0].LogLevel);
882+
883+
Assert.Contains("Session stored", sessionLogMessages[1].State.ToString());
884+
Assert.Equal(LogLevel.Debug, sessionLogMessages[1].LogLevel);
885+
886+
Assert.Empty(sink.Writes.Where(message => message.LoggerName.Equals(typeof(SessionMiddleware).FullName, StringComparison.Ordinal)));
887+
}
888+
889+
[Fact]
890+
public async Task RequestAbortedIgnored()
891+
{
892+
var sink = new TestSink(
893+
writeContext =>
894+
{
895+
return writeContext.LoggerName.Equals(typeof(SessionMiddleware).FullName)
896+
|| writeContext.LoggerName.Equals(typeof(DistributedSession).FullName);
897+
},
898+
beginScopeContext =>
899+
{
900+
return beginScopeContext.LoggerName.Equals(typeof(SessionMiddleware).FullName)
901+
|| beginScopeContext.LoggerName.Equals(typeof(DistributedSession).FullName);
902+
});
903+
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
904+
var builder = new WebHostBuilder()
905+
.Configure(app =>
906+
{
907+
app.UseSession();
908+
app.Run(context =>
909+
{
910+
context.Session.SetInt32("key", 0);
911+
var cts = new CancellationTokenSource();
912+
var token = cts.Token;
913+
cts.Cancel();
858914
context.RequestAborted = token;
915+
return Task.CompletedTask;
859916
});
860917
})
861918
.ConfigureServices(services =>
@@ -875,12 +932,15 @@ public async Task SessionLogsCacheCommitCanceledException()
875932
response.EnsureSuccessStatusCode();
876933
}
877934

878-
Assert.Empty(sink.Writes.Where(message => message.LoggerName.Equals(typeof(DistributedSession).FullName, StringComparison.Ordinal)));
935+
var sessionLogMessages = sink.Writes.Where(message => message.LoggerName.Equals(typeof(DistributedSession).FullName, StringComparison.Ordinal)).ToList();
879936

880-
var sessionMiddlewareLogs = sink.Writes.Where(message => message.LoggerName.Equals(typeof(SessionMiddleware).FullName, StringComparison.Ordinal)).ToList();
937+
Assert.Contains("Session started", sessionLogMessages[0].State.ToString());
938+
Assert.Equal(LogLevel.Information, sessionLogMessages[0].LogLevel);
881939

882-
Assert.Contains("Committing the session was canceled.", sessionMiddlewareLogs[0].State.ToString());
883-
Assert.Equal(LogLevel.Information, sessionMiddlewareLogs[0].LogLevel);
940+
Assert.Contains("Session stored", sessionLogMessages[1].State.ToString());
941+
Assert.Equal(LogLevel.Debug, sessionLogMessages[1].LogLevel);
942+
943+
Assert.Empty(sink.Writes.Where(message => message.LoggerName.Equals(typeof(SessionMiddleware).FullName, StringComparison.Ordinal)));
884944
}
885945

886946
[Fact]

0 commit comments

Comments
 (0)