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

tmp_bind cleanup in the face of exceptions #64

Closed
Tinche opened this issue Jan 20, 2016 · 4 comments
Closed

tmp_bind cleanup in the face of exceptions #64

Tinche opened this issue Jan 20, 2016 · 4 comments

Comments

@Tinche
Copy link
Contributor

@Tinche Tinche commented Jan 20, 2016

The tmp_bind context manager clean-up logic (the code after the yield) isn't protected by a finally clause, so it doesn't get executed when an exception is raised.

>>> from structlog import wrap_logger, PrintLogger
>>> from structlog.threadlocal import tmp_bind, wrap_dict
>>> logger = wrap_logger(PrintLogger(),  context_class=wrap_dict(dict))
>>> try:
...  with tmp_bind(logger, x=5) as tmp_logger:
...   logger = logger.bind(y=3)
...   tmp_logger.msg('event')
...   raise ValueError()
... except:
...  pass
...
x=5 event='event' y=3
>>> logger.msg('event')
x=5 event='event' y=3

I would say the expected behavior is for the last logger to just emit event='event'.

If you agree, it's a simple fix that I could contribute.

@hynek
Copy link
Owner

@hynek hynek commented Jan 21, 2016

Sounds reasonable to me! Cleanup should happen in any case.

@hynek
Copy link
Owner

@hynek hynek commented Jan 22, 2016

(also hurry up if you want to see it in 16.0.0 :))

@Tinche
Copy link
Contributor Author

@Tinche Tinche commented Jan 22, 2016

Will do as soon as I get some free time (today or tomorrow) ;)

@Tinche
Copy link
Contributor Author

@Tinche Tinche commented Jan 23, 2016

Dealt with in #65.

@Tinche Tinche closed this Jan 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants