GNOME Bugzilla – Bug 669810
socket/win32: flush pending read before signaling HUP
Last modified: 2012-02-10 18:08:10 UTC
Unix and Windows gio GSocket behaves differently when the socket is closed by the peer. On Unix, the client receives pending data before receiving HUP. But on Windows, the HUP may come before, resulting in unreliable and racy code. We should have same behaviour on all platforms. According to MSDN documentation: "an application should check for remaining data upon receipt of FD_CLOSE to avoid any possibility of losing data."
Created attachment 207262 [details] [review] gio/tests: add /socket/close_graceful test Add a test to check that remaining data is read before the close event is received.
Created attachment 207263 [details] [review] socket/win32: flush pending read before signaling HUP Unix and Windows gio GSocket behaves differently when the socket is closed by the peer. On Unix, the client receives pending data before receiving HUP. But on Windows, the HUP may come before, resulting in unreliable and racy code. We should have same behaviour on all platforms. According to MSDN documentation: "an application should check for remaining data upon receipt of FD_CLOSE to avoid any possibility of losing data."
Comment on attachment 207262 [details] [review] gio/tests: add /socket/close_graceful test looks good. can you just add a comment to test_close_graceful() pointing out that it's doing the same thing as what g_tcp_connection_set_graceful_disconnect() does?
Comment on attachment 207263 [details] [review] socket/win32: flush pending read before signaling HUP >+ int r, error, buffer; >+ >+ WSASetLastError (0); >+ r = recv (socket->priv->fd, &buffer, sizeof (buffer), MSG_PEEK); >+ error = WSAGetLastError (); >+ WSASetLastError (0); For consistency with the rest of gsocket.c, use "errsv" as the variable for the error code, and call get_socket_errno() rather than calling WSAGetLastError() directly. Also, you shouldn't need the calls to WSASetLastError(). Just rearrange the checks so that they only look at the error code if r < 0.
Created attachment 207269 [details] [review] gio/tests: add /socket/close_graceful test Add a test to check that remaining data is read before the close event is received.
Created attachment 207270 [details] [review] socket/win32: flush pending read before signaling HUP Unix and Windows gio GSocket behaves differently when the socket is closed by the peer. On Unix, the client receives pending data before receiving HUP. But on Windows, the HUP may come before, resulting in unreliable and racy code. We should have same behaviour on all platforms. According to MSDN documentation: "an application should check for remaining data upon receipt of FD_CLOSE to avoid any possibility of losing data."
Attachment 207262 [details] pushed as 6ca817c - gio/tests: add /socket/close_graceful test Attachment 207263 [details] pushed as 704a2ca - socket/win32: flush pending read before signaling HUP Attachment 207269 [details] pushed as 6ca817c - gio/tests: add /socket/close_graceful test Attachment 207270 [details] pushed as 704a2ca - socket/win32: flush pending read before signaling HUP