-
Notifications
You must be signed in to change notification settings - Fork 25
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
The format context mananger is not threadsafe #83
Comments
@walrusVision Thank you for filing a detailed bug report and for following the template. Deep down inside I think I knew that probably wasn't thread-safe, thanks for confirming it. The first thing I'll do is add a warning to the documentation. |
This comes from GitHub issue #83. Using bitmath context-managers, including the plural formatter, is not guaranteed to format instances as desired and may cause errors. Workaround included in doc update.
There we go, https://bitmath.readthedocs.io/en/latest/module.html#context-managers added a note there. Going to leave this bug open for the time being. Maybe I or someone else will have an idea on how to make that work in threads. |
Thanks @tbielawa! |
Using the format context manager in a threaded environment introduces a race condition where the global formatter and global plural flag can be reset by one context manager after another context manager has set it, and before it has formatted a string within that context.
A solution to this is bitmath should either make sure that the context manager is threadsafe, or warn in the documentation that the context manager isn't threadsafe and that a user should use the
Bitmath
object'sformat
function directly instead.How to REPRODUCE the issue:
This test case can reproduce the issue. The test is only checking
format_string
but a similar method could be used to testformat_plural
And will produce the following error
How REPRODUCIBLE (every time? intermittently? only in certain environments?):
In general the the error would only happen in threaded environments where there is a requirement to override the default formatter using the context manager. In our case it was happening intermittently when testing a threaded part of our application which was logging the size of bitmath objects
What you EXPECTED to happen:
For strings to be successfully formatted with the provided formatter to the context
What ACTUALLY happened:
Some sizes where formatted with the formatter of another context.
VERSION of bitmath effected:
Your OPERATING SYSTEM and the affected PYTHON VERSION:
unbuntu16.04 and python 2.7.14
The text was updated successfully, but these errors were encountered: