Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/console1984/freezeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ def self.included(base)
base.extend ClassMethods

# Flag to control manipulating instance data via instance_variable_get and instance_variable_set.
# true by default.
base.thread_mattr_accessor :prevent_instance_data_manipulation_after_freezing, default: true
# true by default. Stored as a singleton-class instance variable on the host so it survives
# +ActiveSupport::IsolatedExecutionState+ being cleared when Rails switches +isolation_level+
# (e.g. the +:thread+ -> +:fiber+ flip the Rails 8.1 default triggers in +after_initialize+).
# A +mattr_accessor+ would leak the flag across the ancestor chain because +Console1984::Ext::Core::Object+
# is included into +Object+; this storage keeps each host independent.
Comment on lines +26 to +30

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Pretty darn nit-picky mr robot =)

base.singleton_class.class_eval do
attr_writer :prevent_instance_data_manipulation_after_freezing

define_method :prevent_instance_data_manipulation_after_freezing do
return @prevent_instance_data_manipulation_after_freezing if defined?(@prevent_instance_data_manipulation_after_freezing)
true
Comment on lines +26 to +36

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

idk up to maintainer, feels like we are trading a lot of complexity for something that would be rare indeed in a console session?

end
end
end

module ClassMethods
Expand Down
41 changes: 41 additions & 0 deletions test/freezeable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "test_helper"

class FreezeableTest < ActiveSupport::TestCase
test "prevent_instance_data_manipulation_after_freezing defaults to true" do
klass = Class.new { include Console1984::Freezeable }

assert_equal true, klass.prevent_instance_data_manipulation_after_freezing
end

test "prevent_instance_data_manipulation_after_freezing survives ActiveSupport::IsolatedExecutionState being cleared" do
# Reproduces the Rails 8.1 boot symptom: gem eager-load writes the opt-out under one
# +isolation_level+, then Rails switches the level in +after_initialize+ which clears
# the previous scope's storage. The write must survive.
klass = Class.new do
include Console1984::Freezeable
self.prevent_instance_data_manipulation_after_freezing = false
end

ActiveSupport::IsolatedExecutionState.clear

assert_equal false, klass.prevent_instance_data_manipulation_after_freezing
end

test "prevent_instance_data_manipulation_after_freezing is independent per including host" do
one = Class.new { include Console1984::Freezeable }
two = Class.new { include Console1984::Freezeable }

one.prevent_instance_data_manipulation_after_freezing = false

assert_equal false, one.prevent_instance_data_manipulation_after_freezing
assert_equal true, two.prevent_instance_data_manipulation_after_freezing
end

test "Console1984::Ext::Core::Object opt-out does not leak into other Freezeable hosts" do
# Object includes Console1984::Ext::Core::Object, which sets the flag to false. A class-variable-backed
# accessor would propagate that false through Ruby's ancestor chain to every descendant of Object,
# silently disabling the protection on every other Freezeable host.
assert_equal false, Console1984::Ext::Core::Object.prevent_instance_data_manipulation_after_freezing
assert_equal true, Console1984::Config.prevent_instance_data_manipulation_after_freezing
end
end