ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
@ 2020-12-01  8:29 marcandre-ruby-core
  2020-12-01  9:14 ` [ruby-core:101175] " ko1
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: marcandre-ruby-core @ 2020-12-01  8:29 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been reported by marcandre (Marc-Andre Lafortune).

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101175] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
@ 2020-12-01  9:14 ` ko1
  2020-12-01  9:23 ` [ruby-core:101176] " marcandre-ruby-core
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ko1 @ 2020-12-01  9:14 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by ko1 (Koichi Sasada).


mmm, should not call `clone` here?


----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88861

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101176] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
  2020-12-01  9:14 ` [ruby-core:101175] " ko1
@ 2020-12-01  9:23 ` marcandre-ruby-core
  2020-12-01 13:09 ` [ruby-core:101178] " daniel
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: marcandre-ruby-core @ 2020-12-01  9:23 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by marcandre (Marc-Andre Lafortune).


ko1 (Koichi Sasada) wrote in #note-1:
> mmm, should not call `clone` here?

Sorry, I don't understand. Above code should be same with `initialize_copy` => `initialize_clone`, if that's what you mean?

The issue I'm pointing out is that one can register the new copy in the initial Ractor and use that reference later to mutate the object.

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88862

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101178] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
  2020-12-01  9:14 ` [ruby-core:101175] " ko1
  2020-12-01  9:23 ` [ruby-core:101176] " marcandre-ruby-core
@ 2020-12-01 13:09 ` daniel
  2020-12-01 17:30 ` [ruby-core:101183] " ko1
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: daniel @ 2020-12-01 13:09 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by Dan0042 (Daniel DeLorme).


The `initialize_copy` is executed in the main ractor, that's why it can set a global variable. It should be executed in the sub-ractor I think.

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88864

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101183] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (2 preceding siblings ...)
  2020-12-01 13:09 ` [ruby-core:101178] " daniel
@ 2020-12-01 17:30 ` ko1
  2020-12-01 17:36 ` [ruby-core:101184] " ko1
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ko1 @ 2020-12-01 17:30 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by ko1 (Koichi Sasada).


Dan0042 (Daniel DeLorme) wrote in #note-3:
> The `initialize_copy` is executed in the main ractor, that's why it can set a global variable. It should be executed in the sub-ractor I think.

It is not acceptable because the src object is accessed from main and sub (`initialize_copy`) simultaneously.


----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88865

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101184] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (3 preceding siblings ...)
  2020-12-01 17:30 ` [ruby-core:101183] " ko1
@ 2020-12-01 17:36 ` ko1
  2020-12-01 17:53 ` [ruby-core:101187] " ko1
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ko1 @ 2020-12-01 17:36 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by ko1 (Koichi Sasada).


marcandre (Marc-Andre Lafortune) wrote in #note-2:
> The issue I'm pointing out is that one can register the new copy in the initial Ractor and use that reference later to mutate the object.

I couldn't guess that the src ractor can accesses copied object with `initialize_copy`.



----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88866

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101187] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (4 preceding siblings ...)
  2020-12-01 17:36 ` [ruby-core:101184] " ko1
@ 2020-12-01 17:53 ` ko1
  2020-12-01 17:59 ` [ruby-core:101188] " eregontp
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ko1 @ 2020-12-01 17:53 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by ko1 (Koichi Sasada).


One idea is prohibit `initialize_copy` written in Ruby, for ractor_copy.
But I'm not sure how it is feasible.

```
ko1@aluminium:~$ gem-codesearch 'def initialize_clone' | wc -l
120
ko1@aluminium:~$ gem-codesearch 'def initialize_copy' | wc -l
3430
```


----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88868

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101188] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (5 preceding siblings ...)
  2020-12-01 17:53 ` [ruby-core:101187] " ko1
@ 2020-12-01 17:59 ` eregontp
  2020-12-01 18:09 ` [ruby-core:101189] " ko1
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: eregontp @ 2020-12-01 17:59 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by Eregon (Benoit Daloze).


An idea: copy everything in the source Ractor as currently, and then `move` the resulting copied object.
That way, the source Ractor might keep references to copied objects, but it won't be able to use them.
This actually means a partial copy for `move` on top of the original deep copy though.

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88869

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101189] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (6 preceding siblings ...)
  2020-12-01 17:59 ` [ruby-core:101188] " eregontp
@ 2020-12-01 18:09 ` ko1
  2020-12-01 18:29 ` [ruby-core:101190] " marcandre-ruby-core
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ko1 @ 2020-12-01 18:09 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by ko1 (Koichi Sasada).


Eregon (Benoit Daloze) wrote in #note-7:
> An idea: copy everything in the source Ractor as currently, and then `move` the resulting copied object.
> That way, the source Ractor might keep references to copied objects, but it won't be able to use them.
> This actually means a partial copy for `move` on top of the original deep copy though.

Complete idea, but it makes two objects per one. mmm.

Doing it if user-defined `initialize_copy/clone` is detected?

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88870

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101190] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (7 preceding siblings ...)
  2020-12-01 18:09 ` [ruby-core:101189] " ko1
@ 2020-12-01 18:29 ` marcandre-ruby-core
  2020-12-01 18:44 ` [ruby-core:101191] " ko1
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: marcandre-ruby-core @ 2020-12-01 18:29 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by marcandre (Marc-Andre Lafortune).


I feel it is important not to break the contract of `initialize_copy` (or otherwise replace it with another one, but that doesn't solve the problem).

Nobody commented on my idea: make the deep copy as currently (in the current Ractor), then move it to the new Ractor (like `send(new_copy, move: true)`)

```ruby 
Ractor.new(obj) { |copy| ... }
# equivalent to
copy = deep_clone(obj)
r = Ractor.new { copy = Ractor.receive ... }
r.send(copy, move: true)
```

My example code above would (correctly) raise an exception when calling attempting to mutate the copy from the wrong Ractor.

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88871

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101191] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (8 preceding siblings ...)
  2020-12-01 18:29 ` [ruby-core:101190] " marcandre-ruby-core
@ 2020-12-01 18:44 ` ko1
  2020-12-01 20:40 ` [ruby-core:101194] " marcandre-ruby-core
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ko1 @ 2020-12-01 18:44 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by ko1 (Koichi Sasada).


> Nobody commented on my idea: make the deep copy as currently (in the current Ractor), then move it to the new Ractor (like send(new_copy, move: true))

same as https://bugs.ruby-lang.org/issues/17359#note-7 ?

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88872

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101194] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (9 preceding siblings ...)
  2020-12-01 18:44 ` [ruby-core:101191] " ko1
@ 2020-12-01 20:40 ` marcandre-ruby-core
  2020-12-01 22:01 ` [ruby-core:101195] " daniel
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: marcandre-ruby-core @ 2020-12-01 20:40 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by marcandre (Marc-Andre Lafortune).


ko1 (Koichi Sasada) wrote in #note-10:
> same as https://bugs.ruby-lang.org/issues/17359#note-7 ?

Ohh, sorry 😅 I got confused because I didn't understand how moving worked. Now I understand better. So yes, there would be a performance penalty involved 😢. And yes, only if user-defined `initialize_copy`/`clone` is detected.

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88875

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101195] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (10 preceding siblings ...)
  2020-12-01 20:40 ` [ruby-core:101194] " marcandre-ruby-core
@ 2020-12-01 22:01 ` daniel
  2020-12-01 22:10 ` [ruby-core:101196] " marcandre-ruby-core
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: daniel @ 2020-12-01 22:01 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by Dan0042 (Daniel DeLorme).


I was under the impression that ractor-copy worked via dump+load sort of like marshaling, in fact I thought that was the basis for #17298 basket communication APIs. Is that not (no longer?) the case? Because Marshal.load doesn't call `initialize_copy` so it seems like the same rules should apply here.

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88876

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101196] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (11 preceding siblings ...)
  2020-12-01 22:01 ` [ruby-core:101195] " daniel
@ 2020-12-01 22:10 ` marcandre-ruby-core
  2020-12-02 14:00 ` [ruby-core:101202] " daniel
  2020-12-02 17:42 ` [ruby-core:101206] " marcandre-ruby-core
  14 siblings, 0 replies; 16+ messages in thread
From: marcandre-ruby-core @ 2020-12-01 22:10 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by marcandre (Marc-Andre Lafortune).


THis was changed to actual traversal and cloning. This way any intermediate object that is shareable is simply shared. Other objects were also not marshalable but can be cloned.

----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88877

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101202] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (12 preceding siblings ...)
  2020-12-01 22:10 ` [ruby-core:101196] " marcandre-ruby-core
@ 2020-12-02 14:00 ` daniel
  2020-12-02 17:42 ` [ruby-core:101206] " marcandre-ruby-core
  14 siblings, 0 replies; 16+ messages in thread
From: daniel @ 2020-12-02 14:00 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by Dan0042 (Daniel DeLorme).


ko1 (Koichi Sasada) wrote in #note-6:
> One idea is prohibit `initialize_copy` written in Ruby, for ractor_copy.
> But I'm not sure how it is feasible.
> 
> ```
> ko1@aluminium:~$ gem-codesearch 'def initialize_clone' | wc -l
> 120
> ko1@aluminium:~$ gem-codesearch 'def initialize_copy' | wc -l
> 3430
> ```

I think it's feasible. `initialize_clone` and `initialize_copy` are mostly (only?) used because `clone` and `dup` only perform shallow copies. See the examples below. Since ractor-copy already performs a deep-copy, the extra copy/cloning in these examples is redundant. Actually... it's buggy? Since the `initialize_clone` is executed in the parent ractor, the objects are created as part of the parent ractor's heap rather than the child ractor's heap?


```
activemodel-6.0.0/lib/active_model/attribute_set.rb
72:    def initialize_clone(_)
73-      @attributes = attributes.clone
74-      super
75-    end

regexp_parser-1.6.0/lib/regexp_parser/expression/classes/group.rb
36:      def initialize_clone(orig)
37-        @name = orig.name.dup
38-        super
39-      end
40-    end

regexp_parser-1.6.0/lib/regexp_parser/expression/quantifier.rb
15:    def initialize_clone(orig)
16-      @text = orig.text.dup
17-      super
18-    end

regexp_parser-1.6.0/lib/regexp_parser/expression/subexpression.rb
15:    def initialize_clone(orig)
16-      self.expressions = orig.expressions.map(&:clone)
17-      super
18-    end

regexp_parser-1.6.0/lib/regexp_parser/expression.rb
24:    def initialize_clone(orig)
25-      self.text       = (orig.text       ? orig.text.dup         : nil)
26-      self.options    = (orig.options    ? orig.options.dup      : nil)
27-      self.quantifier = (orig.quantifier ? orig.quantifier.clone : nil)
28-      super
29-    end

actionpack-6.0.0/lib/action_controller/metal/strong_parameters.rb
1017:      def initialize_copy(source)
1018-        super
1019-        @parameters = @parameters.dup
1020-      end

actionpack-6.0.0/lib/action_dispatch/http/content_security_policy.rb
157:    def initialize_copy(other)
158-      @directives = other.directives.deep_dup
159-    end

actionpack-6.0.0/lib/action_dispatch/middleware/flash.rb
147:      def initialize_copy(other)
148-        if other.now_is_loaded?
149-          @now = other.now.dup
150-          @now.flash = self
151-        end
152-        super
153-      end

actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb
95:    def initialize_copy(other)
96-      self.middlewares = other.middlewares.dup
97-    end

actionview-6.0.0/lib/action_view/path_set.rb
22:    def initialize_copy(other)
23-      @paths = other.paths.dup
24-      self
25-    end
```


----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88886

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [ruby-core:101206] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe
  2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
                   ` (13 preceding siblings ...)
  2020-12-02 14:00 ` [ruby-core:101202] " daniel
@ 2020-12-02 17:42 ` marcandre-ruby-core
  14 siblings, 0 replies; 16+ messages in thread
From: marcandre-ruby-core @ 2020-12-02 17:42 UTC (permalink / raw
  To: ruby-core

Issue #17359 has been updated by marcandre (Marc-Andre Lafortune).


Dan0042 (Daniel DeLorme) wrote in #note-14:
> I think it's feasible. `initialize_clone` and `initialize_copy` are mostly (only?) used because `clone` and `dup` only 

Indeed, I imagine that this will be most of the cases, as of today (i.e. before Ractor).

My concern is to have some way for user classes to handle special cases of deep-cloning for Ractor, in the same way that calling `freeze` from `Ractor.make_shareable` provides a basic way to handle deep-freezing. Without one, and without `Ractor::LVar`, there would be no way to handle these special cases.

Or maybe we can begin without it and we can revisit as use-cases arise, I don't know.


----------------------------------------
Bug #17359: Ractor copy mode is not Ractor-safe
https://bugs.ruby-lang.org/issues/17359#change-88890

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-11-30T10:06:25Z master 89774a938a) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
It should not be possible to mutate an object across Ractors, but the copy mode allows it currently:

```ruby
class Foo
  attr_accessor :x

  def initialize_copy(*)
    $last = self
    super
  end
end
o = Foo.new
o.x = 42

r = Ractor.new(o) do |copy|
  puts copy.x # => 42
  Ractor.yield :sync
  Ractor.yield :sync
  puts copy.x # => 666
end
r.take # => :sync
$last.x = 666
r.take # => :sync
r.take
```

Maybe the `copy` object should be marked as moved?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2020-12-02 17:42 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-01  8:29 [ruby-core:101174] [Ruby master Bug#17359] Ractor copy mode is not Ractor-safe marcandre-ruby-core
2020-12-01  9:14 ` [ruby-core:101175] " ko1
2020-12-01  9:23 ` [ruby-core:101176] " marcandre-ruby-core
2020-12-01 13:09 ` [ruby-core:101178] " daniel
2020-12-01 17:30 ` [ruby-core:101183] " ko1
2020-12-01 17:36 ` [ruby-core:101184] " ko1
2020-12-01 17:53 ` [ruby-core:101187] " ko1
2020-12-01 17:59 ` [ruby-core:101188] " eregontp
2020-12-01 18:09 ` [ruby-core:101189] " ko1
2020-12-01 18:29 ` [ruby-core:101190] " marcandre-ruby-core
2020-12-01 18:44 ` [ruby-core:101191] " ko1
2020-12-01 20:40 ` [ruby-core:101194] " marcandre-ruby-core
2020-12-01 22:01 ` [ruby-core:101195] " daniel
2020-12-01 22:10 ` [ruby-core:101196] " marcandre-ruby-core
2020-12-02 14:00 ` [ruby-core:101202] " daniel
2020-12-02 17:42 ` [ruby-core:101206] " marcandre-ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).