valenok/mongoose — pull request #44 by mitchh (2012-10-16 06:25:32+00:00)
Title: Fix 30s hang->timeout when closing a socket with no more data

375950f6 attempts to drain any data left in a socket being closed; it introduced close_socket_gracefully() and added wait_until_socket_is_readable() in pull(). The former sets the socket to nonblocking to avoid hanging around during the pull() call, but this does not affect the select() call in wait_until_socket_is_readable(). If there is still data to drain, everything completes promptly, but if there is nothing to read, select() waits 30s for data that will never arrive.

This change adds an is_closing flag to mg_connection so the wait_until_socket_is_readable() call can be skipped. I did it there because I didn't want to modify the API to pull() for this special case, and conn is always destroyed after close_connection() completes. I had an earlier version that actually checked (via fcntl()) the nonblocking state, but I didn't know how to do that in Windows.