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

StreamReader.Read does not throw any exceptions when TCP/IP connection lost on Linux #42232

Closed
hayashida-katsutoshi opened this issue Sep 14, 2020 · 4 comments

Comments

@hayashida-katsutoshi
Copy link

StreamReader.Read does not throw any exception when TCP/IP connection lost on Ubuntu 20.04.

General

When you use System.Net.Sockets.TcpListener and StreamReader.Read, the Read method generates System.IO.IOException on Windows 10, when network disconnection occurs. The error message is "Unable to read data from the transport connection: A socket operation was attempted to an unreachable network..". I can implement some error handlers with this exception.

However no exceptions observed on Ubuntu 20.04. The version of dotnet core is 3.1.300. I'm generating the disconnection error removing a network cable physically.

var service = System.Net.Sockets.TcpListener.Create(port);
try
{
    service.Start();

    var serviceClient = service.AcceptTcpClient();
    var serviceStream = serviceClient.GetStream();
    var serviceReader = new StreamReader(serviceStream, Encoding.UTF8);

    while (!isClosed)
    {
        var g = new char[8192];
        int size = serviceReader.Read(g, 0, g.Length);
        //It does something
    }
}
catch (Exception e)
{
    Console.WriteLine($"Error! {e.Message}");
    //It recovers from the error.
}
@mairaw mairaw transferred this issue from dotnet/core Sep 14, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Sep 14, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Sep 16, 2020

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

@scalablecory
Copy link
Contributor

This is a general TCP issue -- if you have no outstanding sends for the other side to ACK, and the other side isn't sending you anything, then the socket has no way of discovering a network cable unplug.

You can set SocketOptionName.KeepAlive to true on your socket, which will cause the TCP stack to send occasional pings to check if an idle connection is still alive. By default this only happens after 2hr, so it may not react quick enough for you -- there are OS settings to adjust it to check sooner.

@hayashida-katsutoshi
Copy link
Author

Thank you everyone. I found that Read function returns 0 when disconnection happened. It is good enough for my purpose because my task is for communication between two embedded systems only, and it is ok as long as my product works. When it detect disconnection, they just start over connection. I would try SocketOptionName.KeepAlive if my workaround does not work.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
@karelz karelz added this to the 6.0.0 milestone Jan 26, 2021
@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants