From 638c1ee16147f40981cdfc785d520eb162dbcbea Mon Sep 17 00:00:00 2001
From: Jan Tattermusch <jtattermusch@google.com>
Date: Tue, 21 Apr 2015 17:14:44 -0700
Subject: [PATCH] partial fix for #546

---
 src/core/iomgr/tcp_server_windows.c | 6 +++++-
 src/core/iomgr/tcp_windows.c        | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/core/iomgr/tcp_server_windows.c b/src/core/iomgr/tcp_server_windows.c
index d1b02a6935..6e10da97cc 100644
--- a/src/core/iomgr/tcp_server_windows.c
+++ b/src/core/iomgr/tcp_server_windows.c
@@ -191,6 +191,8 @@ static void start_accept(server_port *port) {
     goto failure;
   }
 
+  /* TODO(jtattermusch): probably a race here, we regularly get use-after-free on server shutdown */
+  GPR_ASSERT(port->socket != 0xfeeefeee);
   success = port->AcceptEx(port->socket->socket, sock, port->addresses, 0,
                            addrlen, addrlen, &bytes_received,
                            &port->socket->read_info.overlapped);
@@ -244,7 +246,9 @@ static void on_accept(void *arg, int success) {
   }
 
   if (ep) sp->server->cb(sp->server->cb_arg, ep);
-  start_accept(sp);
+  if (success) {
+    start_accept(sp);
+  }
 }
 
 static int add_socket_to_server(grpc_tcp_server *s, SOCKET sock,
diff --git a/src/core/iomgr/tcp_windows.c b/src/core/iomgr/tcp_windows.c
index 90fa877847..71534eaa3d 100644
--- a/src/core/iomgr/tcp_windows.c
+++ b/src/core/iomgr/tcp_windows.c
@@ -127,6 +127,7 @@ static void on_read(void *tcpp, int success) {
 
   if (socket->read_info.wsa_error != 0) {
     char *utf8_message = gpr_format_message(info->wsa_error);
+    gpr_log(GPR_ERROR, "ReadFile overlapped error: %s", utf8_message);
     gpr_free(utf8_message);
     status = GRPC_ENDPOINT_CB_ERROR;
   } else {
-- 
GitLab