Skip to content

Disposing scopes more than once should work #50852

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

Merged
merged 1 commit into from
Apr 8, 2021
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 @@ -176,6 +176,12 @@ private void ClearState()
// try to return to the pool while somebody is trying to access ResolvedServices.
lock (_scopeLock)
{
// Don't attempt to dispose if we're already disposed
if (_state == null)
Copy link
Member Author

Choose a reason for hiding this comment

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

I could also check _disposed.

Copy link
Member

Choose a reason for hiding this comment

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

Why do we only set _state to null below if Return returns true? We don't want to always null it out?

Copy link
Member Author

Choose a reason for hiding this comment

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

Super weird edge case around root scopes vs child scopes #50463 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Maybe add the comment to the code, so we remember next time?

Copy link
Member

Choose a reason for hiding this comment

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

Note, it also doesn't get nulled out when the pool is full.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's in the comment below this statement.

{
return;
}

// ResolvedServices is never cleared for singletons because there might be a compilation running in background
// trying to get a cached singleton service. If it doesn't find it
// it will try to create a new one which will result in an ObjectDisposedException.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public void ResolvedServicesAfterDispose_ThrowsObjectDispose()
Assert.Throws<ObjectDisposedException>(() => serviceProviderEngineScope.ResolvedServices);
}

[Fact]
public void DoubleDisposeWorks()
{
var engine = new FakeEngine();
var serviceProviderEngineScope = new ServiceProviderEngineScope(engine);
serviceProviderEngineScope.ResolvedServices.Add(new ServiceCacheKey(typeof(IFakeService), 0), null);
serviceProviderEngineScope.Dispose();
serviceProviderEngineScope.Dispose();
}

private class FakeEngine : ServiceProviderEngine
{
public FakeEngine() :
Expand Down