# Buggy code
def register_with_event_loop(self, hub):
    """Register the async pool with the current event loop."""
    self._result_handler.register_with_event_loop(hub)
    self.handle_result_event = self._result_handler.handle_event
    self._create_timelimit_handlers(hub)
    self._create_process_handlers(hub)
    self._create_write_handlers(hub)

    # Add handler for when a process exits (calls maintain_pool)
    [self._track_child_process(w, hub) for w in self._pool]
    # Handle_result_event is called whenever one of the
    # result queues are readable.
    [hub.add_reader(fd, self.handle_result_event, fd) for fd in self._fileno_to_outq]

    # Timers include calling maintain_pool at a regular interval
    # to be certain processes are restarted.
    for handler, interval in items(self.timers):
        hub.call_repeatedly(interval, handler)

    hub.on_tick.add(self.on_poll_start)


# Traceback (OSError)
[user] celery.worker.consumer.consumer WARNING 2017-12-18 00:38:27,078 consumer:
Connection to broker lost. Trying to re-establish the connection...
Traceback (most recent call last):
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 320, in start
blueprint.start(self)
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start
step.start(parent)
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 596, in start
c.loop(*c.loop_args())
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/worker/loops.py", line 47, in asynloop
obj.controller.register_with_event_loop(hub)
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/worker/worker.py", line 217, in register_with_event_loop
description='hub.register',
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/bootsteps.py", line 151, in send_all
fun(parent, *args)
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/worker/components.py", line 178, in register_with_event_loop
w.pool.register_with_event_loop(hub)
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/concurrency/prefork.py", line 134, in register_with_event_loop
return reg(loop)
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/concurrency/asynpool.py", line 476, in register_with_event_loop
for fd in self._fileno_to_outq]
File "/home/user/.envs/user/lib/python3.6/site-packages/celery/concurrency/asynpool.py", line 476, in <listcomp>
for fd in self._fileno_to_outq]
File "/home/user/.envs/user/lib/python3.6/site-packages/kombu/async/hub.py", line 207, in add_reader
return self.add(fds, callback, READ | ERR, args)
File "/home/user/.envs/user/lib/python3.6/site-packages/kombu/async/hub.py", line 158, in add
self.poller.register(fd, flags)
File "/home/user/.envs/user/lib/python3.6/site-packages/kombu/utils/eventio.py", line 67, in register
self._epoll.register(fd, events)
OSError: [Errno 9] Bad file descriptor

# Fixed code
def register_with_event_loop(self, hub):
    """Register the async pool with the current event loop."""
    self._result_handler.register_with_event_loop(hub)
    self.handle_result_event = self._result_handler.handle_event
    self._create_timelimit_handlers(hub)
    self._create_process_handlers(hub)
    self._create_write_handlers(hub)

    # Add handler for when a process exits (calls maintain_pool)
    [self._track_child_process(w, hub) for w in self._pool]
    # Handle_result_event is called whenever one of the
    # result queues are readable.
    stale_fds = []
    for fd in self._fileno_to_outq:
        try:
            hub.add_reader(fd, self.handle_result_event, fd)
        except OSError:
            logger.info(
                "Encountered OSError while trying to access fd %s ", fd, exc_info=True
            )
            stale_fds.append(fd)  # take note of stale fd
    for fd in stale_fds:  # Remove now defunct file descriptors
        self._fileno_to_outq.pop(fd, None)

    # Timers include calling maintain_pool at a regular interval
    # to be certain processes are restarted.
    for handler, interval in items(self.timers):
        hub.call_repeatedly(interval, handler)

    hub.on_tick.add(self.on_poll_start)