Skip to content

Always dup body before mutating it#56

Merged
iMacTia merged 1 commit into
lostisland:mainfrom
byroot:better-fstr-fix
May 20, 2026
Merged

Always dup body before mutating it#56
iMacTia merged 1 commit into
lostisland:mainfrom
byroot:better-fstr-fix

Conversation

@byroot

@byroot byroot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Ref: #13

body = body.dup if body.frozen? isn't a good pattern because while it works to not fail if the string is frozen, it doesn't prevent triggering deprecation warnings if the body happened to be a mutable literal string (AKA chilled string).

Either way, it's preferable to always dup the string, Ruby will take care to share the buffer instead of copying if the string is large enough.

`body = body.dup if body.frozen?` isn't a good pattern because
while it works to not fail if the string is frozen, it doesn't
prevent triggering deprecation warnings if the `body` happened
to be a mutable literal string (AKA chilled string).

Either way, it's preferable to always dup the string, Ruby will
take care to share the buffer instead of copying if the string is
large enough.

@iMacTia iMacTia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby will take care to share the buffer instead of copying if the string is large enough

That is great to hear and it makes total sense.
I'll trust your expertise on this one, thank you for the contribution 🙇!

@iMacTia iMacTia merged commit 4bcd626 into lostisland:main May 20, 2026
@byroot byroot deleted the better-fstr-fix branch May 20, 2026 08:19
@jakeonfire

jakeonfire commented May 28, 2026

Copy link
Copy Markdown
Contributor

we started getting frozen string errors for responses with no response body and no content-type. was the change at line 190 necessary?

edit: we can mitigate the issue on our side, just wanted to flag it.

@byroot

byroot commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

Right, line 190 could be reverted, sorry about that.

kevinebaugh added a commit to kevinebaugh/faraday-net_http that referenced this pull request May 28, 2026
PR lostisland#56 changed the fallback in `encoded_body` from `+''` to `''`. With
`frozen_string_literal: true`, the bare literal is frozen, so a response
with no body and no Content-Type returns a frozen empty string that
raises FrozenError when downstream middleware mutates it. Restore the
unary `+` to keep the fallback mutable, as suggested by the maintainer.

Co-Authored-By: jakeonfire <68826+jakeonfire@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
olleolleolle pushed a commit that referenced this pull request May 29, 2026
)

PR #56 changed the fallback in `encoded_body` from `+''` to `''`. With
`frozen_string_literal: true`, the bare literal is frozen, so a response
with no body and no Content-Type returns a frozen empty string that
raises FrozenError when downstream middleware mutates it. Restore the
unary `+` to keep the fallback mutable, as suggested by the maintainer.

Co-authored-by: jakeonfire <68826+jakeonfire@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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.

3 participants