Issue #22216 has been updated by ioquatix (Samuel Williams). @headius You're right - my bad — the isolation there is incidental, not a guarantee. If the frame is captured as a shared proc, threads race on it badly. But I think that actually restates my point rather than refuting it. There is no consistent "svars live in the method frame" rule to preserve today: - within a single method frame: fine; - shared across threads via a captured proc: shared *and* racy (#note-17); - across an enumerator's fiber: bound to the block's *lexical* definition site (which, as you agree, is exactly the problem); - under a fiber scheduler: different again. So there isn't a coherent status quo we'd be protecting. Given that, I'd rather not introduce a new `transparent:` API to bless the leaky path, especially if it needs the non-transferable-fiber restriction that breaks fiber schedulers. Plain EC-local (as in @jhawthorn's PR) seems like the best near-term default: it's the one rule that's simple to reason about, it's no worse than anything we have now, and it doesn't require any special-casing of fibers or a new fiber mode. If anything, it makes the concurrency story strictly less surprising than the current mix. ---------------------------------------- Bug #22216: Special variables (ex. Regexp backref and IO lastline) are thread-unsafe in some cases, incompatible with Ractor https://bugs.ruby-lang.org/issues/22216#change-119076 * Author: headius (Charles Nutter) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- ## Problem Several Regexp-matching methods currently write (and sometimes read) the implicit "backref" `$~` variable in the local frame (and related variables like `$'`). Several IO methods read or write the "last line" `$_` variable in the same way. In both cases, the result is a mutable object, which makes these variables already problematic for ractors. Making matters worse, the frame might be shared if a proc is captured and used across threads or ractors, and there's no static way to inspect a piece of code to know if it expects to read or write these variables. Where procs can be rejected by a proc for accessing captured state, there's no such check possible for these variables. All of these facts make the backref and lastline variables fundamentally incompatible with Ractor. ## Possible remedies A wholesale removal of these variables would solve the problem, but there's a lot of code that depends on them... much of that code without even realizing it, since they might not access the variables directly. In some cases, the dependencies are internal and part of the behavior of core methods. Hard errors when using methods that read or write these variables would avoid introducing threading problems into a Ractor, but would also break a large number of commonly-used methods. There have been experiments to make these variables both frame and thread-local, but they have never been made standard. Updates to backref and lastline are visible across threads and already can lead to concurrency issues even on CRuby. Deprecating the implicit behavior and making it opt-in (or opt-out?), perhaps with keyword arguments or file pragmas, might be a halfway measure. It would probably not be an easy transition. I don't know the right path forward, but I believe this issue needs to be discussed. ## JRuby perspective We continue to mimic CRuby behavior, which has led to our users occasionally running into issues when a proc accesses these variables across threads. Our recommendation: "don't do that". We also have had our frustrations optimizing around these variables, since they implicitly require access across calls. Because we cannot statically detect when they will be used, we essentially treat all method names that might *potentially* access them as deopt triggers. It's not ideal. I'd like to hear ideas for how to make these variables less "magic", less implicit and easier to deal with across calls (and across threads/ractors). -- https://bugs.ruby-lang.org/