Skip to content

Make ActiveSupport::Gzip.compress deterministic - #55382

Merged
byroot merged 1 commit into
rails:mainfrom
Mr0grog:deterministic-gzip-compression
Jul 22, 2025
Merged

byroot merged 1 commit into
rails:mainfrom
Mr0grog:deterministic-gzip-compression

Conversation

@Mr0grog

@Mr0grog Mr0grog commented Jul 22, 2025 •

Copy link
Copy Markdown
Contributor

Motivation / Background

This makes ActiveSupport::Gzip.compress always set the output gzipped data’s last modified timestamp to 0.

I have some tests of code that uses ActiveSupport::Gzip.compress that 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.compress is 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 to 0.

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_name in Ruby Zlib. If mtime should 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:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

`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.
@byroot

byroot commented Jul 22, 2025

Copy link
Copy Markdown
Member

Yeah, gzip timestamp is annoying, I don't see any reason to keep it.

@byroot
byroot merged commit 8cb4f8c into rails:main Jul 22, 2025
@Mr0grog
Mr0grog deleted the deterministic-gzip-compression branch July 22, 2025 08:17
@Mr0grog

Mr0grog commented Jul 22, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for making that quick and easy! ❤️

@phyzical

Copy link
Copy Markdown

and here i was uncompressing the ensure it was the expected result in my specs 😆 nice spot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants