From: "Eregon (Benoit Daloze)" <noreply@...> Date: 2022-10-18T11:05:25+00:00 Subject: [ruby-core:110394] [Ruby master Bug#19062] Introduce `Fiber#locals` for shared inheritable state. Issue #19062 has been updated by Eregon (Benoit Daloze). > It's not thread unsafe to update the locals in Ruby because of the GVL. We should not design general semantics based on a CRuby-only implementation detail. It is thread unsafe and could crash other Ruby implementations for instance. I agree with what @byroot says. If it's some sort of "local/x-local" then I expect save/restore like `with_log_level` to work correctly, to have mutations isolated per Fiber, etc. > When you say something like "It seems extremely unsafe to inherit by default across threads to me" I would personally like to see the code example where it's unsafe, otherwise it's hard for me to understand exactly what the problem is and/or how we could address it. Example: ```ruby Fiber.current.locals[:db_connection] = some_IO Thread.new { Fiber.current.locals[:db_connection].query(...) } Thread.new { Fiber.current.locals[:db_connection].query(...) } ``` This badly breaks the application because the fd is incorrectly shared between threads. If the name is "something local" I'd think people expect the value to actually be at least Fiber-local. I guess the name is the fundamental problem here. I believe what you propose is not related to what most people understand by "Fiber locals". I think any such sharing should be explicit, e.g.: ```ruby # or maybe with_inherited_state Fiber.with_shared_state({ connection_pool: pool }, inherited_by: Fiber / [Fiber, Thread]) { Thread.new { Fiber.current.shared_state(:connection_pool) # => pool } Fiber.new { Fiber.current.shared_state(:connection_pool) # => pool }.resume } ``` BTW Java has: * https://docs.oracle.com/javase/7/docs/api/java/lang/InheritableThreadLocal.html, i.e., a way to mark which Fiber-local should be inherited * https://download.java.net/java/early_access/loom/docs/api/java.base/java/lang/Thread.Builder.html#allowSetThreadLocals(boolean) which says `Sets whether the thread is allowed to set values for its copy of thread-local variables.`. So the intuitive thing of having a copy per thread. * https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part2.html talks about scoped variables, which seems closer to what you want ---------------------------------------- Bug #19062: Introduce `Fiber#locals` for shared inheritable state. https://bugs.ruby-lang.org/issues/19062#change-99700 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal * Assignee: ioquatix (Samuel Williams) * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- After exploring <https://bugs.ruby-lang.org/issues/19058>, I felt uncomfortable about the performance of copying lots of inheritable attributes. Please review that issue for the background and summary of the problem. ## Proposal Introduce `Fiber#locals` which is a hash table of local attributes which are inherited by child fibers. ```ruby Fiber.current.locals[:x] = 10 Fiber.new do pp Fiber.current.locals[:x] # => 10 end ``` It's possible to reset `Fiber.current.locals`, e.g. ```ruby def accept_connection(peer) Fiber.new(locals: nil) do # This causes a new hash table to be allocated. # Generate a new request id for all fibers nested in this one: Fiber[:request_id] = SecureRandom.hex(32) @app.call(env) end.resume end ``` A high level overview of the proposed changes: ```ruby class Fiber def initialize(..., locals: Fiber.current.locals) @locals = locals || Hash.new end attr_accessor :locals def self.[] key self.current.locals[key] end def self.[]= key, value self.current.locals[key] = value end end ``` See the pull request <https://github.com/ruby/ruby/pull/6566> for the full proposed implementation. ## Expected Usage Currently, a lot of libraries use `Thread.current[:x]` which is unexpectedly "fiber local". A common bug shows up when lazy enumerators are used, because it may create an internal fiber. Because `locals` are inherited, code which uses `Fiber[:x]` will not suffer from this problem. Any program that uses true thread locals for per-request state, can adopt the proposed `Fiber#locals` and get similar behaviour, without breaking on per-fiber servers like Falcon, because Falcon can "reset" `Fiber.current.locals` for each request fiber, while servers like Puma won't have to do that and will retain thread-local behaviour. Libraries like ActiveRecord can adopt `Fiber#locals` to avoid the need for users to opt into different "IsolatedExecutionState" models, since it can be transparently handled by the web server (see <https://github.com/rails/rails/pull/43596> for more details). We hope by introducing `Fiber#locals`, we can avoid all the confusion and bugs of the past designs. -- https://bugs.ruby-lang.org/ Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>