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

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

Open
SwissDanillo opened this issue Aug 27, 2024 · 7 comments

Comments

@SwissDanillo
Copy link

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:

  • Ninject.Extensions.Factory (using Castle.Core)

Questions:

  • Any ideas how we could get hold of the code that is generated dynamically?
  • Any other idea or hint what could go wrong?
  • Has anybody else also experienced this kind of behavior?

Looking forward to bring some light into this issue.

@stakx
Copy link
Member

stakx commented Aug 27, 2024

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
Copy link
Author

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
Copy link
Member

stakx commented Aug 30, 2024

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
Copy link
Author

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.

@OliverMacko
Copy link

Although we were still unable to reproduce this issue in an example code, I would like to add more details here:

The exception mentions generic type parameter that doesn't exist in a contract of a factory interface, for example:
exception - "System.Security.VerificationException: Method Castle.Proxies.IOperationFactoryProxy.Create: type argument 'xyz.Calculate.CalculateParameters' violates the constraint of type parameter 'TEntity'."
Create method of IOperationFactory interface:
IOperation<TParams, TResult> Create<TParams, TResult>() where TParams : IOperationParameters;
so there is no TEntity present anywhere in the calling factory method. and CalculateParameters is IOperationParameters

similar example is:
System.Security.VerificationException: Method Castle.Proxies.IReadonlyRepositoryFactoryProxy.CreateReadonlyRepository: type argument 'xyz.IResultDataRepository' violates the constraint of type parameter 'TParams'.
CreateReadonlyRepository method of IReadonlyRepositoryFactory interface:
TReadonlyRepository CreateReadonlyRepository<TReadonlyRepository>() where TReadonlyRepository : ISpecificReadonlyRepository; - again no TParams is present.

We are using Castle.Core 4.4.1.

@stakx
Copy link
Member

stakx commented Oct 3, 2024

@OliverMacko, does your code involve anything else involving a generic type parameter called TEntity? And if so, are you proxying those types (or types derived from them) and/or is there any direct or indirect relation to the types you are proxying?

@OliverMacko
Copy link

yes there are proxied factories that use TEntity type parameter in some of their methods but there is no direct relation between them and this IOperationFactory (first example). They do exist in the same assembly (project), but they are not part of the same type hierarchy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants