apt_repository: Fix crash caused by cache.update() raising an IOError on timeout. (#51996)

* Fix crash caused by cache.update() raising an IOError on timeout.

* Add changelog fragment.
This commit is contained in:
Kevin Backhouse 2019-05-29 20:59:51 +01:00 committed by ansibot
parent c47e9d22b4
commit 79bb7bde5f
2 changed files with 14 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- apt_repository - Fix crash caused by ``cache.update()`` raising an ``IOError`` due to a timeout in ``apt update`` (https://github.com/ansible/ansible/issues/51995)

View file

@ -108,6 +108,7 @@ import os
import re import re
import sys import sys
import tempfile import tempfile
import copy
try: try:
import apt import apt
@ -515,6 +516,7 @@ def main():
else: else:
module.fail_json(msg='Module apt_repository is not supported on target.') module.fail_json(msg='Module apt_repository is not supported on target.')
sourceslist_before = copy.deepcopy(sourceslist)
sources_before = sourceslist.dump() sources_before = sourceslist.dump()
try: try:
@ -544,8 +546,16 @@ def main():
if update_cache: if update_cache:
cache = apt.Cache() cache = apt.Cache()
cache.update() cache.update()
except OSError as err: except (OSError, IOError) as err:
module.fail_json(msg=to_native(err)) # Revert the sourcelist files to their previous state.
# First remove any new files that were created:
for filename in set(sources_after.keys()).difference(sources_before.keys()):
if os.path.exists(filename):
os.remove(filename)
# Now revert the existing files to their former state:
sourceslist_before.save()
# Return an error message.
module.fail_json(msg='apt cache update failed')
module.exit_json(changed=changed, repo=repo, state=state, diff=diff) module.exit_json(changed=changed, repo=repo, state=state, diff=diff)