From f16fcaacb4576dff6b192ca9a12269b91ed973ff Mon Sep 17 00:00:00 2001 From: Michal Ostrowski Date: Mon, 7 Oct 2019 12:16:36 -0500 Subject: [PATCH] Issue 19208: Avoid double close of file-descriptors (poll) After closing a file descriptor honor the "closed" flag to avoid re-closing it in post-fork processing. File descriptors must be closed during an orphan operation, because the closing of the file-descriptor is necessary for the correct function of code that is polling on the descriptor. Thus even if there are active references, the close() call is necessary. But this means that post-fork code may close the file-descriptor, since it is only unregistered from post-fork after the ref-count reaches 0. All of this can be handled by ensuring that the post-fork code honors the "close" flag. --- src/core/lib/iomgr/ev_poll_posix.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/lib/iomgr/ev_poll_posix.cc b/src/core/lib/iomgr/ev_poll_posix.cc index cee6dcbe530..316d137d596 100644 --- a/src/core/lib/iomgr/ev_poll_posix.cc +++ b/src/core/lib/iomgr/ev_poll_posix.cc @@ -1379,7 +1379,9 @@ static void reset_event_manager_on_fork() { gpr_mu_lock(&fork_fd_list_mu); while (fork_fd_list_head != nullptr) { if (fork_fd_list_head->fd != nullptr) { - close(fork_fd_list_head->fd->fd); + if (!fork_fd_list_head->fd->closed) { + close(fork_fd_list_head->fd->fd); + } fork_fd_list_head->fd->fd = -1; } else { close(fork_fd_list_head->cached_wakeup_fd->fd.read_fd);