arduinoWebSockets icon indicating copy to clipboard operation
arduinoWebSockets copied to clipboard

setNoDelay on ESP32 causing error [E][WiFiClient.cpp:309] setOption(): fail on fd -1, errno: 9, "Bad file number"

Open sivar2311 opened this issue 3 years ago • 4 comments

Hi Markus,

on ESP32 it seems that setNoDelay(true) does not work and causing the "Bad file number" error when WebSocketsClient connects to a server.

setNoDelay(true) is called in connectedCb in WebSocketsClient.cpp: https://github.com/Links2004/arduinoWebSockets/blob/a14b6b73b4f05e189ca49d6e84202c2b55db528a/src/WebSocketsClient.cpp#L865-L866

I tried to follow the path to see where the error comes from, but I ended up in lwip_setsockopt_r in lwip.h

At this point my knowledge left me :(

I think the ESP32 should be removed from from the #if macro - what do you think?

sivar2311 avatar Mar 09 '21 04:03 sivar2311

I'm experiencing exactly the same issue. If I fail to connect a few times after getting this message I then get an out of memory exception. It's my understanding that “Bad File Number” error (Errno: 9) in a multithreaded application error, an “Opened once but closed twice” bug. So when thread “Y” carries out its second close(), it actually closes the file descriptor of thread “X”, which was valid & in use. I'm not sure if this is what is causing my our of memory exception, but since setNoDelay(true) disables the Nagle algorithm, that intends to reduce TCP/IP traffic of small packets sent over the network by combining a number of small outgoing messages, and sending them all at once, I think Boris' suggestion is a good one.

genemyers avatar Mar 25 '21 14:03 genemyers

if the setNoDelay is problematic on the ESP32 in some use cases we can remove it via the macro.

Links2004 avatar Mar 25 '21 15:03 Links2004

I'm facing the same issue. How would you remove it via the macro?

Lackmann1994 avatar May 24 '22 12:05 Lackmann1994

Hi Guys, I am facing the same issue was it fixed? If yes what was the fix. I did remove the setnodelay from the macro but no effect on error.

noorhaq avatar Sep 12 '22 09:09 noorhaq