Skip to content

Commit

Permalink
In upload-pack, wait until the client receives all the data and disco…
Browse files Browse the repository at this point in the history
…nnects

This fixes the issue identified in

	git-for-windows#304

On Windows, closing a socket destroys its OS-allocated buffer even when
the latter still contains the last chunk of the data to be sent. Hence,
closing the socket immediately after sending the pack results in a race
condition between sending that last chunk and destroying the OS socket
object.

When the OS socket object is destroyed first, git-daemon fails to
actually send the last chunk of the pack to the client, and the latter
fails with the `early EOF` / `index-pack failed`. That is, the above
race condition results in intermittent failures to clone a repository
hosted on a Windows machine by git-daemon.

To eliminate the race condition, `upload-pack` now sends all data and
then waits until the client closes connection from its side.

Signed-off-by: Oleg Gubanov <[email protected]>
  • Loading branch information
guban committed Feb 28, 2018
1 parent 4435488 commit 635bf1b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions upload-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,23 @@ static int upload_pack_config(const char *var, const char *value, void *unused)
return parse_hide_refs_config(var, value, "uploadpack");
}

/* On Windows, closing a socket destroys its OS-allocated buffer
* even when the latter still contains the last chunk of the data to be sent.
* Therefore, we cannot close the socket immediately
* after sending the pack via the TCP socket associated with stdout.
* To ensure that all data is received by the client,
* we wait until the client disconnects.
*/
static void wait_for_client_disconnect()
{
int timeout_seconds = 30;

struct pollfd pfd;
pfd.fd = 1;
pfd.events = POLLIN;
poll(&pfd, 1, timeout_seconds * 1000);
}

int cmd_main(int argc, const char **argv)
{
const char *dir;
Expand Down Expand Up @@ -1080,6 +1097,7 @@ int cmd_main(int argc, const char **argv)
/* fallthrough */
case protocol_v0:
upload_pack();
wait_for_client_disconnect();
break;
case protocol_unknown_version:
BUG("unknown protocol version");
Expand Down

0 comments on commit 635bf1b

Please sign in to comment.