libimobiledevice / libimobiledevice-glue

A library with common code used by libraries and tools around the libimobiledevice project
GNU Lesser General Public License v2.1
86 stars 69 forks source link

socket.c socket_receive_timeout() can hang on Windows #41

Closed sctol closed 2 months ago

sctol commented 4 months ago

Running into a hang with libimobiledevice on Windows tracked down to socket_receive_timeout() of socket.c, line 1296.

recv() is returning -1 on a socket with errno == 0 when the remote peer disconnected while there are still bytes to read (res > 0)

Line 1296 reads "return -errno;;"

Suggest replacing this with "return (errno == 0) ? -ECONNRESET : -errno;"

nikias commented 4 months ago

I think the real way to handle this is using WSAGetLastError(). In regards to https://github.com/libimobiledevice/libimobiledevice-glue/issues/14 I am working on some modifications, specifically making sure that errno is set to a value based on whatever WSAGetLastError() returned.

sctol commented 4 months ago

Thank you for taking a look at this. I forgot to mention the way I was getting the libimobiledevice to hang was to start idevicebackup2 backup on a paired iPhone, then disconnect the USB cable about half way through the file sends. Maybe 5-10% of the time the idevicebackup2 would lockup with the CPU 100% busy reading nothing.

sctol commented 4 months ago

I did a bit of testing this evening, adding a statement to socket_recieve_timeout() that prints WSAGetLastError(), errno, and the results from socket_check_fd() and recv(). When the disconnect occurs, socket_check_fd() returned 1, recv() returned -1 and WSAGetLastError is 10053 (connection aborted). Errno has one of the following values: 0, 2, 7, 17 with 17 being the most common. If only WSA were portable to the BSD socket standard, including errno.

nikias commented 4 months ago

That helps. As mentioned I am in the process of integrating WSAGetLastError into the code to make sure we have proper errno set. You cannot trust errno in regards to any socket related error/status.

nikias commented 4 months ago

Came up with this fc10c88395f3fefd3b30f3ef9354c45cef7136a6.

nikias commented 2 months ago

This should be resolved by now. If not feel free to re-open.