Project

General

Profile

Actions

Bug #22623

open

Ractor.main.freeze from another Ractor is allowed and races with the owner

Bug #22623: Ractor.main.freeze from another Ractor is allowed and races with the owner

Added by Eregon (Benoit Daloze) 13 minutes ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:127670]

Description

Summary

Kernel#freeze can be called on a Ractor object owned by another Ractor, and succeeds:

Ractor.new { Ractor.main.freeze }.value
Ractor.main.frozen? # => true

This is a write to another Ractor's object, which the other write paths refuse:

Ractor.new { Foo.freeze }.value
# => Ractor::IsolationError: can not modify Foo because it is created by another Ractor
Ractor.new { Ractor.main.instance_variable_set(:@x, 1) }.value
# => Ractor::IsolationError

Module#freeze goes through rb_class_owner_check(), instance_variable_set is guarded too, but Kernel#freeze on a Ractor object is not.

I propose Ractor#freeze raises TypeError, like ENV.freeze and Thread::Queue#freeze.

Why it is a data race, not just a useless flag

rb_obj_freeze_inline() does two plain read-modify-writes of RBasic#flags: RB_FL_SET_RAW(x, RUBY_FL_FREEZE), and RBASIC_SET_SHAPE_ID() for the frozen shape transition (the shape id is stored in the upper bits of the same word). Neither is atomic.

The owning Ractor may be writing that word at the same time, legitimately: Ractor.current.instance_variable_set(:@x, ...) transitions the shape (and may replace the fields object when capacity is exhausted or the shape becomes complex), ObjectSpace.define_finalizer sets FL_FINALIZE, etc. Possible outcomes of the lost update:

  • the freeze is lost;
  • the owner's new shape is lost while the ivar value was already stored: the ivar silently disappears, with no error anywhere;
  • the freezer stores a shape derived from the old one after the owner replaced the fields object for a larger or complex layout, so the shape and the fields object disagree. Readers then index the fields with the wrong layout, which can read out of bounds or interpret a hash table as an array, i.e. memory corruption and a possible crash.

All of these need a tight interleaving, which makes them rare and hard to diagnose rather than harmless.

Ractor objects are the only objects on which this is reachable in practice: the other shareable-but-unfrozen objects are classes and modules (guarded) and ENV (whose freeze raises before writing).

Current semantics of Ractor#freeze

Even locally, freezing a Ractor has no effect. Nothing checks the frozen flag:

r = Ractor.current
r.freeze
r[:a] = 1 # works, unlike Thread#[]= on a frozen Thread which raises FrozenError

So raising instead of silently doing nothing loses nothing.

Proposal

Ractor.current.freeze # => TypeError (cannot freeze #<Ractor:#1 running>)

Precedents: ENV.freeze raises TypeError ("cannot freeze ENV"), Thread::Queue#freeze and Thread::SizedQueue#freeze raise TypeError too. A Ractor is a live execution context with process-level state, in the same category.

An alternative would be to raise Ractor::IsolationError only when the Ractor is owned by another Ractor and keep the no-op locally, but since the local freeze has no meaning either, TypeError in both cases is simpler and more honest.

Relation to Kernel#deep_freeze (#21962)

Kernel#deep_freeze (https://github.com/ruby/ruby/pull/19529) can reach a Ractor object, for example through a Ractor::Port whose mark function references its Ractor, or through a shareable Array containing one, possibly from a different Ractor. For now deep_freeze raises TypeError itself when it reaches a Ractor, as if Ractor#freeze raised. Defining Ractor#freeze to raise would make Kernel#freeze and Kernel#deep_freeze consistent and remove that special case.

Compatibility

Calling freeze on a Ractor has no effect today, so nothing can depend on it. Generic code freezing arbitrary objects already has to cope with ENV and Thread::Queue raising.

No data to display

Actions

Also available in: PDF Atom