castleproject / Core

Castle Core, including Castle DynamicProxy, Logging Services and DictionaryAdapter
http://www.castleproject.org/
Other
2.2k stars 467 forks source link

Intermittent VerificationException 'violates the constraint of type parameter...' #680

Open SwissDanillo opened 2 weeks ago

SwissDanillo commented 2 weeks ago

In our software we are experiencing VerificationExceptions of this kind:

System.Security.VerificationException : Method Castle.Proxies.[some method] type argument '[some type]' violates the constraint of type parameter 'Txxx'

We tried to get hold of the generated proxies to analyze what is generated and why this exception occurs but didn't find a way yet on how to get the JITted code.

We got so far that we proofed the constraints are met but the exception is still thrown. This leads us to the conclusion that something is generated in the wrong way within Castle.Core.

We tried to have an example to reproduce this issue but didn't find a way to do so yet. The only source we have is our software running on different computers and experiencing this issue from time to time and in the last weeks more often. Restarting the software helps to get the system back to running.

Components used:

Questions:

Looking forward to bring some light into this issue.

stakx commented 2 weeks ago

There's really not a lot we can do without any repro code example, sorry. Can you try to reduce your actual code to something more minimal that you could share with us? It doesn't matter if it does not repro the error in 100% of all runs, if it is known to repro the issue every so often that'd be good enough.

Any ideas how we could get hold of the code that is generated dynamically?

If your code can be made to run on the .NET Framework, you can save the generated assembly to disk (using a PersistentProxyBuilder passed to the ProxyGenerator ctor, and proxyGenerator.ProxyBuilder.ModuleScope.SaveAssembly) then inspect it with tools such as ILDasm or ILSpy.

If your code only runs on .NET Core (5+) then that won't work. Perhaps you could get at the generated code using the unmanaged profiling API somehow. I don't know much about that unfortunately.

SwissDanillo commented 2 weeks ago

Thank your for the feedback. We will let you know when we have repro code. Since it is somehow dependent on the load/multithreading (as we suspect) it's hard to catch.

stakx commented 2 weeks ago

Any other idea or hint what could go wrong?

One more thought: make sure that there aren't two types with the same name but from different assemblies involved (one being used in the generic type constraint definition, the other being used as the generic type argument).

SwissDanillo commented 1 week ago

Thank you for the idea @stakx. I had a look if we have duplicate names over multiple assemblies, but this doesn't seem to be the case.