Multiprocess mode: using direct assignment for writing into mmap#315
Merged
brian-brazil merged 2 commits intoprometheus:masterfrom Oct 2, 2018
Merged
Multiprocess mode: using direct assignment for writing into mmap#315brian-brazil merged 2 commits intoprometheus:masterfrom
brian-brazil merged 2 commits intoprometheus:masterfrom
Conversation
…into Using pack_into can create atomicity problems when writing on cpython, setting the bytes to 0 before writing the value. When having a lot of writes, some reads would return 0 instead of the real value. Signed-off-by: David Guerrero <heldroe@gmail.com>
brian-brazil
reviewed
Oct 2, 2018
Contributor
brian-brazil
left a comment
There was a problem hiding this comment.
Thanks, can you add a comment so this doesn't get accidentally reverted in future?
prometheus_client/core.py
Outdated
| @@ -31,7 +31,7 @@ | |||
| _INITIAL_MMAP_SIZE = 1 << 20 | |||
|
|
|||
| _pack_integer = struct.Struct(b'i').pack_into | |||
Contributor
There was a problem hiding this comment.
Is there the same issue with this?
Contributor
Author
There was a problem hiding this comment.
Didn't test in details but I would assume so. I'll apply the same fix there.
…ify the issue. Signed-off-by: David Guerrero <heldroe@gmail.com>
Contributor
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@brian-brazil
Using
struct.pack_intocan create atomicity problems when writing on CPython, setting the bytes to 0 before writing the value.When having a lot of writes, some reads would return 0 instead of the real value. This line in CPython is suspected: https://github.com/python/cpython/blob/2.7/Modules/_struct.c#L1563
You can reproduce the issue by having two scripts running in two different processes but using the same file, one writing an always incrementing counter and the other reading it. Sometimes, the reader script will read 0 instead of the real value. We encountered this in production by running a Django web service behind gunicorn.
Example writer script:
Example reader script:
If you execute both of these at the same time, you will see the number of 0 reads increasing. This PR fixes the issue.