Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ private List<object> BeginDispose()
toDispose = _disposables;
_disposables = null;

ResolvedServices.Clear();
// Not clearing ResolvedServices here because there might be a compilation running in background
// trying to get a cached singleton service instance and if it won't find
// it it will try to create a new one tripping the Debug.Assert in CaptureDisposable
// and leaking a Disposable object in Release mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should happen in CaptureDisposable if the container is already disposed? Shouldn't we throw an ODE that can be observed by the top-level GetService() call?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't get called after dispose. We have ODE checks on all customer accessed entry points. This one is internal that that's why it was able to slip past those checks.

}

return toDispose;
Expand Down
21 changes: 21 additions & 0 deletions src/DependencyInjection/DI/test/ServiceProviderEngineScopeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Extensions.DependencyInjection.Specification.Fakes;
using Xunit;

namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
{
public class ServiceProviderEngineScopeTests
{
[Fact]
public void Dispose_DoesntClearResolvedServices()
{
var serviceProviderEngineScope = new ServiceProviderEngineScope(null);
serviceProviderEngineScope.ResolvedServices.Add(new ServiceCacheKey(typeof(IFakeService), 0), null);
serviceProviderEngineScope.Dispose();

Assert.Single(serviceProviderEngineScope.ResolvedServices);
}
}
}
6 changes: 3 additions & 3 deletions src/DependencyInjection/DependencyInjection.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.28508.60
MinimumVisualStudioVersion = 16.0.0.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DI", "DI", "{B4373F1E-3E9F-49DA-B1BD-CA440932CF52}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Extensions.DependencyInjection.Performance", "DI\perf\Microsoft.Extensions.DependencyInjection.Performance.csproj", "{27C11410-9269-452B-ACF6-BC2E46695DC3}"
Expand Down