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

Memory Leak while dispatching workflows #6449

Open
truthz03 opened this issue Feb 25, 2025 · 0 comments
Open

Memory Leak while dispatching workflows #6449

truthz03 opened this issue Feb 25, 2025 · 0 comments

Comments

@truthz03
Copy link

truthz03 commented Feb 25, 2025

Description

In my usecase I dispatch a very hug number of workflows and I recognized an increasing memory usage.

Steps to Reproduce

Create a workflow which will be dispatched very often (my test dispatches about 200 workflows per second)
After about 1 minute I stop dispatching and wait another minute to be sure that all queues are empty.

Expected Behavior

The memory usage should be nearly the same es at startup time.

Actual Behavior

The memory is higher than expected.
Analysing the heap memory shows that there are a hugh number of CancellationTokenSources.
Image
Image

Environment

  • Elsa Package Version: 3.3.2
  • Operating System: Windows 10

Troubleshooting Attempts

I created my own {Custom}BackgroundCommandSenderHostedService and changed the following code:
From

private async Task ReadOutputAsync(Channel<ICommand> output, CancellationToken cancellationToken)
{
	await foreach (var command in output.Reader.ReadAllAsync(cancellationToken))
	{
		try
		{
			using var scope = _scopeFactory.CreateScope();
			var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>();

			await commandSender.SendAsync(command, CommandStrategy.Default, cancellationToken);
		}
		catch (Exception e)
		{
			_logger.LogError(e, "An unhandled exception occured while processing the queue");
		}
	}
}

To

private async Task ReadOutputAsync(Channel<ICommand> output, CancellationToken cancellationToken)
{
	await foreach (var command in output.Reader.ReadAllAsync(cancellationToken))
	{
		try
		{
			using var scope = _scopeFactory.CreateScope();
			var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>();

			await commandSender.SendAsync(command, CommandStrategy.Default, CancellationToken.None);
		}
		catch (Exception e)
		{
			_logger.LogError(e, "An unhandled exception occured while processing the queue");
		}
	}
}

after that it gives this result:
Image

Any idea whats the problem here or how to fix this?
The strange thing is that there are no instances if I try to debug it.
The BackgroundCommandSenderHostedService has 4 items inside the _output variable and if I check the UnboundedChannel inside them the _items lists are empty and I'm also not able to find a big number of AsyncOperation items.
UnboundedChannel source code

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

1 participant