How to handle half-open TCP connection?
Posted: Fri Jun 08, 2018 12:40 pm
I have a TCP connection that works great as long as the other side works properly, but if the connection is cut for example because of a change of WiFi network on the other side the connection is stuck in half-open mode.
I regularly try to send data on this socket, so the connection loss should be detected immediately and give an exception in by select(), but when the connection is broken, lwIP is happy to keep send()ing data into the void until I get some random strange errors ( Connection already in progress (119) and No more processes (11)) instead of something like (Software caused connection abort (-1) or Connection reset by peer (104)).
I tried setsockopt()ing SO_SNDTIMEO, but I'm not sure this does anything. I saw there is some callback for timeouts, not sure if that's something to do with this, it seems to be part of the raw API.
Edit: On a related note, I have select() in one thread and send() in another thread. This has been working fine until I discovered the half-open problem, but now I read that lwIP send() and other functions are not thread safe? Do I need to handle my (asynchronous) send()s in my select() somwhow?
I regularly try to send data on this socket, so the connection loss should be detected immediately and give an exception in by select(), but when the connection is broken, lwIP is happy to keep send()ing data into the void until I get some random strange errors ( Connection already in progress (119) and No more processes (11)) instead of something like (Software caused connection abort (-1) or Connection reset by peer (104)).
I tried setsockopt()ing SO_SNDTIMEO, but I'm not sure this does anything. I saw there is some callback for timeouts, not sure if that's something to do with this, it seems to be part of the raw API.
Edit: On a related note, I have select() in one thread and send() in another thread. This has been working fine until I discovered the half-open problem, but now I read that lwIP send() and other functions are not thread safe? Do I need to handle my (asynchronous) send()s in my select() somwhow?