Returning the buffer on a failed read results in errors
I have written a simple server based on this echo server and observed the following behavior:
when the connection is closed, the read operation gets 0 bytes read and the buffer id 0, and then "returns" it to the available buffers using a "provide" call.
In reality no buffer was given to that read and it is returning buffer 0 without ever getting it, which results in any operation that uses buffer 0 to have unexpected results due to multiple operations sharing the buffer.
Removing line 153 fixed this.
Might be important: my version uses UNIX sockets and not the INET ones used here.
I was able to reproduce this with INET sockets as well using these few changes: set MAX_CONNECTIONS to 2(to make reproduction faster) add a 5 seconds sleep after a read has been received and before it adds the write request. Run the server Open and close an nc clientwith the server a few times to wrongfully add bid 0 as free multiple times. Open 2 nc clients A,B Enter 1 into A and 2 into B a few times. A will receive 2 after a couple attempts(it appears to happen consistently after the 2nd attempt, which matches with the buffer IDs cycling through the real 0 and 1 buffers before reaching the repeatedly added 0 buffers).
Im certain there is a more automated way and without the sleep to reproduce this, regardless, removing the add_provide_buf line fixes this.