Page 1 of 1

select() seems to signal data to read from a shut down socket [solved]

Posted: Sat Nov 03, 2018 3:18 am
by DrSegatron
I use select() to catch read and exception events:

Code: Select all

returncount=select(max_socket,&read,NULL,&exception,NULL);
This has worked fine before, recv()ing at read events and disconnecting at exception events. But now after a few months I need to build on this code for another project I have some problems.

Connections to server socket and reads from connection sockets work fine, but If I do a graceful disconnect (FIN), instead of immediately getting an exception event as before, it instead sets a read event, which fails as there is no data to recv():

Code: Select all

V (268658) wifi.c: tcp_server_task(): 2 socket(s)
V (269268) wifi.c: tcp_server_task(): select (1 sockets active)
W (269268) wifi.c: tcp_server_task(): Recv returned 0! ESP_OK
This flag is never cleared and the program is in an infinite select()-recv()-select()-recv() loop for quite some while until something times out and the exception event is flagged.

Has an update broken something or is my program flawed in some way?

Re: select() seems to signal data to read from a shut down socket

Posted: Sat Nov 03, 2018 7:59 am
by WiFive
Your code should recognize zero length read as a graceful disconnect.

Re: select() seems to signal data to read from a shut down socket

Posted: Sat Nov 03, 2018 11:27 am
by DrSegatron
I do that as a "workaround" now, are you saying this is fine and by design? Why this change, it certainly gave me exceptions before.

But if it's the intended behaviour, I'm a bit more at ease :)

Edit: Perhaps there's a difference between graceful shutdown and a connection reset?

Re: select() seems to signal data to read from a shut down socket

Posted: Sat Nov 03, 2018 1:09 pm
by WiFive
Yes graceful shutdown can allow you to finish writing before you close. Basically other side is politely saying "I'm done writing, are you done writing?"