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

Don't capture instance in Select for DataProtection. #13743

Merged
merged 2 commits into from
Sep 6, 2019

Conversation

jkotalik
Copy link
Contributor

@jkotalik jkotalik commented Sep 5, 2019

Fixes #13696.

Description
Customers using the Microsoft.AspNetCore.DataProtection.EntityFrameworkCore package would hit an exception when trying to access any data. This is due to changes in EntityFramework to detect memory leaks. This memory leak is actually

System.Security.Cryptography.CryptographicException
  HResult=0x80131501
  Message=An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information.
  Source=Microsoft.AspNetCore.DataProtection
  StackTrace:
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(Byte[] plaintext) in src\DataProtection\DataProtection\src\KeyManagement\KeyRingBasedDataProtector.cs:line 141
   at Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.Protect(IDataProtector protector, String plaintext) in src\DataProtection\Abstractions\src\DataProtectionCommonExtensions.cs:line 200
   at EntityFrameworkCoreSample.Program.Main(String[] args) in src\DataProtection\samples\EntityFrameworkCoreSample\Program.cs:line 38


Inner Exception 1:
InvalidOperationException: Client projection contains reference to constant expression of type: Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.EntityFrameworkCoreXmlRepository<EntityFrameworkCoreSample.DataProtectionKeyContext>. This could potentially cause memory leak.

Customer impact
In 3.0, this renders the package completely unusable.

Regression
Yes. The is a regression from 2.1 and 2.2 with regards to functionality. The memory leak still exists in 2.1 and 2.2 though, see below.

Risk
Low. This changes the call to TryParseKeyXml from an instance method to static.

cc @Pilchie @ajcvickers this change would also need to go into 2.1 and 2.2 as there is a memory leak. This memory leak shouldn't be hit often according to @blowdart , so I believe we need to publicly patch it.

@jkotalik jkotalik added area-dataprotection Includes: DataProtection ask-mode This issue / PR is a patch candidate which we will bar-check internally before patching it. labels Sep 5, 2019
Copy link
Contributor

@ajcvickers ajcvickers left a comment

Choose a reason for hiding this comment

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

Approving from my side, although as was stated elsewhere, we need some test coverage for this.

@jkotalik
Copy link
Contributor Author

jkotalik commented Sep 5, 2019

@ajcvickers there is coverage, however the tests are using InMemory rather than SqlServer. Should that be switched to use SqlLite?

@ajcvickers
Copy link
Contributor

ajcvickers commented Sep 5, 2019

@jkotalik No, in-memory should be okay in this case. Smit corrected me. Due to an issue we have with the in-memory provider it wouldn't catch this. So, switching to SQLite would have helped, but I don't think it is high priority.

@Pilchie
Copy link
Member

Pilchie commented Sep 6, 2019

Thanks for jumping on this @jkotalik. I'll get it in front of Tactics tomorrow.

@jkotalik
Copy link
Contributor Author

jkotalik commented Sep 6, 2019

This was approved by shiproom. I'm going ahead with merging.

@jkotalik
Copy link
Contributor Author

jkotalik commented Sep 9, 2019

So I took a look back and saw this was added in 2.2, so it doesn't apply to 2.1. @blowdart as this only applies to 2.2 and that it isn't a very frequent memory leak (leaks once every 6 months), do you still think we should consider servicing?

@blowdart
Copy link
Contributor

blowdart commented Sep 9, 2019 via email

@smuthiya
Copy link

The latest RC1 does not have this fix in it - still the DataProtection package in unusable with RC1.
Following is the latest package released which does not have this fix

https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection/3.0.0-rc1.19457.4

When this fix will be included and released?

@jkotalik
Copy link
Contributor Author

I’ll verify tomorrow. Thanks for letting us know.

@@ -20,6 +20,8 @@ static void Main(string[] args)
.AddDbContext<DataProtectionKeyContext>(o =>
{
o.UseInMemoryDatabase("DataProtection_EntityFrameworkCore");
// Make sure to create a sql server called DataProtectionApp
//o.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DataProtectionApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0");
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?

@jkotalik
Copy link
Contributor Author

@smuthiya for some reason, this commit wasn't included in the 3.0.0-rc1 release: https://github.com/aspnet/AspNetCore/commits/49e84ee5ff04b17f35cacb9c1d6ccf52d8328dad. This should be present in the next release, but I'll make sure it gets in.

@jkotalik
Copy link
Contributor Author

Ah I see. 3.0.0-rc1 was based on the 3.0.0-preview9 branch, while this commit was merged into release/3.0. Confusing 😢 .

This fix will be available in the next release.

@Pilchie
Copy link
Member

Pilchie commented Sep 24, 2019

Tagging @dougbu here to see if this change flowed properly?

@Pilchie
Copy link
Member

Pilchie commented Sep 24, 2019

Oh - it looks like there was a comment deleted that I was replying to. Never mind.

@smitpatel
Copy link
Contributor

Been through the same in the morning. 😄

@jkotalik
Copy link
Contributor Author

I got a feeling people are going to hit this issue fairly frequently (in their own code). @smitpatel do we have documented guidance here in EF?

@smitpatel
Copy link
Contributor

We have updated the exception message and added some easy way to deal with it in the message as per dotnet/efcore#17623
We have also added fwlink to the message which will point to guidance in EF docs, we have tracking issue to add detailed guidance in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dataprotection Includes: DataProtection ask-mode This issue / PR is a patch candidate which we will bar-check internally before patching it.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants