-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fix UDP memory leak #5404
Fix UDP memory leak #5404
Changes from 9 commits
27ccaed
c459e51
9c72778
b99cca2
74862f7
bb2ec2d
8768a78
5a4251e
80c8d00
94ef7a7
541b776
838faa0
926b220
5faf777
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,10 @@ | |
using System.Linq; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using Akka.Actor; | ||
using Akka.IO; | ||
using Akka.IO.Buffers; | ||
using Akka.TestKit; | ||
using Akka.Util.Internal; | ||
using Xunit; | ||
|
@@ -31,8 +33,15 @@ public UdpIntegrationSpec(ITestOutputHelper output) | |
akka.actor.serialize-messages = on | ||
akka.io.udp.max-channels = unlimited | ||
akka.io.udp.nr-of-selectors = 1 | ||
akka.io.udp.direct-buffer-pool-limit = 100 | ||
akka.io.udp.direct-buffer-size = 1024", output) | ||
|
||
akka.io.udp.buffer-pool = ""akka.io.udp.direct-buffer-pool"" | ||
akka.io.udp.nr-of-selectors = 1 | ||
# This comes out to be about 1.6 Mib maximum total buffer size | ||
akka.io.udp.direct-buffer-pool.buffer-size = 512 | ||
akka.io.udp.direct-buffer-pool.buffers-per-segment = 32 | ||
akka.io.udp.direct-buffer-pool.buffer-pool-limit = 100 | ||
# akka.io.udp.trace-logging = true | ||
akka.loglevel = DEBUG", output) | ||
{ | ||
_addresses = TestUtils.TemporaryServerAddresses(6, udp: true).ToArray(); | ||
} | ||
|
@@ -112,27 +121,28 @@ public void The_UDP_Fire_and_Forget_implementation_must_be_able_to_send_several_ | |
{ | ||
var serverAddress = _addresses[0]; | ||
var clientAddress = _addresses[1]; | ||
var server = BindUdp(serverAddress, TestActor); | ||
var client = BindUdp(clientAddress, TestActor); | ||
var data = ByteString.FromString("Fly little packet!"); | ||
var serverProbe = CreateTestProbe(); | ||
var clientProbe = CreateTestProbe(); | ||
var server = BindUdp(serverAddress, serverProbe); | ||
var client = BindUdp(clientAddress, clientProbe); | ||
|
||
void CheckSendingToClient(int iteration) | ||
{ | ||
server.Tell(Udp.Send.Create(data, clientAddress)); | ||
ExpectMsg<Udp.Received>(x => | ||
server.Tell(Udp.Send.Create(ByteString.FromString(iteration.ToString()), clientAddress)); | ||
clientProbe.ExpectMsg<Udp.Received>(x => | ||
{ | ||
x.Data.ShouldBe(data); | ||
x.Sender.Is(serverAddress).ShouldBeTrue($"{x.Sender} was expected to be {serverAddress}"); | ||
x.Data.ToString().ShouldBe(iteration.ToString()); | ||
x.Sender.Is(serverAddress).ShouldBeTrue($"Client sender {x.Sender} was expected to be {serverAddress}"); | ||
}, hint: $"sending to client failed in {iteration} iteration"); | ||
} | ||
|
||
void CheckSendingToServer(int iteration) | ||
{ | ||
client.Tell(Udp.Send.Create(data, serverAddress)); | ||
ExpectMsg<Udp.Received>(x => | ||
client.Tell(Udp.Send.Create(ByteString.FromString(iteration.ToString()), serverAddress)); | ||
serverProbe.ExpectMsg<Udp.Received>(x => | ||
{ | ||
x.Data.ShouldBe(data); | ||
Assert.True(x.Sender.Is(clientAddress)); | ||
x.Data.ToString().ShouldBe(iteration.ToString()); | ||
x.Sender.Is(clientAddress).ShouldBeTrue($"Server sender {x.Sender} was expected to be {clientAddress}"); | ||
}, hint: $"sending to client failed in {iteration} iteration"); | ||
} | ||
|
||
|
@@ -194,6 +204,97 @@ void CheckSendingToServer(ByteString expected) | |
for (int i = 0; i < iterations; i++) CheckSendingToClient(data[i]); | ||
} | ||
|
||
[Fact] | ||
public void The_UDP_Fire_and_Forget_implementation_must_not_leak_memory() | ||
{ | ||
const int batchCount = 2000; | ||
const int batchSize = 100; | ||
|
||
var serverAddress = _addresses[0]; | ||
var clientAddress = _addresses[1]; | ||
|
||
var udp = Udp.Instance.Apply(Sys); | ||
var poolInfo = udp.SocketEventArgsPool.BufferPoolInfo; | ||
poolInfo.Type.Should().Be(typeof(DirectBufferPool)); | ||
poolInfo.Free.Should().Be(poolInfo.TotalSize); | ||
poolInfo.Used.Should().Be(0); | ||
|
||
var serverProbe = CreateTestProbe(); | ||
var server = BindUdp(serverAddress, serverProbe); | ||
var clientProbe = CreateTestProbe(); | ||
var client = BindUdp(clientAddress, clientProbe); | ||
|
||
var data = ByteString.FromString("Fly little packet!"); | ||
|
||
// send a lot of packets through, the byte buffer pool should not leak anything | ||
for (var n = 0; n < batchCount; ++n) | ||
{ | ||
for (var i = 0; i < batchSize; i++) | ||
server.Tell(Udp.Send.Create(data, clientAddress)); | ||
|
||
var msgs = clientProbe.ReceiveN(batchSize); | ||
var receives = msgs.Cast<Udp.Received>(); | ||
receives.Sum(r => r.Data.Count).Should().Be(data.Count * batchSize); | ||
} | ||
|
||
// stop all connections so all receives are stopped and all pending SocketAsyncEventArgs are collected | ||
server.Tell(Udp.Unbind.Instance, serverProbe); | ||
serverProbe.ExpectMsg<Udp.Unbound>(); | ||
client.Tell(Udp.Unbind.Instance, clientProbe); | ||
clientProbe.ExpectMsg<Udp.Unbound>(); | ||
|
||
// wait for all SocketAsyncEventArgs to be released | ||
Thread.Sleep(1000); | ||
|
||
poolInfo = udp.SocketEventArgsPool.BufferPoolInfo; | ||
poolInfo.Type.Should().Be(typeof(DirectBufferPool)); | ||
poolInfo.Free.Should().Be(poolInfo.TotalSize); | ||
poolInfo.Used.Should().Be(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
} | ||
|
||
[Fact] | ||
public void The_UDP_Fire_and_Forget_SimpleSender_implementation_must_not_leak_memory() | ||
{ | ||
const int batchCount = 2000; | ||
const int batchSize = 100; | ||
|
||
var udp = Udp.Instance.Apply(Sys); | ||
var poolInfo = udp.SocketEventArgsPool.BufferPoolInfo; | ||
poolInfo.Type.Should().Be(typeof(DirectBufferPool)); | ||
poolInfo.Free.Should().Be(poolInfo.TotalSize); | ||
poolInfo.Used.Should().Be(0); | ||
|
||
var serverAddress = _addresses[0]; | ||
var serverProbe = CreateTestProbe(); | ||
var server = BindUdp(serverAddress, serverProbe); | ||
var sender = SimpleSender(); | ||
|
||
var data = ByteString.FromString("Fly little packet!"); | ||
|
||
// send a lot of packets through, the byte buffer pool should not leak anything | ||
for (var n = 0; n < batchCount; ++n) | ||
{ | ||
for (int i = 0; i < batchSize; i++) | ||
sender.Tell(Udp.Send.Create(data, serverAddress)); | ||
|
||
var msgs = serverProbe.ReceiveN(batchSize); | ||
var receives = msgs.Cast<Udp.Received>(); | ||
receives.Sum(r => r.Data.Count).Should().Be(data.Count * batchSize); | ||
} | ||
|
||
// stop all connections so all receives are stopped and all pending SocketAsyncEventArgs are collected | ||
server.Tell(Udp.Unbind.Instance, serverProbe); | ||
serverProbe.ExpectMsg<Udp.Unbound>(); | ||
|
||
// wait for all SocketAsyncEventArgs to be released | ||
Thread.Sleep(1000); | ||
|
||
poolInfo = udp.SocketEventArgsPool.BufferPoolInfo; | ||
poolInfo.Type.Should().Be(typeof(DirectBufferPool)); | ||
poolInfo.Free.Should().Be(poolInfo.TotalSize); | ||
poolInfo.Used.Should().Be(0); | ||
} | ||
|
||
[Fact] | ||
public void The_UDP_Fire_and_Forget_implementation_must_call_SocketOption_beforeBind_method_before_bind() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,22 @@ protected BufferPoolAllocationException(SerializationInfo info, StreamingContext | |
} | ||
} | ||
|
||
public class BufferPoolInfo | ||
Aaronontheweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public BufferPoolInfo(Type type, long totalSize, long free, long used) | ||
{ | ||
Type = type; | ||
TotalSize = totalSize; | ||
Free = free; | ||
Used = used; | ||
} | ||
|
||
public Type Type { get; } | ||
public long TotalSize { get; } | ||
public long Free { get; } | ||
public long Used { get; } | ||
} | ||
|
||
/// <summary> | ||
/// An interface used to acquire/release recyclable chunks of | ||
/// bytes to be reused without need to triggering GC. | ||
|
@@ -70,6 +86,8 @@ public interface IBufferPool | |
/// </summary> | ||
/// <param name="buf"></param> | ||
void Release(IEnumerable<ByteBuffer> buf); | ||
|
||
BufferPoolInfo Diagnostics(); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -131,6 +149,17 @@ public DirectBufferPool(int bufferSize, int buffersPerSegment, int initialSegmen | |
} | ||
} | ||
|
||
public BufferPoolInfo Diagnostics() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea |
||
=> new BufferPoolInfo( | ||
type: typeof(DirectBufferPool), | ||
totalSize: _segments.Count * _segmentSize, | ||
free: _buffers.Count * _bufferSize, | ||
used: (_segments.Count * _segmentSize) - (_buffers.Count * _bufferSize)); | ||
|
||
public override string ToString() | ||
=> $"_bufferSize: [{_buffers}], _bufferPerSegment: [{_buffersPerSegment}], _segmentSize: [{_segmentSize}], " + | ||
$"_segments.Count: [{_segments.Count}], _buffers.Count: [{_buffers.Count}]"; | ||
|
||
private void AllocateSegment() | ||
{ | ||
lock (_syncRoot) | ||
|
@@ -183,7 +212,7 @@ public IEnumerable<ByteBuffer> Rent(int minimumSize) | |
AllocateSegment(); | ||
} | ||
|
||
throw new BufferPoolAllocationException($"Couldn't allocate enough byte buffer to fill the tolal requested size of {minimumSize} bytes"); | ||
throw new BufferPoolAllocationException($"Couldn't allocate enough byte buffer to fill the total requested size of {minimumSize} bytes"); | ||
} | ||
catch | ||
{ | ||
|
@@ -195,8 +224,10 @@ public IEnumerable<ByteBuffer> Rent(int minimumSize) | |
public void Release(ByteBuffer buf) | ||
{ | ||
// only release buffers that have actually been taken from one of the segments | ||
if (buf.Count == _bufferSize && _segments.Contains(buf.Array)) | ||
_buffers.Push(buf); | ||
if (buf.Count != _bufferSize || !_segments.Contains(buf.Array)) | ||
throw new Exception("Wrong ArraySegment<byte> was released to DirectBufferPool"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sanity check to make sure that any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would it only happen during development? |
||
|
||
_buffers.Push(buf); | ||
} | ||
|
||
public void Release(IEnumerable<ByteBuffer> buffers) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM