[#120855] [Ruby master Bug#21104] Net::HTTP connections failing in Ruby >= 3.4.0 on macOS with Happy Eyeballs enabled — "mjt58 (Mike Thompson) via ruby-core" <ruby-core@...>

Issue #21104 has been reported by mjt58 (Mike Thompson).

14 messages 2025/02/01

[#120873] [Ruby master Bug#21111] RbConfig::CONFIG['CXX'] quietly set to "false" when Ruby cannot build C++ programs — "stanhu (Stan Hu) via ruby-core" <ruby-core@...>

Issue #21111 has been reported by stanhu (Stan Hu).

10 messages 2025/02/03

[#120884] [Ruby master Bug#21115] Etc.getgrgid is not Ractor-safe but is marked as such — "Eregon (Benoit Daloze) via ruby-core" <ruby-core@...>

Issue #21115 has been reported by Eregon (Benoit Daloze).

7 messages 2025/02/05

[#120897] [Ruby master Bug#21119] Programs containing `Dir.glob` with a thread executing a CPU-heavy task run very slowly. — "genya0407 (Yusuke Sangenya) via ruby-core" <ruby-core@...>

Issue #21119 has been reported by genya0407 (Yusuke Sangenya).

6 messages 2025/02/06

[#121054] [Ruby master Bug#21139] Prism and parse.y parses `it = it` differently — "tompng (tomoya ishida) via ruby-core" <ruby-core@...>

Issue #21139 has been reported by tompng (tomoya ishida).

19 messages 2025/02/14

[#121060] [Ruby master Feature#21140] Add a method to get the address of certain JIT related functions — "tenderlovemaking (Aaron Patterson) via ruby-core" <ruby-core@...>

Issue #21140 has been reported by tenderlovemaking (Aaron Patterson).

23 messages 2025/02/14

[#121077] [Ruby master Misc#21143] Speficy order of execution const_added vs inherited — "fxn (Xavier Noria) via ruby-core" <ruby-core@...>

Issue #21143 has been reported by fxn (Xavier Noria).

15 messages 2025/02/17

[#121142] [Ruby master Misc#21154] Document or change Module#autoload? — "fxn (Xavier Noria) via ruby-core" <ruby-core@...>

Issue #21154 has been reported by fxn (Xavier Noria).

32 messages 2025/02/23

[#121172] [Ruby master Feature#21157] Comparison operator <> — lpogic via ruby-core <ruby-core@...>

SXNzdWUgIzIxMTU3IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGxwb2dpYyAoxYF1a2FzeiBQb21pZXTF

11 messages 2025/02/26

[ruby-core:120877] [Ruby master Feature#21033] Allow lambdas that don't access `self` to be Ractor shareable

From: "osyoyu (Daisuke Aritomo) via ruby-core" <ruby-core@...>
Date: 2025-02-04 17:20:57 UTC
List: ruby-core #120877
Issue #21033 has been updated by osyoyu (Daisuke Aritomo).


Nice patch! I feel this makes Ractor programming practical.

ObjectSpace.each_object could effectively leak `self` after it has been marked as shareable, but I'm not sure if that's a real problem... Maybe it is ok since `self` won't show up in other Ractors?

```ruby
class Foo
  def make_lambda
    lambda {
      ObjectSpace.each_object do |obj|
        p obj if obj.class == Foo
      end
    }
  end
end
```

----------------------------------------
Feature #21033: Allow lambdas that don't access `self` to be Ractor shareable
https://bugs.ruby-lang.org/issues/21033#change-111752

* Author: tenderlovemaking (Aaron Patterson)
* Status: Open
----------------------------------------
Hi,

I would like to allow lambdas that don't access `self` to be eligible for Ractor shareability regardless of the shareability status of `self`.

Consider the following code:

```ruby
class Foo
  def make_lambda
    x = 123
    lambda { x }
  end
end

Ractor.make_shareable(Foo.new.make_lambda)
```

With Ruby 3.4.X, this will raise an exception.  The reason is because `self`, which is an unfrozen instance of `Foo`, is not shareable.  However, we can see from the code that the lambda doesn't access `self`.  I would like to make lambdas such as the ones above eligible for shareability, and I've submitted a patch [here](https://github.com/ruby/ruby/pull/12567).

I think we can detect access to `self` by scanning the instructions in the lambda.  Any references to `putself`, `getinstancevariable`, or `setinstancevariable` will result in using the default behavior (checking the frozen status of `self`).

## Considerations

### What about `eval`?

I think that `eval` is not a problem because calling eval has an implicit reference to `self`:

```
$ ./miniruby --dump=insns -e 'lambda { eval("123") }'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,22)>
0000 putself                                                          (   1)[Li]
0001 send                                   <calldata!mid:lambda, argc:0, FCALL>, block in <main>
0004 leave

== disasm: #<ISeq:block in <main>@-e:1 (1,7)-(1,22)>
0000 putself                                                          (   1)[LiBc]
0001 putchilledstring                       "123"
0003 opt_send_without_block                 <calldata!mid:eval, argc:1, FCALL|ARGS_SIMPLE>
0005 leave                                  [Br]
```

If we try to call `eval` inside the lambda, there will be an implicit `putself` instruction added which means we will fall back to the old behavior.

### What about `binding`?

If you call `binding` from inside the `lambda` there will be a `putself` instruction so we fall back to the old behavior.  This is the same as the `eval` case.

### What about `binding` via a local?

If you assign `binding` to a local, shareability will fail because the `lambda` references an unshareable local:

```ruby
class Foo
  def make_lambda
    x = binding
    lambda { x }
  end
end

b = Foo.new.make_lambda
# exception because local `x` is not shareable
Ractor.make_shareable(b)
```

### What about accessing `binding` via the proc itself?

The lambda can references itself via a local and access binding, but again this will fail isolation when locals are scanned:

```ruby
class Foo
  def make_lambda
    x = lambda { x.binding.eval("self") }
  end
end

b = Foo.new.make_lambda
# exception because local `x` is not shareable
Ractor.make_shareable(b)
```

I _think_ I've covered all cases where `self` can possibly escape.  I would appreciate any feedback.

Again, [here is the patch](https://github.com/ruby/ruby/pull/12567).

Thanks.

---Files--------------------------------
0001-Allow-lambdas-that-don-t-access-self-to-be-made-shar.patch (6.51 KB)


-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/


In This Thread

Prev Next