Skip to content

Commit ad0d09c

Browse files
committed
daemon: graceful shutdown of client connection
On Windows, a connection is shutdown when the last open handle to it is closed. When that last open handle is stdout of our child process, an abortive shutdown is triggered when said process exits. Ensure a graceful shutdown of the client connection by keeping an open handle until we detect our child process has finished. This allows all the data to be sent to the client, instead of being discarded. Fixes git-for-windows#304 Signed-off-by: Kim Gybels <[email protected]>
1 parent 7e0d711 commit ad0d09c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

daemon.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,16 +834,18 @@ static struct child {
834834
struct child *next;
835835
struct child_process cld;
836836
struct sockaddr_storage address;
837+
int connection;
837838
} *firstborn;
838839

839-
static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen)
840+
static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen, int connection)
840841
{
841842
struct child *newborn, **cradle;
842843

843844
newborn = xcalloc(1, sizeof(*newborn));
844845
live_children++;
845846
memcpy(&newborn->cld, cld, sizeof(*cld));
846847
memcpy(&newborn->address, addr, addrlen);
848+
newborn->connection = connection;
847849
for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
848850
if (!addrcmp(&(*cradle)->address, &newborn->address))
849851
break;
@@ -888,6 +890,7 @@ static void check_dead_children(void)
888890
*cradle = blanket->next;
889891
live_children--;
890892
child_process_clear(&blanket->cld);
893+
close(blanket->connection);
891894
free(blanket);
892895
} else
893896
cradle = &blanket->next;
@@ -928,13 +931,13 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
928931
}
929932

930933
cld.argv = cld_argv.argv;
931-
cld.in = incoming;
934+
cld.in = dup(incoming);
932935
cld.out = dup(incoming);
933936

934937
if (start_command(&cld))
935938
logerror("unable to fork");
936939
else
937-
add_child(&cld, addr, addrlen);
940+
add_child(&cld, addr, addrlen, incoming);
938941
}
939942

940943
static void child_handler(int signo)

0 commit comments

Comments
 (0)