NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

Creating an endpoint and not referencing the instance results in memory/resource leak

Open ramonsmits opened this issue 1 year ago • 1 comments

Describe the bug

Description

Creating an endpoint and not referencing the instance results in memory leak.

Expected behavior

The instance to eventually be able to be garbage collected when there is no longer a reference to the instance.

Actual behavior

When an instance is started but no longer referenced it remains active and in memory for the duration of the application.

Versions

9.0.x

Please list the version of the relevant packages or applications in which the bug exists.

Steps to reproduce

  1. Start an instance in separate method returning nothing, ensure its not inlined
  2. Create an infinite loop that triggers the GC to collect and wait for finalizers with a sleep/delay
  3. Observe that the instance remains running

Relevant log output

No response

Additional Information

No response

ramonsmits avatar Jun 13 '24 15:06 ramonsmits

This happens for example currently indirectly in ServiceControl indirectly at:

  • https://github.com/Particular/ServiceControl/blob/5.2.4/src/ServiceControl.Audit/Auditing/AuditIngestion.cs

  • https://github.com/Particular/ServiceControl/blob/5.2.4/src/ServiceControl/Operations/ErrorIngestion.cs#L145-L151

ramonsmits avatar Jun 13 '24 15:06 ramonsmits