# Buggy code
def _initialize_notified_handlers(self, play):
    """
    Clears and initializes the shared notified handlers dict with entries
    for each handler in the play, which is an empty array that will contain
    inventory hostnames for those hosts triggering the handler.
    """

    # Zero the dictionary first by removing any entries there.
    # Proxied dicts don't support iteritems, so we have to use keys()
    self._notified_handlers.clear()
    self._listening_handlers.clear()

    def _process_block(b):
        temp_list = []
        for t in b.block:
            if isinstance(t, Block):
                temp_list.extend(_process_block(t))
            else:
                temp_list.append(t)
        return temp_list

    handler_list = []
    for handler_block in play.handlers:
        handler_list.extend(_process_block(handler_block))

    # then initialize it with the given handler list
    for handler in handler_list:
        if handler._uuid not in self._notified_handlers:
            self._notified_handlers[handler._uuid] = []
        if handler.listen:
            listeners = handler.listen
            if not isinstance(listeners, list):
                listeners = [listeners]
            for listener in listeners:
                if listener not in self._listening_handlers:
                    self._listening_handlers[listener] = []
                self._listening_handlers[listener].append(handler._uuid)


# Traceback (KeyError)
ansible-playbook -vvvv playbook.yml
No config file found; using defaults
[WARNING]: Host file not found: /etc/ansible/hosts

[WARNING]: provided hosts list is empty, only localhost is available

Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: playbook.yml *********************************************************
1 plays in playbook.yml

PLAY [localhost] ***************************************************************

TASK [include_role] ************************************************************
task path: /Users/xxx/workspace/xxx/playbook.yml:6

TASK [handlertest : command] ***************************************************
task path: /Users/xxx/workspace/xxx/roles/handlertest/tasks/main.yml:3
Using module file /usr/local/lib/python2.7/site-packages/ansible/modules/core/commands/command.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: xxx
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1478572148.78-187995060050975 `" &amp;&amp; echo ansible-tmp-1478572148.78-187995060050975="` echo $HOME/.ansible/tmp/ansible-tmp-1478572148.78-187995060050975 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /var/folders/62/pqgvprd11fb4k6shw2pdzkqm0000gq/T/tmpGA2Gau TO /Users/xxx/.ansible/tmp/ansible-tmp-1478572148.78-187995060050975/command.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/xxx/.ansible/tmp/ansible-tmp-1478572148.78-187995060050975/ /Users/xxx/.ansible/tmp/ansible-tmp-1478572148.78-187995060050975/command.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/local/opt/python/bin/python2.7 /Users/xxx/.ansible/tmp/ansible-tmp-1478572148.78-187995060050975/command.py; rm -rf "/Users/xxx/.ansible/tmp/ansible-tmp-1478572148.78-187995060050975/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
ERROR! Unexpected Exception: HANDLER: handlertest : Some handler
the full traceback was:

Traceback (most recent call last):
File "/usr/local/bin/ansible-playbook", line 103, in <module>
exit_code = cli.run()
File "/usr/local/lib/python2.7/site-packages/ansible/cli/playbook.py", line 159, in run
results = pbex.run()
File "/usr/local/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 154, in run
result = self._tqm.run(play=play)
File "/usr/local/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py", line 282, in run
play_return = strategy.run(iterator, play_context)
File "/usr/local/lib/python2.7/site-packages/ansible/plugins/strategy/linear.py", line 278, in run
results += self._wait_on_pending_results(iterator)
File "/usr/local/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 534, in _wait_on_pending_results
results = self._process_pending_results(iterator)
File "/usr/local/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 399, in _process_pending_results
if original_host not in self._notified_handlers[target_handler]:
KeyError: HANDLER: handlertest : Some handler

# Fixed code
def _initialize_notified_handlers(self, play):
    """
    Clears and initializes the shared notified handlers dict with entries
    for each handler in the play, which is an empty array that will contain
    inventory hostnames for those hosts triggering the handler.
    """

    # Zero the dictionary first by removing any entries there.
    # Proxied dicts don't support iteritems, so we have to use keys()
    self._notified_handlers.clear()
    self._listening_handlers.clear()

    def _process_block(b):
        temp_list = []
        for t in b.block:
            if isinstance(t, Block):
                temp_list.extend(_process_block(t))
            else:
                temp_list.append(t)
        return temp_list

    handler_list = []
    for handler_block in play.handlers:
        handler_list.extend(_process_block(handler_block))
    # then initialize it with the given handler list
    self.update_handler_list(handler_list)