Repository: ansible/ansible

Issue:
Reset persistent connection fails with socket not present error
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
plugins/strategy/__init__.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
2.5
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).

-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->

##### SUMMARY
<!--- Explain the problem briefly -->

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
Run junos integration test:

```yaml
$ ANSIBLE_ROLES_PATH=targets ansible-playbook -i inventory.network junos.yaml -vvvv --diff
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [Has any previous test failed?] **************************************************************************************************************
task path: /Users/gnalawad/ansible/ganeshrn/ansible/test/integration/junos.yaml:174
skipping: [junos] => {
    "changed": false,
    "skip_reason": "Conditional result was False"
}
META: ran handlers
META: ran handlers
ERROR! Unexpected Exception, this is probably a bug: socket_path does not exist or cannot be found
the full traceback was:

Traceback (most recent call last):
  File "/Users/gnalawad/ansible/ganeshrn/ansible/bin/ansible-playbook", line 109, in <module>
    exit_code = cli.run()
  File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/cli/playbook.py", line 130, in run
    results = pbex.run()
  File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/executor/playbook_executor.py", line 154, in run
    result = self._tqm.run(play=play)
  File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/executor/task_queue_manager.py", line 296, in run
    strategy.cleanup()
  File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/plugins/strategy/__init__.py", line 146, in cleanup
    conn.reset()
  File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/module_utils/connection.py", line 140, in __rpc__
    response = self._exec_jsonrpc(name, *args, **kwargs)
  File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/module_utils/connection.py", line 115, in _exec_jsonrpc
    raise ConnectionError('socket_path does not exist or cannot be found')
ConnectionError: socket_path does not exist or cannot be found
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py
--- a/lib/ansible/plugins/strategy/__init__.py
+++ b/lib/ansible/plugins/strategy/__init__.py
@@ -36,7 +36,7 @@
 from ansible.module_utils.six.moves import queue as Queue
 from ansible.module_utils.six import iteritems, itervalues, string_types
 from ansible.module_utils._text import to_text
-from ansible.module_utils.connection import Connection
+from ansible.module_utils.connection import Connection, ConnectionError
 from ansible.playbook.helpers import load_list_of_blocks
 from ansible.playbook.included_file import IncludedFile
 from ansible.playbook.task_include import TaskInclude
@@ -140,8 +140,12 @@ def __init__(self, tqm):
     def cleanup(self):
         # close active persistent connections
         for sock in itervalues(self._active_connections):
-            conn = Connection(sock)
-            conn.reset()
+            try:
+                conn = Connection(sock)
+                conn.reset()
+            except ConnectionError as e:
+                # most likely socket is already closed
+                display.debug("got an error while closing persistent connection: %s" % e)
         self._final_q.put(_sentinel)
         self._results_thread.join()
 


Test patch: