force-net / DeepCloner

Fast object cloner for .NET
MIT License
530 stars 73 forks source link

Possible Heap Corruption and Garbage Collection Interference... #27

Closed AnthonyAtApex closed 2 years ago

AnthonyAtApex commented 2 years ago

Howdy...

We spent the last 3 weeks working with MS on an issues without SOAP interface where we have integrated the DeepCloner tool. According to MS Support the DeepCloner is causing HEAP corruption which is then causing the Garbage Collector to throw errors due to Access Violations. This causes the dotNet6 framwork to crash and make the SOAP interface non-responsive.

Has anyone else see this issue? Any more info that the devs can use to fix this issue?

force-net commented 2 years ago

Is it only on .net6? Can you provide minimal snippet of code which cause this problem?

AnthonyAtApex commented 2 years ago

It's possible that it's in .Net 5 too, but it was exacerbated with .Net 6. QA says they've saw the SOAP issue since we implemented DeepCloner it just wasn't reproducible on demand like it was with .Net 6.

we were using it like this:

public static T? Clone(this T source) { return source == null ? default : source.DeepClone(); }

public static async Task<T?> CloneAsync(this T source) { return await Task.FromResult(source.Clone()); }

public static async Task<T?> CloneDataContractAsync(this T source) { return await Task.FromResult(source.CloneDataContract()); }

await sqlConnection.CloneAsync();
force-net commented 2 years ago

Please, do on clone SqlConnection object. DeepCloner will create not copy of connection, but copy of connection class. So, you'll have two simultaneous instances of one sql connection. Moreover, SqlConnection uses native resources which impossible to clone (it's out of .NET). So, when you Dispose one connection and use second - the second one uses already released native resources and this causes Access Violation.

DeepCloner is intended to copy DTO objects without complex logic on it. Yes, it can be used to copy any complex structure, but behavior is undefined for this.

AnthonyAtApex commented 2 years ago

That makes sense. Thanks.