Skip to content

Conversation

@bvicenzo
Copy link
Contributor

using the gem Twitter, that uses this gem, we're receiving the bellow error:
force_encoding

This would have happened on this PR when the strings inside of gem have became frozen also, and the problem was solved making test strings unfrozen.

This PR proposes to make the Response class deal with frozen strings.

@bvicenzo bvicenzo force-pushed the bv-fix-force-encoding-for-frozen-strings branch from 39a07dd to 312ffc0 Compare March 10, 2021 18:25
@bvicenzo bvicenzo force-pushed the bv-fix-force-encoding-for-frozen-strings branch from 312ffc0 to 7c81046 Compare March 10, 2021 18:41
@ixti
Copy link
Member

ixti commented Mar 11, 2021

thanks for the PR i'm gonna merge it together with the one that changes the api to be more io alike asap

@caifara
Copy link

caifara commented Aug 18, 2021

We're running into this too, any chance on a quick release?

@tarcieri tarcieri merged commit 2df5e46 into httprb:master Aug 18, 2021
@tarcieri
Copy link
Member

Can you confirm the problem is fixed? If so, I can cut a release

@caifara
Copy link

caifara commented Aug 18, 2021

That's really great! I'm quite ashamed to first ask a fast release only to see the frozen version in our dependencies to be quite a bit older. That said, the mentioned error and changes in this PR point to the exact same problem and the source doesn't seem to have changed much on that point.

Another problem is that we see the error in production sporadically, but not in development. That means there's no straightforward way to be sure the problem is really fixed.

And yet another problem is that I don't really know why the solution proposed in this PR is really helping. Where is the bug in the original code? I can only trigger the bug locally by forcing @contents.freeze in the while loop. As you don't run a release immediately I'm guessing you too aren't entirely sure?

@tarcieri
Copy link
Member

Correct, I'm not sure either, and this looks like it will allocate two strings for every read, which will increase GC pressure. In that regard it's probably not the greatest solution.

@tarcieri
Copy link
Member

A better strategy is probably:

  1. Check if string matches @encoding. If it does, we're done.
  2. Check if string is frozen. If it isn't, use #force_encoding(@encoding)
  3. If the string is frozen, allocate a new string with the given encoding

@bvicenzo bvicenzo deleted the bv-fix-force-encoding-for-frozen-strings branch August 18, 2021 20:43
@tarcieri tarcieri mentioned this pull request Sep 10, 2021
ojab added a commit to ojab/webmock that referenced this pull request Jan 14, 2022
After httprb/http#653 http.rb doesn't force
encode stream result to be in the correct encoding
lfdebrux added a commit to alphagov/tech-docs-monitor that referenced this pull request Feb 28, 2022
Bumps [http](https://github.com/httprb/http) from 3.3.0 to 5.0.4.
- [Changelog](https://github.com/httprb/http/blob/main/CHANGES.md)
- [Commits](httprb/http@v3.3.0...v5.0.4)

Breaking changes don't affect this project. This should also fix any
issues from frozen string weirdness in later versions of Ruby (see
[comment in PR #40][1], httprb/http#653).

[1]: #40 (comment)
lfdebrux added a commit to alphagov/tech-docs-monitor that referenced this pull request Feb 28, 2022
Bumps [http](https://github.com/httprb/http) from 3.3.0 to 5.0.4.
- [Changelog](https://github.com/httprb/http/blob/main/CHANGES.md)
- [Commits](httprb/http@v3.3.0...v5.0.4)

Breaking changes don't affect this project. This should also fix any
issues from frozen string weirdness in later versions of Ruby (see
[[1]], [[2]]).

[1]: #40 (comment)
[2]: httprb/http#653
timja added a commit to hmcts/tech-docs-monitor that referenced this pull request May 6, 2022
* Bump addressable from 2.5.2 to 2.8.0

Bumps [addressable](https://github.com/sporkmonger/addressable) from 2.5.2 to 2.8.0.
- [Release notes](https://github.com/sporkmonger/addressable/releases)
- [Changelog](https://github.com/sporkmonger/addressable/blob/main/CHANGELOG.md)
- [Commits](sporkmonger/addressable@addressable-2.5.2...addressable-2.8.0)

---
updated-dependencies:
- dependency-name: addressable
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

* Remove GOV.UK Developer Docs from the notify list

GOV.UK no longer uses 'Daniel the Manual Spaniel', since alphagov/govuk-developer-docs#2836 and alphagov/govuk-developer-docs#2837 were merged.

* Bump rake from 13.0.3 to 13.0.6

Bumps [rake](https://github.com/ruby/rake) from 13.0.3 to 13.0.6.
- [Release notes](https://github.com/ruby/rake/releases)
- [Changelog](https://github.com/ruby/rake/blob/master/History.rdoc)
- [Commits](ruby/rake@v13.0.3...v13.0.6)

---
updated-dependencies:
- dependency-name: rake
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* TRG-139: Support sites which use relative URLs.

This involves using the site's URL, in combination with the relative URLs returned in `/api/pages.json` and using them to build absolute an absolute URL to each page which is to be notified upon.

Sites which use absolute links are unaffected.

* Add gitignore

This project currently lets you commit the `.bundle` folder, if you're
not careful. Add a copy of the GitHub suggested gitignore file for Ruby
projects [[1]], to make it harder to do that.

[1]: https://github.com/github/gitignore/blob/ce5da10a3a43c4dd8bd9572eda17c0a37ee0eac1/Ruby.gitignore

* Bump http from 3.3.0 to 5.0.4

Bumps [http](https://github.com/httprb/http) from 3.3.0 to 5.0.4.
- [Changelog](https://github.com/httprb/http/blob/main/CHANGES.md)
- [Commits](httprb/http@v3.3.0...v5.0.4)

Breaking changes don't affect this project. This should also fix any
issues from frozen string weirdness in later versions of Ruby (see
[[1]], [[2]]).

[1]: alphagov#40 (comment)
[2]: httprb/http#653

* Bump Ruby to latest 2.x release

Ruby 2.6 is in security maintenance phase and will reach end-of-life
soon [[1]]. This commit changes the `.ruby-version` file used by
developers and tooling to use the current latest release of 2.7, which
is the last in the 2.x series.

[1]: https://www.ruby-lang.org/en/downloads/branches/

* Fix Concourse pipeline

Missed this from commit d375525.

* Bump bundler from 1.7.5 to 2.1.4

Bumps bundler to the same version as used in the current ruby:2.75
Docker image (280416eb1a1c).

* Reorder and group dependencies

* Bump vcr from 4.0.0 to 6.0.0

Bumps [vcr](https://github.com/vcr/vcr) from 4.0.0 to 6.0.0.
- [Release notes](https://github.com/vcr/vcr/releases/tag/v6.0.0)
- [Changelog](https://github.com/vcr/vcr/blob/master/CHANGELOG.md)
- [Commits](vcr/vcr@v4.0.0...v6.0.0)

Breaking changes don't affect this project.

* Remove default help text from spec_helper file

* Move VCR configuration to spec_helper file

* Add VCR RSpec metadata configuration

Needed to make `vcr: true` work.

* Use VCR RSpec metadata instead of before and after blocks

* Add tests for Notifier#run

* Add support for Slack chat.postMessage API

If the environment variable `SLACK_TOKEN` is set use Slack's REST API
[1] instead of webhooks.

This is needed because new-style webhooks generated by Slack apps cannot
post to any channel [2]; instead we need to use an OAuth token with
scopes chat:write and chat:write.public.

[1]: https://api.slack.com/methods/chat.postMessage
[2]: https://api.slack.com/messaging/webhooks

* Replace Concourse pipeline to run monitor with GitHub Actions workflow

We want to use GitHub Actions everywhere, instead of having some of the
infra running on a Concourse installation.

This commit replaces the Concourse CI pipeline with a GitHub Actions
workflow.

Note that although the schedules are configured to be similar, they
won't be exactly the same, as concourses' time resource behaves
differently to cron. Also, the GitHub Actions scheduled event doesn't
allow configuration of timezone, so the midday-ness of the new scheduled
messages will be subject to daylight savings.

Another change from the Concourse pipeline is that we now use a Slack
token rather than a webhook. This is so that we can use a Slack app to
configure the secrets for the integration, rather than the deprecated
webhooks. This should hopefully make sharing the control of the
integration among multiple people easier.

* Remove unneeded notify file

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris Ashton <ChrisBAshton@users.noreply.github.com>
Co-authored-by: Laurence de Bruxelles <laurence.debruxelles@digital.cabinet-office.gov.uk>
Co-authored-by: edd.grant <edd@eddgrant.com>
Co-authored-by: Philip Potter <philip.g.potter@gmail.com>
timja pushed a commit to hmcts/tech-docs-monitor that referenced this pull request May 6, 2022
Bumps [http](https://github.com/httprb/http) from 3.3.0 to 5.0.4.
- [Changelog](https://github.com/httprb/http/blob/main/CHANGES.md)
- [Commits](httprb/http@v3.3.0...v5.0.4)

Breaking changes don't affect this project. This should also fix any
issues from frozen string weirdness in later versions of Ruby (see
[[1]], [[2]]).

[1]: alphagov#40 (comment)
[2]: httprb/http#653
timja pushed a commit to hmcts/tech-docs-monitor that referenced this pull request May 6, 2022
Bumps [http](https://github.com/httprb/http) from 3.3.0 to 5.0.4.
- [Changelog](https://github.com/httprb/http/blob/main/CHANGES.md)
- [Commits](httprb/http@v3.3.0...v5.0.4)

Breaking changes don't affect this project. This should also fix any
issues from frozen string weirdness in later versions of Ruby (see
[[1]], [[2]]).

[1]: alphagov#40 (comment)
[2]: httprb/http#653
@cooljeanius
Copy link

This caused sferik/t-ruby#452; how do I update its dependency on this to include the fix? I tried something in sferik/t-ruby#462 but I'm not sure if it's right... (I don't actually speak ruby)

@tarcieri
Copy link
Member

It was released in v5.0.2 in August of last year.

You need to update your Gemfile.lock, e.g. bundle update

@janko
Copy link
Member

janko commented Jan 15, 2024

Correct, I'm not sure either, and this looks like it will allocate two strings for every read, which will increase GC pressure. In that regard it's probably not the greatest solution.

Yeah, the benefit of the previous String#clear implementation was that it deallocates chunks immediately, instead of having to wait for the GC to collect them. So, after this change, HTTP::Response::Body#to_s allocates 2x more memory than it did previously for responses with large bodies (e.g. images).

We have a background job where we need to download many images in a row, and it's currently getting OOM-killed. Deallocating content immediately after its no longer would likely help prevent the memory usage from growing too rapidly, allowing time for GC. I wanted to use Body#to_s for downloading images, but now image chunks will remain in memory that I cannot String#clear, so I'll need to use Body#readpartial instead.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Aug 11, 2025
Changelog:
## [5.3.1] - 2025-06-09

### Changed

- Revert switch to the native llhttp on MRI, as it's not compatible with
  standalone bundles
  ([#802](httprb/http#802))


## [5.3.0] - 2025-06-09

### Added

- (backported) Add .retriable feature to Http
- (backported) Add more specific ConnectionError classes
- (backported) New feature: RaiseError

### Changed

- (backported) Drop depenency on base64
- (backported) Cache header normalization to reduce object allocation
- (backported) Use native llhttp on MRI


## [5.2.0] - 2024-02-05

### Added

- Add `Connection#finished_request?`
  ([#743](httprb/http#743))
- Add `Instrumentation#on_error`
  ([#746](httprb/http#746))
- Add `base64` dependency (suppresses warnings on Ruby 3.0)
  ([#759](httprb/http#759))
- Add `PURGE` HTTP verb
  ([#757](httprb/http#757))
- Add Ruby-3.3 support

### Changed

- **BREAKING** Process features in reverse order
  ([#766](httprb/http#766))
- **BREAKING** Downcase Content-Type charset name
  ([#753](httprb/http#753))
- **BREAKING** Make URI normalization more conservative
  ([#758](httprb/http#758))

### Fixed

- Close sockets on initialize failure
  ([#762](httprb/http#762))
- Prevent CRLF injection due to broken URL normalizer
  ([#765](httprb/http#765))

[unreleased]: httprb/http@v5.3.0...5-x-stable
[5.3.0]: httprb/http@v5.2.0...v5.3.0
[5.2.0]: httprb/http@v5.1.1...v5.2.0

## 5.1.1 (2022-12-17)

* [#731](httprb/http#731)
  Strip brackets from IPv6 addresses in `HTTP::URI`.
  ([@jeraki])

* [#722](httprb/http#722)
  Add `on_redirect` callback.
  ([@benubois])

## 5.1.0 (2022-06-17)

* Drop ruby-2.5 support.

* [#715](httprb/http#715)
  Set default encoding to UTF-8 for `application/json`.
  ([@drwl])

* [#712](httprb/http#712)
  Recognize cookies set by redirect.
  ([@tkellogg])

* [#707](httprb/http#707)
  Distinguish connection timeouts.
  ([@YuLeven])

## 5.0.4 (2021-10-07)

* [#698](httprb/http#698)
  Fix `HTTP::Timeout::Global#connect_ssl`.
  ([@tarcieri])

## 5.0.3 (2021-10-06)

* [#695](httprb/http#695)
  Revert DNS resolving feature.
  ([@PhilCoggins])

* [#694](httprb/http#694)
  Fix cookies extraction.
  ([@flosacca])

## 5.0.2 (2021-09-10)

* [#686](httprb/http#686)
  Correctly reset the parser.
  ([@bryanp])

* [#684](httprb/http#684)
  Don't set Content-Length for GET, HEAD, DELETE, or CONNECT requests without a BODY.
  ([@jyn514])

* [#679](httprb/http#679)
  Use features on redirected requests.
  ([@nomis])

* [#678](httprb/http#678)
  Restore `HTTP::Response` `:uri` option for backwards compatibility.
  ([@schwern])

* [#676](httprb/http#676)
  Update addressable because of CVE-2021-32740.
  ([@matheussilvasantos])

* [#653](httprb/http#653)
  Avoid force encodings on frozen strings.
  ([@bvicenzo])

* [#638](httprb/http#638)
  DNS failover handling.
  ([@midnight-wonderer])


## 5.0.1 (2021-06-26)

* [#670](httprb/http#670)
  Revert `Response#parse` behavior introduced in [#540].
  ([@DannyBen])

* [#669](httprb/http#669)
  Prevent bodies from being resubmitted when following unsafe redirects.
  ([@odinhb])

* [#664](httprb/http#664)
  Bump llhttp-ffi to 0.3.0.
  ([@bryanp])


## 5.0.0 (2021-05-12)

* [#656](httprb/http#656)
  Handle connection timeouts in `Features`
  ([@semenyukdmitry])

* [#651](httprb/http#651)
  Replace `http-parser` with `llhttp`
  ([@bryanp])

* [#647](httprb/http#647)
  Add support for `MKCALENDAR` HTTP verb
  ([@meanphil])

* [#632](httprb/http#632)
  Respect the SSL context's `verify_hostname` value
  ([@colemannugent])

* [#625](httprb/http#625)
  Fix inflator with empty responses
  ([@LukaszMaslej])

* [#599](httprb/http#599)
  Allow passing `HTTP::FormData::{Multipart,UrlEncoded}` object directly.
  ([@ixti])

* [#593](httprb/http#593)
  [#592](httprb/http#592)
  Support informational (1XX) responses.
  ([@ixti])

* [#590](httprb/http#590)
  [#589](httprb/http#589)
  Fix response headers paring.
  ([@Bonias])

* [#587](httprb/http#587)
  [#585](httprb/http#585)
  Fix redirections when server responds with multiple Location headers.
  ([@ixti])

* [#581](httprb/http#581)
  [#582](httprb/http#582)
  Add Ruby 2.7.x support.
  ([@janko])

* [#577](httprb/http#577)
  Fix `Chainable#timeout` with frozen Hash.
  ([@antonvolkoff])

* [#576](httprb/http#576)
  [#524](httprb/http#524)
  **BREAKING CHANGE**
  Preserve header names casing.
  ([@joshuaflanagan])

* [#540](httprb/http#540)
  [#538](httprb/http#538)
  **BREAKING CHANGE**
  Require explicit MIME type for Response#parse
  ([@ixti])

* [#532](httprb/http#532)
  Fix pipes support in request bodies.
  ([@ixti])

* [#530](httprb/http#530)
  Improve header fields name/value validation.
  ([@Bonias])

* [#506](httprb/http#506)
  [#521](httprb/http#521)
  Skip auto-deflate when there is no body.
  ([@Bonias])

* [#489](httprb/http#489)
  Fix HTTP parser.
  ([@ixti], [@fxposter])

* [#546](httprb/http#546)
  **BREAKING CHANGE**
  Provide initiating `HTTP::Request` object on `HTTP::Response`.
  ([@joshuaflanagan])

* [#571](httprb/http#571)
  Drop Ruby 2.3.x support.
  ([@ixti])

* [3ed0c31](httprb/http@3ed0c31)
  Drop Ruby 2.4.x support.
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.

6 participants