# Buggy code
def _do_handler_run(
    self, handler, handler_name, iterator, play_context, notified_hosts=None
):
    # FIXME: need to use iterator.get_failed_hosts() instead?
    # if not len(self.get_hosts_remaining(iterator._play)):
    #    self._tqm.send_callback('v2_playbook_on_no_hosts_remaining')
    #    result = False
    #    break
    saved_name = handler.name
    handler.name = handler_name
    self._tqm.send_callback("v2_playbook_on_handler_task_start", handler)
    handler.name = saved_name

    if notified_hosts is None:
        notified_hosts = self._notified_handlers[handler]

    run_once = False
    try:
        action = action_loader.get(handler.action, class_only=True)
        if handler.run_once or getattr(action, "BYPASS_HOST_LOOP", False):
            run_once = True
    except KeyError:
        # we don't care here, because the action may simply not have a
        # corresponding action plugin
        pass

    host_results = []
    for host in notified_hosts:
        if not handler.has_triggered(host) and (
            not iterator.is_failed(host) or play_context.force_handlers
        ):
            task_vars = self._variable_manager.get_vars(
                loader=self._loader, play=iterator._play, host=host, task=handler
            )
            self.add_tqm_variables(task_vars, play=iterator._play)
            self._queue_task(host, handler, task_vars, play_context)
            if run_once:
                break

    # collect the results from the handler run
    host_results = self._wait_on_pending_results(iterator)

    try:
        included_files = IncludedFile.process_include_results(
            host_results,
            self._tqm,
            iterator=iterator,
            inventory=self._inventory,
            loader=self._loader,
            variable_manager=self._variable_manager,
        )
    except AnsibleError as e:
        return False

    result = True
    if len(included_files) > 0:
        for included_file in included_files:
            try:
                new_blocks = self._load_included_file(
                    included_file, iterator=iterator, is_handler=True
                )
                # for every task in each block brought in by the include, add the list
                # of hosts which included the file to the notified_handlers dict
                for block in new_blocks:
                    iterator._play.handlers.append(block)
                    for task in block.block:
                        result = self._do_handler_run(
                            handler=task,
                            handler_name=None,
                            iterator=iterator,
                            play_context=play_context,
                            notified_hosts=included_file._hosts[:],
                        )
                        if not result:
                            break
            except AnsibleError as e:
                for host in included_file._hosts:
                    iterator.mark_host_failed(host)
                    self._tqm._failed_hosts[host.name] = True
                display.warning(str(e))
                continue

    # wipe the notification list
    self._notified_handlers[handler] = []
    display.debug("done running handlers, result is: %s" % result)
    return result


# Traceback (AttributeError)
RUNNING HANDLER [couchdb : restart couchdb] ************************************
included: /home/xxx/git/xxx/xxx/config/ansible/galaxy_roles/couchdb/tasks/restart.yml for default

RUNNING HANDLER [couchdb : uri] ************************************************
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/core/network/basics/uri.py
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: vagrant
<127.0.0.1> SSH: EXEC ssh -vvv -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2222 -o 'IdentityFile="/home/xxx/git/xxx/xxx/.vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=30 -o ControlPath=/home/xxx/.ansible/cp/ansible-ssh-%h-%p-%r 127.0.0.1 '/bin/sh -c '"'"'( umask 77 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514 `" &amp;&amp; echo ansible-tmp-1478255214.96-274210896519514="` echo $HOME/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514 `" ) &amp;&amp; sleep 0'"'"''
<127.0.0.1> PUT /tmp/tmp4o4etY TO /home/vagrant/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514/uri.py
<127.0.0.1> SSH: EXEC scp -vvv -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2222 -o 'IdentityFile="/home/xxx/git/xxx/xxx/.vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=30 -o ControlPath=/home/xxx/.ansible/cp/ansible-ssh-%h-%p-%r /tmp/tmp4o4etY '[127.0.0.1]:/home/vagrant/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514/uri.py'
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: vagrant
<127.0.0.1> SSH: EXEC ssh -vvv -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2222 -o 'IdentityFile="/home/xxx/git/xxx/xxx/.vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=30 -o ControlPath=/home/xxx/.ansible/cp/ansible-ssh-%h-%p-%r 127.0.0.1 '/bin/sh -c '"'"'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514/ /home/vagrant/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514/uri.py &amp;&amp; sleep 0'"'"''
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: vagrant
<127.0.0.1> SSH: EXEC ssh -vvv -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2222 -o 'IdentityFile="/home/xxx/git/xxx/xxx/.vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=30 -o ControlPath=/home/xxx/.ansible/cp/ansible-ssh-%h-%p-%r -tt 127.0.0.1 '/bin/sh -c '"'"'/usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514/uri.py; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1478255214.96-274210896519514/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'"'"''
ERROR! Unexpected Exception: 'NoneType' object has no attribute 'register'
the full traceback was:

Traceback (most recent call last):
File "/usr/bin/ansible-playbook", line 103, in <module>
exit_code = cli.run()
File "/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 159, in run
results = pbex.run()
File "/usr/lib/python2.7/dist-packages/ansible/executor/playbook_executor.py", line 154, in run
result = self._tqm.run(play=play)
File "/usr/lib/python2.7/dist-packages/ansible/executor/task_queue_manager.py", line 282, in run
play_return = strategy.run(iterator, play_context)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/linear.py", line 221, in run
results.extend(self._execute_meta(task, play_context, iterator))
File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 838, in _execute_meta
self.run_handlers(iterator, play_context)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 703, in run_handlers
result = self._do_handler_run(handler, handler.get_name(), iterator=iterator, play_context=play_context)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 772, in _do_handler_run
notified_hosts=included_file._hosts[:],
File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 743, in _do_handler_run
host_results = self._wait_on_pending_results(iterator)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 534, in _wait_on_pending_results
results = self._process_pending_results(iterator)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 316, in _process_pending_results
if original_task.register:
AttributeError: 'NoneType' object has no attribute 'register'
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

# Fixed code
def _do_handler_run(
    self, handler, handler_name, iterator, play_context, notified_hosts=None
):
    # FIXME: need to use iterator.get_failed_hosts() instead?
    # if not len(self.get_hosts_remaining(iterator._play)):
    #    self._tqm.send_callback('v2_playbook_on_no_hosts_remaining')
    #    result = False
    #    break
    saved_name = handler.name
    handler.name = handler_name
    self._tqm.send_callback("v2_playbook_on_handler_task_start", handler)
    handler.name = saved_name

    if notified_hosts is None:
        notified_hosts = self._notified_handlers[handler]

    run_once = False
    try:
        action = action_loader.get(handler.action, class_only=True)
        if handler.run_once or getattr(action, "BYPASS_HOST_LOOP", False):
            run_once = True
    except KeyError:
        # we don't care here, because the action may simply not have a
        # corresponding action plugin
        pass

    host_results = []
    for host in notified_hosts:
        if not handler.has_triggered(host) and (
            not iterator.is_failed(host) or play_context.force_handlers
        ):
            task_vars = self._variable_manager.get_vars(
                loader=self._loader, play=iterator._play, host=host, task=handler
            )
            self.add_tqm_variables(task_vars, play=iterator._play)
            self._queue_task(host, handler, task_vars, play_context)
            if run_once:
                break

    # collect the results from the handler run
    host_results = self._wait_on_pending_results(iterator)

    try:
        included_files = IncludedFile.process_include_results(
            host_results,
            self._tqm,
            iterator=iterator,
            inventory=self._inventory,
            loader=self._loader,
            variable_manager=self._variable_manager,
        )
    except AnsibleError as e:
        return False

    result = True
    if len(included_files) > 0:
        for included_file in included_files:
            try:
                new_blocks = self._load_included_file(
                    included_file, iterator=iterator, is_handler=True
                )
                # for every task in each block brought in by the include, add the list
                # of hosts which included the file to the notified_handlers dict
                for block in new_blocks:
                    iterator._play.handlers.append(block)
                    iterator.cache_block_tasks(block)
                    for task in block.block:
                        result = self._do_handler_run(
                            handler=task,
                            handler_name=None,
                            iterator=iterator,
                            play_context=play_context,
                            notified_hosts=included_file._hosts[:],
                        )
                        if not result:
                            break
            except AnsibleError as e:
                for host in included_file._hosts:
                    iterator.mark_host_failed(host)
                    self._tqm._failed_hosts[host.name] = True
                display.warning(str(e))
                continue

    # wipe the notification list
    self._notified_handlers[handler] = []
    display.debug("done running handlers, result is: %s" % result)
    return result