Skip to content

Commit

Permalink
Fix can't modify frozen Hash error.
Browse files Browse the repository at this point in the history
Fix: #411

The load path cache can be mutated, so we need to unfreeze the hash
when it happens.
  • Loading branch information
byroot committed Mar 8, 2022
1 parent c365322 commit 94e56f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/bootsnap/load_path_cache/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def fetch(key)

v = get(key)
unless v
@dirty = true
v = yield
mark_for_mutation!
@data[key] = v
end
v
Expand All @@ -40,7 +40,7 @@ def set(key, value)
raise(SetOutsideTransactionNotAllowed) unless @txn_mutex.owned?

if value != @data[key]
@dirty = true
mark_for_mutation!
@data[key] = value
end
end
Expand All @@ -59,6 +59,11 @@ def transaction

private

def mark_for_mutation!
@dirty = true
@data = @data.dup if @data.frozen?
end

def commit_transaction
if @dirty
dump_data
Expand Down
6 changes: 6 additions & 0 deletions test/load_path_cache/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def test_modification
assert_equal("c", store3.get("a"))
end

def test_modification_of_loaded_store
store.transaction { store.set("a", "b") }
store = Store.new(@path)
store.transaction { store.set("c", "d") }
end

def test_stores_arrays
store.transaction { store.set("a", [1234, %w(a b)]) }

Expand Down

0 comments on commit 94e56f9

Please sign in to comment.