From f45b52b5fb50b956ae110066987efb4a908c0a34 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:43:45 +0000 Subject: [PATCH 1/5] Initial plan From 4af4cb57dd47b74719f5020d31447cb21077937e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:54:05 +0000 Subject: [PATCH 2/5] Fix connection cancellation interceptor dispatch and aggregation Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com> --- .../DbConnectionInterceptorAggregator.cs | 22 ++++ .../RelationalConnectionDiagnosticsLogger.cs | 5 +- .../Diagnostics/InterceptorAggregatorTest.cs | 29 +++++ .../RelationalConnectionTest.cs | 101 ++++++++++++++++++ 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs diff --git a/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs b/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs index 6bc0e8d5dc4..7c9550ad12e 100644 --- a/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs +++ b/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs @@ -194,6 +194,28 @@ await _interceptors[i].ConnectionDisposedAsync(connection, eventData) } } + public void ConnectionCanceled( + DbConnection connection, + ConnectionEndEventData eventData) + { + for (var i = 0; i < _interceptors.Length; i++) + { + _interceptors[i].ConnectionCanceled(connection, eventData); + } + } + + public async Task ConnectionCanceledAsync( + DbConnection connection, + ConnectionEndEventData eventData, + CancellationToken cancellationToken = default) + { + for (var i = 0; i < _interceptors.Length; i++) + { + await _interceptors[i].ConnectionCanceledAsync(connection, eventData, cancellationToken) + .ConfigureAwait(false); + } + } + public void ConnectionFailed( DbConnection connection, ConnectionErrorEventData eventData) diff --git a/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs b/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs index 0418bdc277f..53504a32b23 100644 --- a/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs +++ b/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs @@ -953,7 +953,10 @@ public virtual Task ConnectionCanceledAsync( diagnosticSourceEnabled, simpleLogEnabled); - interceptor?.ConnectionCanceled(connection.DbConnection, eventData); + if (interceptor != null) + { + return interceptor.ConnectionCanceledAsync(connection.DbConnection, eventData, cancellationToken); + } } return Task.CompletedTask; diff --git a/test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs b/test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs new file mode 100644 index 00000000000..d3075bd2482 --- /dev/null +++ b/test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs @@ -0,0 +1,29 @@ +// 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.Diagnostics.Internal; + +namespace Microsoft.EntityFrameworkCore.Diagnostics; + +public class InterceptorAggregatorTest +{ + [Theory] + [InlineData(typeof(DbConnectionInterceptorAggregator), typeof(IDbConnectionInterceptor))] + [InlineData(typeof(DbCommandInterceptorAggregator), typeof(IDbCommandInterceptor))] + [InlineData(typeof(DbTransactionInterceptorAggregator), typeof(IDbTransactionInterceptor))] + [InlineData(typeof(SaveChangesInterceptorAggregator), typeof(ISaveChangesInterceptor))] + [InlineData(typeof(IdentityResolutionInterceptorAggregator), typeof(IIdentityResolutionInterceptor))] + [InlineData(typeof(QueryExpressionInterceptorAggregator), typeof(IQueryExpressionInterceptor))] + [InlineData(typeof(MaterializationInterceptorAggregator), typeof(IMaterializationInterceptor))] + public void Composite_implements_all_interceptor_methods(Type aggregatorType, Type interceptorType) + { + var compositeType = Assert.Single( + aggregatorType.GetNestedTypes(BindingFlags.NonPublic), + interceptorType.IsAssignableFrom); + + foreach (var method in compositeType.GetInterfaceMap(interceptorType).TargetMethods) + { + Assert.Equal(compositeType, method.DeclaringType); + } + } +} diff --git a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs index d4ca81eb238..b290c0d513b 100644 --- a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs +++ b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs @@ -1084,6 +1084,107 @@ public async Task Reports_command_diagnostic_on_cancellation() }); } + [Theory] + [InlineData(false, 1, false)] + [InlineData(false, 2, false)] + [InlineData(true, 1, false)] + [InlineData(true, 2, false)] + [InlineData(true, 1, true)] + [InlineData(true, 2, true)] + public async Task Reports_connection_cancellation_to_interceptors(bool async, int interceptorCount, bool cancelWhileOpening) + { + var callbacks = new List(); + var interceptors = Enumerable.Range(0, interceptorCount).Select(_ => new CancellationInterceptor(callbacks)).ToArray(); + using var cancellationTokenSource = new CancellationTokenSource(); + using var dbConnection = new CancelingDbConnection(cancelWhileOpening ? cancellationTokenSource : null); + using var context = FakeRelationalTestHelpers.Instance.CreateContext( + new DbContextOptionsBuilder().UseFakeRelational(dbConnection).AddInterceptors(interceptors).Options); + using var connection = new CancelingRelationalConnection( + context.GetService()); + + var exception = async + ? await Assert.ThrowsAsync(() => connection.OpenAsync(cancellationTokenSource.Token)) + : Assert.Throws(() => connection.Open()); + + Assert.Same(dbConnection.Exception, exception); + Assert.Equal(ConnectionState.Closed, dbConnection.State); + Assert.Equal(interceptors, callbacks); + foreach (var interceptor in interceptors) + { + Assert.Equal(async, interceptor.AsyncCalled); + Assert.NotEqual(async, interceptor.SyncCalled); + Assert.Equal(async ? cancellationTokenSource.Token : default, interceptor.CancellationToken); + var eventData = Assert.IsType(interceptor.EventData); + Assert.Equal(async, eventData.IsAsync); + Assert.Same(dbConnection, eventData.Connection); + Assert.Same(context, eventData.Context); + Assert.Equal(connection.ConnectionId, eventData.ConnectionId); + Assert.Same(interceptors[0].EventData, eventData); + } + } + + private class CancelingRelationalConnection(RelationalConnectionDependencies dependencies) + : RelationalConnection(dependencies) + { + protected override DbConnection CreateDbConnection() + => throw new NotImplementedException(); + } + + private class CancelingDbConnection(CancellationTokenSource? cancellationTokenSource) : FakeDbConnection("Database=FrodoLives") + { + public OperationCanceledException Exception { get; } = new(); + + public override void Open() + => throw Exception; + + public override Task OpenAsync(CancellationToken cancellationToken) + { + cancellationTokenSource?.Cancel(); + return Task.FromException(Exception); + } + } + + private class CancellationInterceptor(List callbacks) : DbConnectionInterceptor + { + public bool SyncCalled { get; private set; } + public bool AsyncCalled { get; private set; } + public CancellationToken CancellationToken { get; private set; } + public ConnectionEndEventData? EventData { get; private set; } + + public override void ConnectionCanceled(DbConnection connection, ConnectionEndEventData eventData) + { + Assert.Same(connection, eventData.Connection); + SyncCalled = true; + EventData = eventData; + callbacks.Add(this); + } + + public override async Task ConnectionCanceledAsync( + DbConnection connection, + ConnectionEndEventData eventData, + CancellationToken cancellationToken = default) + { + await Task.Yield(); + Assert.Same(connection, eventData.Connection); + AsyncCalled = true; + CancellationToken = cancellationToken; + EventData = eventData; + callbacks.Add(this); + } + + public override void ConnectionFailed(DbConnection connection, ConnectionErrorEventData eventData) + => Assert.Fail("Cancellation should not be reported as a failure."); + + public override Task ConnectionFailedAsync( + DbConnection connection, + ConnectionErrorEventData eventData, + CancellationToken cancellationToken = default) + { + Assert.Fail("Cancellation should not be reported as a failure."); + return Task.CompletedTask; + } + } + [Fact] public async Task HandleTransactionCompleted_with_concurrent_ClearTransactions_is_thread_safe() { From fd3addf8ed7edebe51d560f19dd41fa42f9815c1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:12:12 +0000 Subject: [PATCH 3/5] Remove interceptor aggregator completeness test per review Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com> --- .../Diagnostics/InterceptorAggregatorTest.cs | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs diff --git a/test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs b/test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs deleted file mode 100644 index d3075bd2482..00000000000 --- a/test/EFCore.Relational.Tests/Diagnostics/InterceptorAggregatorTest.cs +++ /dev/null @@ -1,29 +0,0 @@ -// 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.Diagnostics.Internal; - -namespace Microsoft.EntityFrameworkCore.Diagnostics; - -public class InterceptorAggregatorTest -{ - [Theory] - [InlineData(typeof(DbConnectionInterceptorAggregator), typeof(IDbConnectionInterceptor))] - [InlineData(typeof(DbCommandInterceptorAggregator), typeof(IDbCommandInterceptor))] - [InlineData(typeof(DbTransactionInterceptorAggregator), typeof(IDbTransactionInterceptor))] - [InlineData(typeof(SaveChangesInterceptorAggregator), typeof(ISaveChangesInterceptor))] - [InlineData(typeof(IdentityResolutionInterceptorAggregator), typeof(IIdentityResolutionInterceptor))] - [InlineData(typeof(QueryExpressionInterceptorAggregator), typeof(IQueryExpressionInterceptor))] - [InlineData(typeof(MaterializationInterceptorAggregator), typeof(IMaterializationInterceptor))] - public void Composite_implements_all_interceptor_methods(Type aggregatorType, Type interceptorType) - { - var compositeType = Assert.Single( - aggregatorType.GetNestedTypes(BindingFlags.NonPublic), - interceptorType.IsAssignableFrom); - - foreach (var method in compositeType.GetInterfaceMap(interceptorType).TargetMethods) - { - Assert.Equal(compositeType, method.DeclaringType); - } - } -} From a4bf3de15db5dfe04584cc1eedfd5f28a0dd6561 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Wed, 21 Oct 2026 15:42:56 -0700 Subject: [PATCH 4/5] Add tests for interceptor aggregator implementations Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../RelationalConnectionTest.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs index b290c0d513b..d5218323b5a 100644 --- a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs +++ b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs @@ -1084,6 +1084,24 @@ public async Task Reports_command_diagnostic_on_cancellation() }); } + [Theory] + [InlineData(typeof(IDbCommandInterceptor), typeof(Diagnostics.Internal.DbCommandInterceptorAggregator))] + [InlineData(typeof(IDbConnectionInterceptor), typeof(Diagnostics.Internal.DbConnectionInterceptorAggregator))] + [InlineData(typeof(IDbTransactionInterceptor), typeof(Diagnostics.Internal.DbTransactionInterceptorAggregator))] + [InlineData(typeof(ISaveChangesInterceptor), typeof(Diagnostics.Internal.SaveChangesInterceptorAggregator))] + [InlineData(typeof(IIdentityResolutionInterceptor), typeof(Diagnostics.Internal.IdentityResolutionInterceptorAggregator))] + [InlineData(typeof(IMaterializationInterceptor), typeof(Diagnostics.Internal.MaterializationInterceptorAggregator))] + [InlineData(typeof(IQueryExpressionInterceptor), typeof(Diagnostics.Internal.QueryExpressionInterceptorAggregator))] + public void Interceptor_aggregators_implement_all_interface_methods(Type interceptorType, Type aggregatorType) + { + var compositeType = Assert.Single( + aggregatorType.GetNestedTypes(BindingFlags.NonPublic).Where(interceptorType.IsAssignableFrom)); + + Assert.All( + compositeType.GetInterfaceMap(interceptorType).TargetMethods, + method => Assert.Equal(compositeType, method.DeclaringType)); + } + [Theory] [InlineData(false, 1, false)] [InlineData(false, 2, false)] From 5166c7a10e0ddef5bbf82533b7c4495e0c0046b8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 31 Oct 2026 00:42:47 +0000 Subject: [PATCH 5/5] Use Assert.Single predicate overload in aggregator test Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com> --- test/EFCore.Relational.Tests/RelationalConnectionTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs index d5218323b5a..34f4a3da935 100644 --- a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs +++ b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs @@ -1095,7 +1095,7 @@ public async Task Reports_command_diagnostic_on_cancellation() public void Interceptor_aggregators_implement_all_interface_methods(Type interceptorType, Type aggregatorType) { var compositeType = Assert.Single( - aggregatorType.GetNestedTypes(BindingFlags.NonPublic).Where(interceptorType.IsAssignableFrom)); + aggregatorType.GetNestedTypes(BindingFlags.NonPublic), interceptorType.IsAssignableFrom); Assert.All( compositeType.GetInterfaceMap(interceptorType).TargetMethods,