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

ReceiveMessageFrom does not work correctly on Linux after altering BufferList to a single buffer #46860

Open
antonfirsov opened this issue Jan 12, 2021 · 3 comments

Comments

@antonfirsov
Copy link
Member

antonfirsov commented Jan 12, 2021

After setting / using SocketAsyncEventArgs.BufferList we should be able to revert to single-buffer mode by calling:

saea.BufferList = null;
saea.SetBuffer(...);

While working on UDP test harmonization, I discovered that this does not work with ReceiveMessageFrom on Linux. Haven't checked other Unixes.

Repro:

[Fact]
public void ReceiveMessageFromIssue()
{
    using var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    using var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    using var saea = new SocketAsyncEventArgs();
    var completed = new ManualResetEventSlim();
    saea.Completed += delegate { completed.Set(); };

    receiver.Bind(new IPEndPoint(IPAddress.Loopback, 0));
    sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
    saea.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

    // Commenting out the following line makes the issue disappear:
    saea.BufferList = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[1]) };

    saea.BufferList = null;
    saea.SetBuffer(new byte[1024], 0, 1024);

    sender.SendTo(new byte[1024], receiver.LocalEndPoint);
    if (receiver.ReceiveMessageFromAsync(saea)) 
        Assert.True(completed.Wait(1000), "Expected operation to complete within timeout");

    // Fails on Linux, works on Windows:
    Assert.Equal(1024, saea.BytesTransferred);
}
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Net.Sockets untriaged New issue has not been triaged by the area owner labels Jan 12, 2021
@ghost
Copy link

ghost commented Jan 12, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

After setting / using SocketAsyncEventArgs.BufferList we should be able to revert to single-buffer mode by calling:

saea.BufferList = null;
saea.SetBuffer(...);

While working on UDP test harmonization, I discovered that this does not work with ReceiveMessageFrom on Linux. Haven't checked with other Unixes.

Repro:

[Fact]
public void ReceiveMessageFromIssue()
{
    using var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    using var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    using var saea = new SocketAsyncEventArgs();
    var completed = new ManualResetEventSlim();
    saea.Completed += delegate { completed.Set(); };

    receiver.Bind(new IPEndPoint(IPAddress.Loopback, 0));
    sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
    saea.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

    // Commenting out the following line makes the issue disappear:
    saea.BufferList = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[1]) };

    saea.BufferList = null;
    saea.SetBuffer(new byte[1024], 0, 1024);

    sender.SendTo(new byte[1024], receiver.LocalEndPoint);
    if (receiver.ReceiveMessageFromAsync(saea)) 
        Assert.True(completed.Wait(1000), "Expected operation to complete within timeout");

    // Fails on Linux, works on Windows:
    Assert.Equal(1024, saea.BytesTransferred);
}
Author: antonfirsov
Assignees: -
Labels:

area-System.Net.Sockets, untriaged

Milestone: -

@geoffkizer
Copy link
Contributor

Good find, thanks.

@antonfirsov antonfirsov removed the untriaged New issue has not been triaged by the area owner label Feb 4, 2021
@karelz karelz added the bug label May 13, 2021
@karelz
Copy link
Member

karelz commented May 13, 2021

Triage: Corner case (unlikely for customers to hit it), fine to move to Future.

@karelz karelz modified the milestones: 6.0.0, Future May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants