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

ProxyGenerator leaking #675

Open
kdma opened this issue Dec 28, 2023 · 2 comments
Open

ProxyGenerator leaking #675

kdma opened this issue Dec 28, 2023 · 2 comments

Comments

@kdma
Copy link

kdma commented Dec 28, 2023

Hi we have a Service Fabic cluster and we use Castle.Core.AsyncInterceptor (2.0.0 which has dependency towards Castle.Core 4.4.0) for telemetry injection, we noticed DynamicProxyGenAssembly2 modules leaking.

This is the setup for classes and DI:

public class ServiceInstanceGenerator
   {
       private readonly LoggingInterceptor _loggingInterceptor;
       private readonly ProxyGenerator _proxyGenerator;

       public ServiceInstanceGenerator(LoggingInterceptor loggingInterceptor, ProxyGenerator proxyGenerator)
       {
           _loggingInterceptor = loggingInterceptor;
           _proxyGenerator = proxyGenerator;
       }

       public TInterface GetActor<TInterface>(ActorId id) where TInterface : class, IActor
       {
           var service = ActorProxy.Create<TInterface>(id);
           return _proxyGenerator.CreateInterfaceProxyWithTargetInterface(service, _loggingInterceptor);
       }
       public TInterface GetService<TInterface>(Uri uri) where TInterface : class, IService
       {
           var service = ServiceProxy.Create<TInterface>(uri);
           return _proxyGenerator.CreateInterfaceProxyWithTargetInterface(service, _loggingInterceptor);
       }
   }

internal class ContainerModule : Module
    {

        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<ProxyGenerator>().AsSelf().SingleInstance();
            builder.RegisterInstance(new JsonSerializerOptions() { Converters = { new ByteArrayConverter() } });
            builder.RegisterType<LoggingInterceptor>().AsSelf().SingleInstance();
            builder.RegisterType<ServiceInstanceGenerator>().AsSelf().SingleInstance();
            builder.RegisterType<AssignmentsActor>().AsSelf().SingleInstance();
			builder.RegisterType<TelemetryClient>().AsSelf().SingleInstance();
            builder.Register((c, p) => c.Resolve<ServiceInstanceGenerator>().GetActor<ITransactions>(p.TypedAs<ActorId>())).As<ITransactions>();
        }
		
	}
 public class AssignmentsActor
 {
     private readonly Func<ActorId, ITransactions> _actorFactory;

     public AssignmentsActor(Func<ActorId, ITransactions> actorFactory)
     {
         _actorFactory = actorFactory;
     }

private ITransactions GetActor(ActorId id)
{
    return _actorFactory(id);
}

We tought that by registering the dependencies as single istance and the factory we could prevent the DynamicProxyGenLeaking but with this setup we can find this in the dumps:

image

Is our reasoning correct?

@stakx
Copy link
Member

stakx commented Dec 28, 2023

This appears to be more of an issue with your DI container than with DynamicProxy per se. Perhaps .SingleInstance() doesn't do what you think it does? (For example, it might create a single instance per web request or something like that.) Either way, I'm afraid you might be at the wrong repository here.

@kdma
Copy link
Author

kdma commented Dec 28, 2023

Hi stakx,
Thanks I believe its something in between Autofac and Castle and of course SF the mother of all the beasts not playing nicely with each other.
I am going to crosspost there as suggested and update here for anyone wandering in the future. :)

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

No branches or pull requests

2 participants