Skip to content

Commit

Permalink
Query: Dispose inner data readers for split query (#21454)
Browse files Browse the repository at this point in the history
Part of #21441
  • Loading branch information
smitpatel committed Jun 29, 2020
1 parent b88ba0a commit 709535d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
23 changes: 17 additions & 6 deletions src/EFCore.Relational/Query/Internal/SplitQueryingEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ private bool InitializeReader(DbContext _, bool result)
public void Dispose()
{
_dataReader?.Dispose();
foreach (var dataReader in _resultCoordinator.DataReaders)
{
dataReader?.DataReader.Dispose();
}
_resultCoordinator.DataReaders.Clear();

_dataReader = null;
}

Expand Down Expand Up @@ -300,17 +306,22 @@ private async Task<bool> InitializeReaderAsync(DbContext _, bool result, Cancell
return result;
}

public ValueTask DisposeAsync()
public async ValueTask DisposeAsync()
{
if (_dataReader != null)
{
var dataReader = _dataReader;
_dataReader = null;
await _dataReader.DisposeAsync().ConfigureAwait(false);
foreach (var dataReader in _resultCoordinator.DataReaders)
{
if (dataReader != null)
{
await dataReader.DataReader.DisposeAsync().ConfigureAwait(false);
}
}
_resultCoordinator.DataReaders.Clear();

return dataReader.DisposeAsync();
_dataReader = null;
}

return default;
}
}
}
Expand Down
65 changes: 61 additions & 4 deletions test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.Caching.Memory;
Expand Down Expand Up @@ -7467,6 +7468,62 @@ ORDER BY [p].[Id]"
});
}

[ConditionalFact]
public virtual void SplitQuery_disposes_inner_data_readers()
{
var (options, testSqlLoggerFactory) = CreateOptions21355(null);
using var context = new BugContext21355(options);
var dbConnection = context.Database.GetDbConnection();

Assert.Equal(ConnectionState.Closed, dbConnection.State);

context.Parents.Include(p => p.Children1).Include(p => p.Children2).AsSplitQuery().ToList();

Assert.Equal(ConnectionState.Closed, dbConnection.State);
}

[ConditionalFact]
public virtual async Task SplitQuery_disposes_inner_data_readers_async()
{
var (options, testSqlLoggerFactory) = CreateOptions21355(null);
using var context = new BugContext21355(options);
var dbConnection = context.Database.GetDbConnection();

Assert.Equal(ConnectionState.Closed, dbConnection.State);

await context.Parents.Include(p => p.Children1).Include(p => p.Children2).AsSplitQuery().ToListAsync();

Assert.Equal(ConnectionState.Closed, dbConnection.State);
}

[ConditionalFact]
public virtual void SplitQuery_disposes_inner_data_readers_single()
{
var (options, testSqlLoggerFactory) = CreateOptions21355(null);
using var context = new BugContext21355(options);
var dbConnection = context.Database.GetDbConnection();

Assert.Equal(ConnectionState.Closed, dbConnection.State);

context.Parents.Include(p => p.Children1).Include(p => p.Children2).AsSplitQuery().Single();

Assert.Equal(ConnectionState.Closed, dbConnection.State);
}

[ConditionalFact]
public virtual async Task SplitQuery_disposes_inner_data_readers_single_async()
{
var (options, testSqlLoggerFactory) = CreateOptions21355(null);
using var context = new BugContext21355(options);
var dbConnection = context.Database.GetDbConnection();

Assert.Equal(ConnectionState.Closed, dbConnection.State);

await context.Parents.Include(p => p.Children1).Include(p => p.Children2).AsSplitQuery().SingleAsync();

Assert.Equal(ConnectionState.Closed, dbConnection.State);
}

private class Parent21355
{
public string Id { get; set; }
Expand Down Expand Up @@ -7511,10 +7568,10 @@ private class AnotherChild21355
{
Id = "Parent1",
Children1 = new List<Child21355>
{
new Child21355(),
new Child21355()
}
{
new Child21355(),
new Child21355()
}
});
context.SaveChanges();
}
Expand Down

0 comments on commit 709535d

Please sign in to comment.