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

Fix caching of resettable services for pooled uninitialized context #25086

Merged
merged 1 commit into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
Expand Down Expand Up @@ -788,7 +789,7 @@ void IDbContextPoolable.SnapshotConfiguration()
[EntityFrameworkInternal]
void IResettableService.ResetState()
{
foreach (var service in _cachedResettableServices ??= GetResettableServices())
foreach (var service in GetResettableServices())
{
service.ResetState();
}
Expand All @@ -807,7 +808,7 @@ void IResettableService.ResetState()
[EntityFrameworkInternal]
async Task IResettableService.ResetStateAsync(CancellationToken cancellationToken)
{
foreach (var service in _cachedResettableServices ??= GetResettableServices())
foreach (var service in GetResettableServices())
{
await service.ResetStateAsync(cancellationToken).ConfigureAwait(false);
}
Expand All @@ -817,22 +818,29 @@ async Task IResettableService.ResetStateAsync(CancellationToken cancellationToke
_disposed = true;
}

private List<IResettableService> GetResettableServices()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private IEnumerable<IResettableService> GetResettableServices()
{
var resettableServices = new List<IResettableService>();
if (_cachedResettableServices is not null)
{
return _cachedResettableServices;
}

var services
= _contextServices?.InternalServiceProvider?
.GetService<IEnumerable<IResettableService>>();
var resettableServices = new List<IResettableService>();

if (services != null)
var services = _contextServices?.InternalServiceProvider.GetService<IEnumerable<IResettableService>>();
if (services is not null)
{
resettableServices.AddRange(services);

// Note that if the context hasn't been initialized yet, we don't cache the resettable services
// (since some services haven't been added yet).
_cachedResettableServices = resettableServices;
}

if (_sets != null)
if (_sets is not null)
{
resettableServices.AddRange((_sets.Values.OfType<IResettableService>()));
resettableServices.AddRange(_sets.Values.OfType<IResettableService>());
}

return resettableServices;
Expand Down
37 changes: 37 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/DbContextPoolingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,17 @@ private interface ISecondContext

private class SecondContext : DbContext, ISecondContext
{
public DbSet<Blog> Blogs { get; set; }

public SecondContext(DbContextOptions options)
: base(options)
{
}

public class Blog
{
public int Id { get; set; }
}
}

[ConditionalFact]
Expand Down Expand Up @@ -717,6 +724,36 @@ public async Task Context_configuration_is_reset(bool useInterface, bool async)
Assert.Null(context1.Database.GetCommandTimeout());
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task Uninitialized_context_configuration_is_reset_properly(bool async)
{
var serviceProvider = BuildServiceProvider<SecondContext>();

DbContext ctx;
using (var scope = serviceProvider.CreateScope())
using (var ctx1 = scope.ServiceProvider.GetRequiredService<SecondContext>())
{
await ctx1.DisposeAsync();
ctx = ctx1;
}

using (var scope = serviceProvider.CreateScope())
using (var ctx2 = scope.ServiceProvider.GetRequiredService<SecondContext>())
{
Assert.Same(ctx, ctx2);
ctx2.Blogs.Add(new SecondContext.Blog());
}

using (var scope = serviceProvider.CreateScope())
using (var ctx3 = scope.ServiceProvider.GetRequiredService<SecondContext>())
{
Assert.Same(ctx, ctx3);
Assert.Empty(ctx3.ChangeTracker.Entries());
}
}

[ConditionalTheory]
[InlineData(false, false)]
[InlineData(true, false)]
Expand Down