ractor-rails-shim 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +101 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +265 -0
  5. data/exe/ractor-rails-check +39 -0
  6. data/lib/ractor_rails_shim/check.rb +190 -0
  7. data/lib/ractor_rails_shim/fallback_ies.rb +33 -0
  8. data/lib/ractor_rails_shim/patches/action_controller.rb +301 -0
  9. data/lib/ractor_rails_shim/patches/action_dispatch.rb +851 -0
  10. data/lib/ractor_rails_shim/patches/action_view.rb +582 -0
  11. data/lib/ractor_rails_shim/patches/active_model_attribute.rb +97 -0
  12. data/lib/ractor_rails_shim/patches/active_record_model_schema.rb +58 -0
  13. data/lib/ractor_rails_shim/patches/active_support.rb +1085 -0
  14. data/lib/ractor_rails_shim/patches/activerecord.rb +1727 -0
  15. data/lib/ractor_rails_shim/patches/class_attribute.rb +194 -0
  16. data/lib/ractor_rails_shim/patches/core.rb +806 -0
  17. data/lib/ractor_rails_shim/patches/devise.rb +172 -0
  18. data/lib/ractor_rails_shim/patches/execution_wrapper.rb +76 -0
  19. data/lib/ractor_rails_shim/patches/kaminari.rb +82 -0
  20. data/lib/ractor_rails_shim/patches/make_shareable.rb +853 -0
  21. data/lib/ractor_rails_shim/patches/mattr_accessor.rb +162 -0
  22. data/lib/ractor_rails_shim/patches/openssl.rb +58 -0
  23. data/lib/ractor_rails_shim/patches/orm_adapter.rb +32 -0
  24. data/lib/ractor_rails_shim/patches/polymorphic_routes.rb +115 -0
  25. data/lib/ractor_rails_shim/patches/propshaft.rb +110 -0
  26. data/lib/ractor_rails_shim/patches/rack.rb +160 -0
  27. data/lib/ractor_rails_shim/patches/rails_module.rb +192 -0
  28. data/lib/ractor_rails_shim/patches/route_helpers.rb +119 -0
  29. data/lib/ractor_rails_shim/patches/rubygems.rb +65 -0
  30. data/lib/ractor_rails_shim/patches/url_helpers.rb +78 -0
  31. data/lib/ractor_rails_shim/patches/warden.rb +195 -0
  32. data/lib/ractor_rails_shim/patches/zeitwerk_registry.rb +124 -0
  33. data/lib/ractor_rails_shim/patches.rb +67 -0
  34. data/lib/ractor_rails_shim/version.rb +5 -0
  35. data/lib/ractor_rails_shim/version_check.rb +88 -0
  36. data/lib/ractor_rails_shim.rb +33 -0
  37. metadata +104 -0
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ActiveRecord caches a number of values in class-level ivars via `@ivar ||=`
4
+ # (ModelSchema::ClassMethods). Several of these hold UNshareable values
5
+ # (AttributeSet::Builder, AttributeSet::YAMLEncoder, Hashes of Attribute
6
+ # objects) or take arguments, so neither pre-warming nor `Ractor.make_shareable`
7
+ # makes them safe for worker Ractors: a worker reading the class ivar hits
8
+ # Ractor::IsolationError (unshareable value) and writing it (the `||=`) hits
9
+ # "can not set instance variables of classes/modules by non-main Ractors".
10
+ #
11
+ # We redirect these specific caches to per-Ractor storage
12
+ # (ActiveSupport::IsolatedExecutionState), keyed by the class. Each worker
13
+ # Ractor computes and keeps its OWN copy — it never touches the shared class
14
+ # ivar, so there is no cross-boundary (unshareable) value and no class-ivar
15
+ # write. The values are deterministic from the schema/connection, so per-Ractor
16
+ # caching is behavior-preserving. In the main Ractor this behaves identically
17
+ # (compute once, cache).
18
+
19
+ module RactorRailsShim
20
+ module ActiveRecordModelSchemaPatch
21
+ def self.prepended(base)
22
+ base.prepend(InstanceMethods)
23
+ end
24
+
25
+ module InstanceMethods
26
+ def _returning_columns_for_insert(connection) # :nodoc:
27
+ key = :"rrs_returning_cols_#{object_id}"
28
+ ActiveSupport::IsolatedExecutionState[key] ||= begin
29
+ auto_populated_columns = columns.filter_map do |c|
30
+ c.name if connection.return_value_after_insert?(c)
31
+ end
32
+
33
+ auto_populated_columns.empty? ? Array(primary_key) : auto_populated_columns
34
+ end
35
+ end
36
+
37
+ def attributes_builder # :nodoc:
38
+ key = :"rrs_attributes_builder_#{object_id}"
39
+ ActiveSupport::IsolatedExecutionState[key] ||= begin
40
+ defaults = _default_attributes.except(*(column_names - [primary_key]))
41
+ ::ActiveModel::AttributeSet::Builder.new(attribute_types, defaults)
42
+ end
43
+ end
44
+
45
+ def column_defaults # :nodoc:
46
+ key = :"rrs_column_defaults_#{object_id}"
47
+ ActiveSupport::IsolatedExecutionState[key] ||=
48
+ _default_attributes.deep_dup.to_hash.freeze
49
+ end
50
+
51
+ def yaml_encoder # :nodoc:
52
+ key = :"rrs_yaml_encoder_#{object_id}"
53
+ ActiveSupport::IsolatedExecutionState[key] ||=
54
+ ::ActiveModel::AttributeSet::YAMLEncoder.new(attribute_types)
55
+ end
56
+ end
57
+ end
58
+ end