Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request grpc#2779 from jtattermusch/properly_decrement_act…
Browse files Browse the repository at this point in the history
…ive_ports

Properly decrement active_ports on server shutdown on Win.
  • Loading branch information
nicolasnoble committed Aug 4, 2015
2 parents 98dd83e + dd8a80f commit 847dfff
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/core/iomgr/tcp_server_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ static int prepare_socket(SOCKET sock, const struct sockaddr *addr,
return -1;
}

static void decrement_active_ports_and_notify(server_port *sp) {
sp->shutting_down = 0;
sp->socket->read_info.outstanding = 0;
gpr_mu_lock(&sp->server->mu);
GPR_ASSERT(sp->server->active_ports > 0);
if (0 == --sp->server->active_ports) {
gpr_cv_broadcast(&sp->server->cv);
}
gpr_mu_unlock(&sp->server->mu);
}

/* start_accept will reference that for the IOCP notification request. */
static void on_accept(void *arg, int from_iocp);

Expand Down Expand Up @@ -234,6 +245,15 @@ static void start_accept(server_port *port) {
return;

failure:
if (port->shutting_down) {
/* We are abandoning the listener port, take that into account to prevent
occasional hangs on shutdown. The hang happens when sp->shutting_down
change is not seen by on_accept and we proceed to trying new accept,
but we fail there because the listening port has been closed in the
meantime. */
decrement_active_ports_and_notify(port);
return;
}
utf8_message = gpr_format_message(WSAGetLastError());
gpr_log(GPR_ERROR, message, utf8_message);
gpr_free(utf8_message);
Expand Down Expand Up @@ -277,14 +297,7 @@ static void on_accept(void *arg, int from_iocp) {
if (sp->shutting_down) {
/* During the shutdown case, we ARE expecting an error. So that's well,
and we can wake up the shutdown thread. */
sp->shutting_down = 0;
sp->socket->read_info.outstanding = 0;
gpr_mu_lock(&sp->server->mu);
GPR_ASSERT(sp->server->active_ports > 0);
if (0 == --sp->server->active_ports) {
gpr_cv_broadcast(&sp->server->cv);
}
gpr_mu_unlock(&sp->server->mu);
decrement_active_ports_and_notify(sp);
return;
} else {
char *utf8_message = gpr_format_message(WSAGetLastError());
Expand Down

0 comments on commit 847dfff

Please sign in to comment.