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
Socket.Poll timeout occurs unexpectedly #26209
Comments
I'm not sure I understand your use case @luigiberrettini Maybe simple sample code would help. |
Hi @wfurt here you can find references to the behavior I need clarifications about:
Basically I am trying to detect UDP and TCP (besides using KeepAlive) disconnections before continuing sending data on a Socket.
|
AFAIK, there is no reliable way how to detect "disconnect" for UDP. (as there is no connection) Your data may or may not be delivered and there is no feedback or confirmation. The only option is getting ICMP (port)unreachable. And you will not get it unless you sent data to closed port. dotnet/corefx#1 from your previous comment is really confusing. Detection if socket is readable or writable is fully independent of data sent to the socket. |
Using a heartbeat is not feasible because I am not the owner of the Syslog protocol The only way to know if a Socket is really writable is to send data on it, but to avoid the server receiving bad data I would like to know if I can send some sort of data that is usually discarded (0 bytes array or something else). I read that I know that UDP is a connectionless protocol, but in the UDP constructor A As a result I need a way to understand if the Socket can be used to send data on it both for UDP and TCP To me it is really strange that I am allowed to call Poll on a UDP socket and this does not detects disconnections as stated in the documentation: either the docs should be updated or there should be a TcpSocket class implementing Poll and a UdpSocket not implementing it. Even if only for the TCP case should I call Or should I use the approach suggested on StackOverflow? I would like to have a confirmation that by design |
no, I was not suggesting heat-beat and I'm somewhat familiar with syslog protocol. Back to the Poll, discussion: The primary function is not to detect disconnects. It can detect errors on socket - and for TCP disconnect is one of them. do "man poll" on your linux system and read the page. Since UDP does not have connection, there is no disconnect and socket will not transition to error mode unless you get ICMP back as I mentioned. (all that is handled at OS level) You can sue select() to get all the three attributes in single call. You can use 0 for timeout and the call would return immediately. But why don't you just write your data and react to errors? |
About heart-beats I only wanted to clarify that it was not feasible for me even if you did not suggest it. The poll man page does not help so much in understanding how to detect a disconnection in C# and As for Syslog RFCs I support both RFC 3164 and RFC 5424. Unfortunately section 8.5 of RFC 5424 does not explain/suggest implementation details: My code is already reacting to errors but I would like to minimize message lost: a potentially big log message can be split in mutiple Syslog messages and if only one fails it would cause the entire message to be lost. Since I am quite confused, it would be really helpful if you could give me a crystal clear answer on what I should do.
|
One of the reasons for introducing this check was that during debug sessions I noticed that, after unplugging the cable, the first Write did not fail, therefore I was losing messages. |
UDP is unreliable and you can always loose some messages. The write() will succeed when you get your data into socket buffer in kernel. You can try to write simple C program to study OS behavior. |
This seems answered + no activity for 1.5 years, closing. If there is still some question remaining, I recommend to open a new issue and clarify slearly what is the question. Thanks! |
I am trying to detect disconnection issues by using
Socket.Poll
.Microsoft Docs say that
Socket.Poll
:or that the remote host was shut down ungracefully (you must attempt to send or receive data to detect these kinds of errors)
SelectMode.SelectRead
returns:true
if Listen(Int32) has been called and a connection is pendingtrue
if data is available for readingtrue
if the connection has been closed, reset, or terminatedfalse
otherwiseIs it possible to detect disconnections with both
SelectMode.SelectWrite
and sending a 0 byte array or something similar that will be ignored by a standard Syslog server that is up and running with the client connected to it?When the server is up and the connection ok, if I use Poll with
SelectMode.SelectRead
before sending data, I experience a delay due to the timeout expiring.I do not understand if the expected behavior of Poll is:
true
quickly, i.e. no timeout expirationfalse
only after the timeout elapsesIf this is the case to check that everything is ok I should set a very low timeout.
What is a reasonable value i.e. 0 or 1 are ok?
The text was updated successfully, but these errors were encountered: