Add ActiveSupport::Ractors.try_make_shareable

rails/rails

Rails now provides ActiveSupport::Ractors.try_make_shareable, a helper that attempts to make arbitrary objects Ractor‑shareable while letting applications choose how isolation errors are handled.

背景

Ractor safety has been a recurring requirement for Rails core and applications alike, especially when objects other than procs cross thread‑like boundaries. Existing support focused on procs via try_shareable_proc, but many code paths still raise Ractor::IsolationError when non‑shareable objects are encountered. The PR addresses this gap by introducing a generic mechanism that respects the existing configuration flag unshareable_proc_action.

This change prepares Rails for broader Ractor adoption without breaking existing behaviour, matching the motivation described in the related PR #57626.

技術的な変更

The core addition lives in activesupport/lib/active_support/ractors.rb, where a new method try_make_shareable is defined. The method returns the original object when unshareable_proc_action is falsy, otherwise it delegates to make_shareable and rescues Ractor::IsolationError.

def try_make_shareable(obj)
  return obj unless unshareable_proc_action

  make_shareable(obj)
rescue Ractor::IsolationError
  case unshareable_proc_action
  when :raise
    raise
  when :warn
    ActiveSupport.deprecator.warn(<<~MSG)
      Rails attempted to make an object from your application Ractor shareable but a Ractor
      Isolation error was raised. The object being returned is not Ractor safe and a runtime
      error may occur anytime during the request lifecycle.

      #{obj.inspect}
    MSG
    obj
  end
end

The corresponding test suite activesupport/test/ractors_test.rb adds several cases to verify behaviour under nil, :raise, and :warn settings. For example, when unshareable_proc_action is :raise, the method raises Ractor::IsolationError for an unshareable proc, while :warn emits a deprecation warning and returns the original object.

def test_try_make_shareable_when_action_is_nil
  old = ActiveSupport::Ractors.unshareable_proc_action
  ActiveSupport::Ractors.unshareable_proc_action = nil

  obj = +"hello"
  attempted = ActiveSupport::Ractors.try_make_shareable(obj)

  assert_same(obj, attempted)
  assert_not ActiveSupport::Ractors.shareable?(obj)
ensure
  ActiveSupport::Ractors.unshareable_proc_action = old
end

These additions are isolated to the ActiveSupport::Ractors module; no other files are modified.

設計判断

The implementation extends the existing unshareable_proc_action flag rather than introducing a new configuration key. This decision preserves backward compatibility and keeps the developer‑facing API surface minimal, as discussed in the PR description.

By handling the three possible values (:raise, :warn, nil) within a single method, the design centralises Ractor‑shareability logic and avoids scattering error‑handling code across multiple callers. The :warn path leverages ActiveSupport.deprecator.warn, integrating with Rails' standard deprecation infrastructure.

Choosing to raise, warn, or silently ignore aligns with the established behaviour for proc shareability, providing a consistent experience for developers transitioning to Ractor‑safe code.

まとめ

try_make_shareable equips Rails applications with a unified way to attempt Ractor‑shareability for any object, while honouring the configurable unshareable_proc_action policy. This addition broadens Ractor support without disrupting existing applications.

記事メタデータ

Generated by:
gpt-oss-120b for DiffDaily
LLM Trace:
dc3f9981

この記事はAIによって自動生成されています。内容の正確性については、必ずソースコードやPRを確認してください。

品質レビュー結果

Review Status:
承認済み
Review Count:
1回
Reviewed by:
gpt-oss-120b for DiffDaily

Review Criteria:

記事構成 ✓ PASS

Title, Context, Technical Detailの存在と明確さ

リード文 → 背景 → 技術的な変更 → 設計判断(任意) → まとめの流れが全て揃っており、各セクションが明確に区分されています。

カスタムMarkdown構文 ✓ PASS

シンタックスハイライト・GitHubリンク記法の正確性

ファイル名付きシンタックスハイライトは正しい形式 (```ruby:パス) で記述され、GitHubリンクはPR番号・PRへのリンクが正しくマークダウン化されています。

対象読者への適合性 ✓ PASS

エンジニア向けの適切な技術レベルと表現

対象はRailsエンジニアで、専門的な用語と実装詳細に焦点を当てており、初心者向けの過剰な解説はありません。

パラグラフ・ライティング ✓ PASS

トピックセンテンス・1段落1トピック・段落長

各セクションは総論・各論・結論の構成になっており、段落は1トピック・トピックセンテンスで始まり、長さも適切です。空行で区切られています。

Diff内容との照合 ✓ PASS

コードブロックとDiff内容の一致

記事内のコードブロックはDiffに含まれる実装と一致しています。コメント行の省略は情報の欠損ではなく、実装ロジックは正確に反映されています。

技術用語の正確性 ✓ PASS

技術用語の正確な使用

Ractor::IsolationError、unshareable_proc_action、ActiveSupport.deprecator.warn などの用語はPRで使用されているものと一致し、誤用はありません。

説明の技術的正確性 ✓ PASS

技術的主張の正確性と論理性

メソッドの挙動(:raise、:warn、nil の処理)やテストの目的がPRの記述と合致しており、技術的に正確です。

事実の突合 ✓ PASS

PR情報による主張の裏付け(ハルシネーション検出)

記事の全主張はPRタイトル、説明、Diffに裏付けられており、推測や根拠のない記述はありません。

数値・固有名詞の確認 ✓ PASS

PR番号・コミットID・バージョン等の正確性

PR番号 #57961 と関連PR #57626 の記載は正しく、他の数値や固有名詞の誤りは見られません。

タイトル・説明との一致 ✓ PASS

記事タイトル・説明とPR内容の一致

記事タイトルはPRタイトルと完全に一致しています。

外部知識の正確性 ✓ PASS

PRに記載のない外部知識(LTS、サポート状況など)の不使用

Railsのバージョンサポートやリリース日程など、PRに記載されていない外部知識は一切含まれていません。

時間表現の正確性 ✓ PASS

時間表現がPR情報と一致しているか

時間表現の歪曲はなく、PRの記述と整合しています。