Skip to content

Memory leak in UdpClient on Unix #111400

Open
@stalexig

Description

@stalexig

Description

I'm using UdpClient to read data asynchronously over Udp. To cancel the operation after a timeout, I use CancellationTokenSource. I noticed that with a slow device, the timeout often triggers and the memory begins to grow. DotMemory shows a constant increase in the number of these objects:

Image

Reproduction Steps

The problem only exists on Unix systems, there is no leak on Windows.

Here is test code that reproduces the described problem:

using var udpClient = new UdpClient();
udpClient.Connect(IPAddress.Parse(addr), port);

while (true)
{
    try
    {
        using var tokenSource = new CancellationTokenSource(100);
        var result = await udpClient.ReceiveAsync(tokenSource.Token);
    }
    catch (OperationCanceledException)
    {
    }
    
    await Task.Delay(100).ConfigureAwait(false);
}

Expected behavior

All resources should be released after ReceiveAsync exits with a cancellation token

Actual behavior

There is a memory leak on Unix

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions