diff --git a/src/ipc_listener.cpp b/src/ipc_listener.cpp index 15520e92..f76e07a5 100644 --- a/src/ipc_listener.cpp +++ b/src/ipc_listener.cpp @@ -280,6 +280,13 @@ zmq::fd_t zmq::ipc_listener_t::accept () return retired_fd; } + // Race condition can cause socket not to be closed (if fork happens + // between accept and this point). +#ifdef FD_CLOEXEC + int rc = fcntl (sock, F_SETFD, FD_CLOEXEC); + errno_assert (rc != -1); +#endif + // IPC accept() filters #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED if (!filter (sock)) { diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 5f9d92b8..82fa0622 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -288,6 +288,13 @@ zmq::fd_t zmq::tcp_listener_t::accept () } #endif + // Race condition can cause socket not to be closed (if fork happens + // between accept and this point). +#ifdef FD_CLOEXEC + int rc = fcntl (sock, F_SETFD, FD_CLOEXEC); + errno_assert (rc != -1); +#endif + if (!options.tcp_accept_filters.empty ()) { bool matched = false; for (options_t::tcp_accept_filters_t::size_type i = 0; i != options.tcp_accept_filters.size (); ++i) {