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

Breaking change: Throw when resolving on a disposed service provider #45116

Merged
merged 18 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -17,7 +17,7 @@ internal class ServiceProviderEngineScope : IServiceScope, IServiceProvider, IAs
private List<object> _disposables;

private bool _disposed;
private static object s_locker = new object();
private object _locker = new object();
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved

public ServiceProviderEngineScope(ServiceProviderEngine engine)
{
Expand Down Expand Up @@ -49,7 +49,7 @@ internal object CaptureDisposable(object service)
return service;
}

lock (s_locker)
lock (_locker)
{
if (_disposed)
{
Expand Down Expand Up @@ -152,7 +152,7 @@ static async ValueTask Await(int i, ValueTask vt, List<object> toDispose)
private List<object> BeginDispose()
{
List<object> toDispose;
lock (s_locker)
lock (_locker)
{
if (_disposed)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Fakes;
using Microsoft.Extensions.DependencyInjection.Specification;
using Microsoft.Extensions.DependencyInjection.Specification.Fakes;
Expand Down Expand Up @@ -266,55 +265,62 @@ public void ScopeDispose_PreventsServiceResolution()
//Check that resolution from root works
Assert.NotNull(provider.CreateScope());
}

[ThreadStatic]
public static int ThreadId;

[Fact]
public async Task ServiceProviderDispose_ContinueResolvingServices_Throws()
private void GetService_ThenDisposeOnDifferentThread_ServiceDisposed()
{
var services = new ServiceCollection();
ServiceProvider sp = null;
services.AddSingleton<Foo>();
var sp = services.BuildServiceProvider();
var foo = sp.GetRequiredService<Foo>();
Task.Run(() => sp.Dispose()).Wait();

Assert.Equal(
Foo.TimesConstructorCalled,
Foo.TimesDisposeCalled);
}

var lazy = new Lazy<Thing1>(() =>
[Fact]
public void GetService_DisposeOnSameThread_Throws()
{
var services = new ServiceCollection();
services.AddSingleton<DisposableFoo>();
var sp = services.BuildServiceProvider();
Assert.Throws<ObjectDisposedException>(() =>
{
// Tries to take the singleton lock (global)
return new Thing1(sp);
// foo ctor disposes ServiceProvider
var foo = sp.GetRequiredService<DisposableFoo>();
});
}

services.AddTransient(sp =>
public class Foo : IDisposable
{
public static int TimesConstructorCalled = 0;
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
public static int TimesDisposeCalled = 0;
public Foo()
{
if (ThreadId == 1)
{
Thread.Sleep(1000);
}
else
{
// Let Thread 1 over take Thread 2
Thread.Sleep(3000);
}

return lazy.Value;
});
services.AddSingleton<Thing2>();

sp = services.BuildServiceProvider();

var t1 = Task.Run(() =>
Interlocked.Increment(ref TimesConstructorCalled);
Thread.Sleep(5000);
Copy link
Member

Choose a reason for hiding this comment

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

What is this testing?

Copy link
Member Author

@maryamariyan maryamariyan Dec 1, 2020

Choose a reason for hiding this comment

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

If during resolve of this service, service provider gets disposed on another thread, the test asserts that we dispose of the service as well.

Copy link
Member

Choose a reason for hiding this comment

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

But the way the test is written above, this sleep will happen inline and then the object will be returned on line 276.

Once the object is returned, then you are starting a new thread to dispose the service provider.

So I don't think the sleep is doing anything beneficial.

Copy link
Member

Choose a reason for hiding this comment

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

Good catch, we need to kick off 2 threads.

Copy link
Member Author

@maryamariyan maryamariyan Dec 2, 2020

Choose a reason for hiding this comment

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

If during resolve of this service, service provider gets disposed on another thread, the test asserts that we dispose of the service as well.

Removed this test and instead used tests below to also assert that the service (for both IDisposable and IAsyncDisposable cases) get disposed:

  • GetAsyncService_DisposeAsyncOnSameThread_ThrowsAndDoesNotHangAndDisposeAsyncGetsCalled
  • GetService_DisposeOnSameThread_ThrowsAndDoesNotHangAndDisposeGetsCalled

The removed case would have been very similar in outcome to the two test cases I mentioned above, so perhaps OK that we don't add it.

}
public void Dispose()
{
ThreadId = 1;
using var scope1 = sp.CreateScope();
scope1.ServiceProvider.GetRequiredService<Thing1>();
});
Interlocked.Increment(ref TimesDisposeCalled);
}
}

var t2 = Task.Run(() =>
public class DisposableFoo : IDisposable
{
public static int TimesConstructorCalled = 0;
public static int TimesDisposeCalled = 0;
public DisposableFoo(IServiceProvider sp)
{
ThreadId = 2;
using var scope2 = sp.CreateScope();
scope2.ServiceProvider.GetRequiredService<Thing2>();
});

await Assert.ThrowsAsync<ObjectDisposedException>(async () => { await t1; await t2; });
Interlocked.Increment(ref TimesConstructorCalled);
(sp as IDisposable).Dispose();
}
public void Dispose()
{
Interlocked.Increment(ref TimesDisposeCalled);
}
}

public class Thing2 : IDisposable
Expand Down