ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: ko1@atdot.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:101920] [Ruby master Bug#17420] Unsafe mutation of $" when doing non-RubyGems require in Ractor
Date: Tue, 05 Jan 2021 01:39:07 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-89766.20210105013907.772@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17420.20201221183149.772@ruby-lang.org

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


Current `require` behavior with ractors are not well considered and we need to consider it.

> Maybe a solution would be to do all require in the main Ractor?

Yes, This is one good option. But not sure it is only one solution...

If we allow to require from non-main ractors, the only problem is `$LOADED_FEATURES`?

----------------------------------------
Bug #17420: Unsafe mutation of $" when doing non-RubyGems require in Ractor
https://bugs.ruby-lang.org/issues/17420#change-89766

* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 3.0.0dev (2020-12-16T10:12:48Z master a9a7f4d8b8) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
With an empty file `a.rb`:
```
$ ruby --disable-gems -e 'Ractor.new { puts $" }.take'
-e:1:in `block in <main>': can not access global variables $" from non-main Ractors (RuntimeError)
```
That is expected, given the rules for global variables.

```
ruby --disable-gems -e 'Ractor.new { require "./a.rb"; }.take; p $"'
[... , "/home/eregon/a.rb"]
```
Is it OK that the Ractor can do `require`, which does modify `$"`?

I think it's not, and it might lead to segfaults if e.g. the main Ractor mutates `$"` in parallel to some other Ractor doing `require`.

Probably `require` needs to be forbidden in non-main Ractors (it does mutate `$"`, so it's logical), or there needs to be always VM-global synchronization on any access to `$"` (otherwise, segfaults are possible).
The latter doesn't seem reasonable, especially when considering the user might do `$".each { ... }`.

---

Note that RubyGems' `require` does not work on non-main Ractors (pretty much expected given it depends on a lot of global state):
```
$ ruby -e 'Ractor.new { require "./a.rb"; }.take'
<internal:/home/eregon/prefix/ruby-master/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:37:in `require': can not access non-shareable objects in constant Kernel::RUBYGEMS_ACTIVATION_MONITOR by non-main ractor. (NameError)
```

This probably also has consequences for `autoload`.
Maybe the `zeitwerk` gem can help with the mode to resolve all autoload at once.



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

  parent reply	other threads:[~2021-01-05  1:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-21 18:31 [ruby-core:101598] [Ruby master Bug#17420] Unsafe mutation of $" when doing non-RubyGems require in Ractor eregontp
2020-12-27 17:30 ` [ruby-core:101752] " marcandre-ruby-core
2020-12-28  8:43 ` [ruby-core:101770] " duerst
2020-12-28 11:58 ` [ruby-core:101775] " eregontp
2020-12-28 21:27 ` [ruby-core:101798] " shatrov
2021-01-05  1:39 ` ko1 [this message]
2021-01-05  2:16 ` [ruby-core:101922] " marcandre-ruby-core
2021-01-07 13:22 ` [ruby-core:101972] " eregontp
2021-01-07 13:23 ` [ruby-core:101973] " eregontp
2023-01-16 13:46 ` [ruby-core:111834] " Eregon (Benoit Daloze) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-89766.20210105013907.772@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).