Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError: local variable 'f' referenced before assignment in /plugins/cache/jsonfile.py", line 101 #16268

Closed
altvod opened this issue Jun 14, 2016 · 2 comments
Labels
bug This issue/PR relates to a bug.

Comments

@altvod
Copy link

altvod commented Jun 14, 2016

ISSUE TYPE
  • Bug Report
ANSIBLE VERSION
ansible 2.0.2.0

Variable f (in the finally clause) is not defined if file opening failed in /plugins/cache/jsonfile.py, line 94:

 93         try:
 94             f = codecs.open(cachefile, 'w', encoding='utf-8')
 95         except (OSError,IOError) as e:
 96             display.warning("error while trying to write to %s : %s" % (cachefile, to_bytes(e)))
 97             pass
 98         else:
 99             f.write(jsonify(value))
100         finally:
101             f.close()

Which sometimes results in the following exception (thus hiding the original exception):

Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 86, in <module>
    sys.exit(cli.run())
  File "/usr/lib/python2.6/site-packages/ansible/cli/playbook.py", line 150, in run
    results = pbex.run()
  File "/usr/lib/python2.6/site-packages/ansible/executor/playbook_executor.py", line 140, in run
    result = self._tqm.run(play=play)
  File "/usr/lib/python2.6/site-packages/ansible/executor/task_queue_manager.py", line 238, in run
    play_return = strategy.run(iterator, play_context)
  File "/usr/lib/python2.6/site-packages/ansible/plugins/strategy/linear.py", line 268, in run
    results += self._wait_on_pending_results(iterator)
  File "/usr/lib/python2.6/site-packages/ansible/plugins/strategy/__init__.py", line 354, in _wait_on_pending_results
    results = self._process_pending_results(iterator)
  File "/usr/lib/python2.6/site-packages/ansible/plugins/strategy/__init__.py", line 331, in _process_pending_results
    self._variable_manager.set_host_facts(target_host, facts)
  File "/usr/lib/python2.6/site-packages/ansible/vars/__init__.py", line 580, in set_host_facts
    self._fact_cache.update(host.name, facts)
  File "/usr/lib/python2.6/site-packages/ansible/plugins/cache/__init__.py", line 77, in update
    self._plugin.set(key, host_cache)
  File "/usr/lib/python2.6/site-packages/ansible/plugins/cache/jsonfile.py", line 101, in set
    f.close()
UnboundLocalError: local variable 'f' referenced before assignment

I guess, there's no need to close if we failed to open...
This is line 104 in the current version

bcoca added a commit that referenced this issue Jun 14, 2016
@bcoca bcoca closed this as completed in 07846f7 Jun 14, 2016
@altvod
Copy link
Author

altvod commented Jun 14, 2016

I disagree with the fix - it hides the original exception
(can't reopen the issue though...)

@bcoca
Copy link
Member

bcoca commented Jun 14, 2016

the original exception is already dealt with. it issues a warning

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

3 participants