Decode each attachment once in extract_attachments - #7
Merged
Conversation
Mail#decoded does not memoize (mail-2.9.0 message.rb:1893) — it base64-decodes into a new String on every call. extract_attachments called it twice per attachment, once for the upload body and again for `size:`, allocating the full attachment twice. Measured on ruby:3.4.9-slim under a 384Mi limit with a 34.4MB message (25MB attachment), peak cgroup memory for a single request drops 255.5MB -> 195.2MB, and RSS retained after two concurrent requests drops 125.4MB -> 75.4MB. This does not fix the OOMKills: at 2 concurrent requests peak is unchanged (324.4MB -> 323.9MB) because other copies dominate there, and 3 concurrent requests still exit 137. The remaining cause is that the whole message is held in memory (raw + parsed + decoded) while puma allows 5 concurrent threads. get_message had no test coverage at all, which is how this went unnoticed; add specs for the attachment metadata and a guard asserting a single decode per attachment. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
ebeigarts
force-pushed
the
bugfix/avoid-double-decode
branch
from
August 6, 2026 23:38
a6d4389 to
3bfb6d7
Compare
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.
Mail#decoded does not memoize (mail-2.9.0 message.rb:1893) — it
base64-decodes into a new String on every call. extract_attachments called
it twice per attachment, once for the upload body and again for
size:, allocating the full attachment twice.Measured on ruby:3.4.9-slim under a 384Mi limit with a 34.4MB message
(25MB attachment), peak cgroup memory for a single request drops
255.5MB -> 195.2MB, and RSS retained after two concurrent requests drops
125.4MB -> 75.4MB.
This does not fix the OOMKills: at 2 concurrent requests peak is
unchanged (324.4MB -> 323.9MB) because other copies dominate there, and 3
concurrent requests still exit 137. The remaining cause is that the whole
message is held in memory (raw + parsed + decoded) while puma allows 5
concurrent threads.
get_message had no test coverage at all, which is how this went unnoticed;
add specs for the attachment metadata and a guard asserting a single
decode per attachment.
Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com