Repository navigation
Make ActiveSupport::Gzip.compress deterministic - #55382
Merged
Merged
Conversation
`ActiveSupport::Gzip.compress` used to include the current timestamp in the gzipped output (the default behavior of Zlib), causing multiple calls with the same input data to output different results depending on when it was called. In many situations (such as caching), ensuring that the output is always the same for a given input is useful, so this helper now always sets the timestamp to `0` instead.
Member
|
Yeah, gzip timestamp is annoying, I don't see any reason to keep it. |
Contributor
Author
|
Thanks for making that quick and easy! ❤️ |
|
and here i was uncompressing the ensure it was the expected result in my specs 😆 nice spot! |
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.
Motivation / Background
This makes
ActiveSupport::Gzip.compressalways set the output gzipped data’s last modified timestamp to0.I have some tests of code that uses
ActiveSupport::Gzip.compressthat have been flaky for a long time, and recently discovered this is because the output of that method includes the timestamp of when it was compressed. If two calls with the same input happen during different seconds, then you get different output (so, in my flaky tests, they fail to compare correctly).One solution for those flaky tests is to decompress the result and assert that the result is what was expected, but it occurred to me that most folks might be like me and expect this helper to deterministically output the same thing for any given input, so that’s what I’ve done here. Not doing so might be subtly breaking various caching mechanisms for a lot of apps (for example, when investigating the issue, one of the first results I found on the web was a post about someone encountering the same problem in Python). Other parts of Rails itself have dealt with similar problems by doing the same thing internally (#49166).
Detail
Since
ActiveSupport::Gzip.compressis mainly meant as a simple shortcut for Rails users (it doesn’t seem to be used internally), I thought it would be appropriate to simply make it set the timestamp to0.However, that could cause subtle breakage for users who are relying on the current timestamp behavior. I’m happy to make this an option instead (defaulting off if boolean or to the current time if a timestamp) if that’s a problem or if folks think that this should just be configurable in general.
(Relatedly, Gzip headers can also carry an optional filename —
orig_namein Ruby Zlib. Ifmtimeshould be exposed as an option here, it might make sense to expose that, too.)Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]