Skip to content

behavior change with Socket sending reset #29980

Description

@wfurt

This is outcome of dotnet/corefx#37486. Even if that aimed to fix Synchronous blocking calls, it also impacts Async operations:

let say there is simple receiver app:

using System;
using System.Net;
using System.Net.Sockets;

namespace Receiver
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket l = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            l.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
            l.Listen(1);

            Socket s = l.Accept();
            Console.WriteLine("got socket!");

            byte[] buffer = new byte[1000];
            int rlen = 0;
            do {
                rlen = s.Receive(buffer, SocketFlags.None);
            }
            while (rlen > 0);

            Console.WriteLine("All done");
        }
    }

and then sender using Async IO.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;


namespace dotnet_latest
{
    class Program
    {
        static async Task reader(Socket s)
        {
            byte[] buffer = new byte[1000];
            int readLength=0;
            do
            {
                readLength = await s.ReceiveAsync(buffer, SocketFlags.None);
                Console.WriteLine("Got {0} bytes", readLength);
            }
            while (readLength > 0);
            Console.WriteLine("Reader all done");
        }

        static void Main(string[] args)
        {
            Socket s= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
            Task.Run(() => reader(s));

            Console.WriteLine("Hello World!");
            Console.ReadLine();
            s.Close();
        }
    }
}

before the change the receiver would simply finish after hitting enter on sending side.

with the latest socket call it fails:

Unhandled exception. System.Net.Sockets.SocketException (104): Connection reset by peer
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, SocketFlags socketFlags)
   at Receiver.Program.Main(String[] args) in /home/furt/abcd/Program.cs:line 21

It seems like we send reset when we should not.

image

cc: @tmds @davidsh @stephentoub

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions