Skip to content

Breaking: Make request_store an optional dependency#296

Merged
willnet merged 3 commits intogazay:masterfrom
krororo:replace-current-attributes
Jan 10, 2026
Merged

Breaking: Make request_store an optional dependency#296
willnet merged 3 commits intogazay:masterfrom
krororo:replace-current-attributes

Conversation

@krororo
Copy link
Contributor

@krororo krororo commented Dec 27, 2025

Summary

Remove hard dependency on request_store.

Problem

The gem currently depends on request_store, which uses Thread#[] for storage. However, Thread#[] provides fiber-local variables rather than thread-local variables, which can cause unexpected behavior in fiber-based concurrency scenarios.

From Ruby documentation:

Thread#[] returns a fiber-local variable

See: https://docs.ruby-lang.org/en/4.0/Thread.html#method-i-5B-5D

Solution

Rails 5.2 introduced ActiveSupport::CurrentAttributes, which properly handles both thread and fiber contexts. This PR:

  1. Uses CurrentAttributes for Rails 5.2+ (no external dependency needed)
  2. Falls back to RequestStore for older Rails versions when available
  3. Makes request_store an optional dependency instead of required

Breaking Change ⚠️

IMPORTANT for Rails < 5.2 users:

If you're using Rails version earlier than 5.2, you must explicitly add request_store to your Gemfile:

gem 'request_store'

Rails 5.2+ users don't need any changes.

BREAKING CHANGE: request_store is no longer a required dependency.
For Rails < 5.2, users must explicitly add `gem 'request_store'`
to their Gemfile.

Rails 5.2+ users will automatically use ActiveSupport::CurrentAttributes
instead. This modernizes the gem while maintaining support for older
Rails versions.
Copy link
Collaborator

@willnet willnet left a comment

Choose a reason for hiding this comment

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

The basic policy looks good (LGTM). Additionally, it would be better to have a test for when a LoadError occurs. I would like to add a test environment running a version below Rails 5.2 without request_store in the Gemfile to verify that the error message is displayed correctly.

Co-authored-by: Shinichi Maeshima <netwillnet@gmail.com>
@krororo krororo force-pushed the replace-current-attributes branch from 8bbe714 to 7017390 Compare January 2, 2026 03:56
@krororo
Copy link
Contributor Author

krororo commented Jan 2, 2026

@willnet Thank you for your review.
I added a test in commit 7017390. Is my understanding correct?
Please let me know if my understanding is incorrect.

@krororo krororo requested a review from willnet January 2, 2026 04:05
@willnet
Copy link
Collaborator

willnet commented Jan 4, 2026

@krororo It is true that the tests are passing even for Rails versions below 5.2 without request_store. However, what I was thinking of was creating a new job in .github/workflows/ci.yml, separate from the test job, to run gon in an environment with specific versions like Ruby 2.4 and Rails 5.1. Could you please make this adjustment?

@krororo krororo force-pushed the replace-current-attributes branch from 7017390 to 9e36954 Compare January 10, 2026 09:02
Add a dedicated CI job to verify that gon properly raises an error
with the expected message when used with Rails 5.1 without the
request_store gem installed.
@krororo krororo force-pushed the replace-current-attributes branch from 9e36954 to 9c13f62 Compare January 10, 2026 09:04
@krororo
Copy link
Contributor Author

krororo commented Jan 10, 2026

@willnet Sorry for the delayed reply.
I added a new job to the ci.yml.
I'm using shell to verify that it produces an error.

@willnet willnet merged commit 22c0b51 into gazay:master Jan 10, 2026
67 of 74 checks passed
@krororo krororo deleted the replace-current-attributes branch January 10, 2026 12:00
@willnet
Copy link
Collaborator

willnet commented Jan 11, 2026

v7.0.0 released!

https://github.com/gazay/gon/releases/tag/v7.0.0

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.

2 participants