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 in EndPoint channels bugfix #2111

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/Proto.Remote/Endpoints/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,19 @@ private void RejectRemoteDeliver(RemoteDeliver env)

private async Task RunAsync()
{
var waiter = new MultiTaskReuseWaiter<bool>(
() => _remoteDelivers.Reader.WaitToReadAsync(CancellationToken),
() => _remotePriorityDelivers.Reader.WaitToReadAsync(CancellationToken));

while (!CancellationToken.IsCancellationRequested)
{
try
{
var messages = new List<RemoteDeliver>(RemoteConfig.EndpointWriterOptions.EndpointWriterBatchSize);

while (true)
{
var t1 = _remoteDelivers.Reader.WaitToReadAsync(CancellationToken).AsTask();
var t2 = _remotePriorityDelivers.Reader.WaitToReadAsync(CancellationToken).AsTask();
await Task.WhenAny(t1, t2);
await waiter.WaitAnyAsync();

var i = 0;
while (true)
{
Expand Down Expand Up @@ -571,4 +573,38 @@ private MessageBatch CreateBatch(IReadOnlyCollection<RemoteDeliver> m)
// Logger.LogTrace("[{SystemAddress}] Sending {Count} envelopes for {Address}", System.Address, envelopes.Count, Address);
return batch;
}

/// <summary>
/// Preserves non completed Tasks between <see cref="WaitAnyAsync"/> calls.
/// This is necessary to prevent memory leak https://github.com/asynkron/protoactor-dotnet/issues/2110
/// </summary>
class MultiTaskReuseWaiter<T>
{
private readonly Func<ValueTask<T>>[] _taskFactories;
private readonly Task<T>?[] _tasks;

public MultiTaskReuseWaiter(params Func<ValueTask<T>>[] taskFactories)
{
_taskFactories = taskFactories;
_tasks = new Task<T>?[taskFactories.Length];
}

public async ValueTask<T> WaitAnyAsync()
{
for (var i = 0; i < _taskFactories.Length; i++)
{
if (_tasks[i]?.IsCompleted == false)
continue;

var vt = _taskFactories[i].Invoke();
if (vt.IsCompleted)
return await vt;

_tasks[i] = vt.AsTask();
}

return await await Task.WhenAny(_tasks!);
}

}
}
Loading