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

Clear reference to DbDataReader from RelationalDataReader #28989

Merged
merged 1 commit into from
Sep 7, 2022
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
40 changes: 30 additions & 10 deletions src/EFCore.Relational/Storage/RelationalDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,22 @@ public virtual void Dispose()
{
_disposed = true;

if (!interceptionResult.IsSuppressed)
try
{
_reader.Dispose();
_command.Parameters.Clear();
_command.Dispose();
_relationalConnection.Close();
if (!interceptionResult.IsSuppressed)
{
_reader.Dispose();
_command.Parameters.Clear();
_command.Dispose();
_relationalConnection.Close();
}
}
finally
{
_reader = null!;
_command = null!;
_relationalConnection = null!;
_logger = null;
}
}
}
Expand Down Expand Up @@ -259,12 +269,22 @@ public virtual async ValueTask DisposeAsync()
{
_disposed = true;

if (!interceptionResult.IsSuppressed)
try
{
if (!interceptionResult.IsSuppressed)
{
await _reader.DisposeAsync().ConfigureAwait(false);
_command.Parameters.Clear();
await _command.DisposeAsync().ConfigureAwait(false);
await _relationalConnection.CloseAsync().ConfigureAwait(false);
}
}
finally
{
await _reader.DisposeAsync().ConfigureAwait(false);
_command.Parameters.Clear();
await _command.DisposeAsync().ConfigureAwait(false);
await _relationalConnection.CloseAsync().ConfigureAwait(false);
_reader = null!;
_command = null!;
_relationalConnection = null!;
_logger = null;
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions test/EFCore.Relational.Tests/Storage/RelationalDataReaderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Storage.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider;

// ReSharper disable MethodHasAsyncOverload

namespace Microsoft.EntityFrameworkCore.Storage;

public class RelationalDataReaderTest
{
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Does_not_hold_reference_to_DbDataReader_after_dispose(bool async)
{
var fakeConnection = CreateConnection();
var relationalCommand = CreateRelationalCommand(commandText: "CommandText");

var reader = relationalCommand.ExecuteReader(new(
fakeConnection,
new Dictionary<string, object>(),
readerColumns: null,
context: null,
logger: null));

Assert.NotNull(reader.DbDataReader);

if (async)
{
await reader.DisposeAsync();
}
else
{
reader.Dispose();
}

Assert.Null(reader.DbDataReader);
}

private const string ConnectionString = "Fake Connection String";

private static FakeRelationalConnection CreateConnection(IDbContextOptions options = null)
=> new(options ?? CreateOptions());

private static IDbContextOptions CreateOptions(
RelationalOptionsExtension optionsExtension = null)
{
var optionsBuilder = new DbContextOptionsBuilder();

((IDbContextOptionsBuilderInfrastructure)optionsBuilder)
.AddOrUpdateExtension(
optionsExtension
?? new FakeRelationalOptionsExtension().WithConnectionString(ConnectionString));

return optionsBuilder.Options;
}

private IRelationalCommand CreateRelationalCommand(
string commandText = "Command Text",
IReadOnlyList<IRelationalParameter> parameters = null)
=> new RelationalCommand(
new RelationalCommandBuilderDependencies(
new TestRelationalTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>()),
new ExceptionDetector()),
commandText,
parameters ?? Array.Empty<IRelationalParameter>());

public static IEnumerable<object[]> IsAsyncData = new[] { new object[] { false }, new object[] { true } };
}