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

DrSegatron
Posts: 22
Joined: Tue Oct 10, 2017 2:46 pm

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

Postby DrSegatron » Sat Nov 03, 2018 3:18 am

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?
Last edited by DrSegatron on Sat Nov 03, 2018 4:41 pm, edited 1 time in total.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

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

Postby WiFive » Sat Nov 03, 2018 7:59 am

Your code should recognize zero length read as a graceful disconnect.

DrSegatron
Posts: 22
Joined: Tue Oct 10, 2017 2:46 pm

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

Postby DrSegatron » Sat Nov 03, 2018 11:27 am

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?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

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

Postby WiFive » Sat Nov 03, 2018 1:09 pm

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?"

Who is online

Users browsing this forum: No registered users and 37 guests