Skip to content

Decode each attachment once in extract_attachments - #7

Merged
ebeigarts merged 1 commit into
mainfrom
bugfix/avoid-double-decode
Aug 6, 2026
Merged

Decode each attachment once in extract_attachments#7
ebeigarts merged 1 commit into
mainfrom
bugfix/avoid-double-decode

Conversation

@ebeigarts

Copy link
Copy Markdown
Member

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

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
ebeigarts force-pushed the bugfix/avoid-double-decode branch from a6d4389 to 3bfb6d7 Compare August 6, 2026 23:38
@ebeigarts
ebeigarts merged commit 5ef8e14 into main Aug 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant