CSV.parse changes I18n.locale during header_converters since v3.2.6 and still in v3.3.5
## summary
When using CSV.parse with header_converters, the I18n.locale value is unexpectedly changed inside the converter block in csv v3.2.6.
* Expected: I18n.locale remains the same (:en in this case).
* Actual: Inside the converter block, I18n.locale becomes :ja (the default locale), even though it is :en before and after parsing.
* This regression started in v3.2.6 — it worked as expected until v3.2.5.
## Steps to reproduce
```ruby
# Gemfileå
source "https://rubygems.org"
gem "csv", "3.3.5"
gem "i18n", "1.14.7"
## repro.rb
require "csv"
require "i18n"
I18n.available_locales = [:en, :ja]
I18n.default_locale = :ja
I18n.locale = :en
puts "[before] I18n.locale=#{I18n.locale}"
data = "name\nTaro\n"
CSV.parse(data,
headers: true,
header_converters: [
->(f, info) { puts "[lambda] I18n.locale=#{I18n.locale}" }
]
)
puts "[after] I18n.locale=#{I18n.locale}"
```
```bash
bundle install
bundle exec ruby repro.rb
```
### Expected behavior
```bash
[before] I18n.locale=en
[lambda] I18n.locale=en
[after] I18n.locale=en
```
### Actual behavior
```bash
[before] I18n.locale=en
[lambda] I18n.locale=ja
[after] I18n.locale=en
```
## System configuration
**Ruby version**: 3.4.6
**CSV version**: 3.3.5
**I18n version**: 1.14.7
## Additional information
* This issue does not occur in CSV v3.2.5, and starts occurring from CSV v3.2.6.
* Regression seems to start from this [commit](https://github.com/ruby/csv/commit/acc05116c55579b78925dd27024969b9daf69135#diff-3d24e8ebe77fb6a7f68bee82e4470b8bdc39ced1859ef6697f671e10daff7417R2595)
* **Possible cause**:
* The issue occurs because CSV v3.2.6 changed from direct enumeration (`parser_enumerator.each`) to Fiber-based enumeration (`parser_enumerator.next` in a loop).
* I18n stores locale configuration in Thread.current[:i18n_config]. However, when Fibers execute in a new thread context, this thread-local storage gets reset to default values, causing `I18n.locale` to revert from `:en` to `:ja` (default_locale) during header_converters execution.
关闭于 2025-10-03 5 条评论