From b3320fc7c387c0105aeb13911288ec33e9f9470e Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 4 Mar 2016 14:52:06 -0800 Subject: [PATCH 1/2] Fix race in poll() based pollset It was possible for entries in watchers[] to be deleted by another thread before we called begin_poll. I'd like to do something nicer than this eventually... but that'll be easier once we've got the polling loop cleaned up (which is currently WIP) --- src/core/iomgr/pollset_multipoller_with_poll_posix.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/iomgr/pollset_multipoller_with_poll_posix.c b/src/core/iomgr/pollset_multipoller_with_poll_posix.c index 4dddfff2302..0a447ba96c1 100644 --- a/src/core/iomgr/pollset_multipoller_with_poll_posix.c +++ b/src/core/iomgr/pollset_multipoller_with_poll_posix.c @@ -122,6 +122,7 @@ static void multipoll_with_poll_pollset_maybe_work_and_unlock( } else { h->fds[fd_count++] = h->fds[i]; watchers[pfd_count].fd = h->fds[i]; + GRPC_FD_REF(watchers[pfd_count].fd, "multipoller_start"); pfds[pfd_count].fd = h->fds[i]->fd; pfds[pfd_count].revents = 0; pfd_count++; @@ -135,8 +136,10 @@ static void multipoll_with_poll_pollset_maybe_work_and_unlock( gpr_mu_unlock(&pollset->mu); for (i = 2; i < pfd_count; i++) { - pfds[i].events = (short)grpc_fd_begin_poll(watchers[i].fd, pollset, worker, + grpc_fd *fd = watchers[i].fd; + pfds[i].events = (short)grpc_fd_begin_poll(fd, pollset, worker, POLLIN, POLLOUT, &watchers[i]); + GRPC_FD_UNREF(fd, "multipoller_start"); } /* TODO(vpai): Consider first doing a 0 timeout poll here to avoid From f1b6d61965adf0cf264c826b0a08b5a8b32e57c3 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 4 Mar 2016 15:00:02 -0800 Subject: [PATCH 2/2] Fix formatting --- src/core/iomgr/pollset_multipoller_with_poll_posix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/iomgr/pollset_multipoller_with_poll_posix.c b/src/core/iomgr/pollset_multipoller_with_poll_posix.c index 0a447ba96c1..92d6fb72414 100644 --- a/src/core/iomgr/pollset_multipoller_with_poll_posix.c +++ b/src/core/iomgr/pollset_multipoller_with_poll_posix.c @@ -137,8 +137,8 @@ static void multipoll_with_poll_pollset_maybe_work_and_unlock( for (i = 2; i < pfd_count; i++) { grpc_fd *fd = watchers[i].fd; - pfds[i].events = (short)grpc_fd_begin_poll(fd, pollset, worker, - POLLIN, POLLOUT, &watchers[i]); + pfds[i].events = (short)grpc_fd_begin_poll(fd, pollset, worker, POLLIN, + POLLOUT, &watchers[i]); GRPC_FD_UNREF(fd, "multipoller_start"); }