[ruby-core:111353] [Ruby master Feature#19078] Introduce `Fiber#storage` for inheritable fiber-scoped variables.

Issue #19078 has been updated by Eregon (Benoit Daloze). matz (Yukihiro Matsumoto) wrote in #note-22:
(4) Fiber.new(...,storage: hash|true|false|nil) - accepted, but true/false should be experimental The feature (4) with true/false are hard to read the intention (true to inherit with dup, false to inherit without dup), so we need experiment here as well.
`false` is a violation of everything we discussed (with @byroot etc). It means no more isolation between Fibers, and setting a variable accidentally changes other Fiber storages, leading to tons of confusion. I believe no language does this for very good reasons. I don't know when you added this @ioquatix but that is an unacceptable change to the design long discussed. Also there is no experimental warning or anything as matz has asked. I will remove `storage: false` now, this is the only safe thing to do before the release. Also this is a necessary condition to avoid synchronization on Fiber[]/Fiber[]= for non-GIL Rubies. BTW, Fiber#storage and Fiber#storage= do not check which thread calls them. But a different thread should of course never be allowed to reassign the storage of a Fiber it doesn't belong to. Accessing it also feels wrong. ---------------------------------------- Feature #19078: Introduce `Fiber#storage` for inheritable fiber-scoped variables. https://bugs.ruby-lang.org/issues/19078#change-100727 * Author: ioquatix (Samuel Williams) * Status: Closed * Priority: Normal * Assignee: ioquatix (Samuel Williams) ---------------------------------------- Pull Request: https://github.com/ruby/ruby/pull/6612 This is an evolution of the previous ideas: - https://bugs.ruby-lang.org/issues/19058 - https://bugs.ruby-lang.org/issues/19062 This PR introduces fiber scoped variables, and is a solution for problems like <https://github.com/ioquatix/ioquatix/discussions/17>. The main interface is: ```ruby Fiber[key] = value Fiber[key] # => value ``` The variables are scoped (local to) a fiber and inherited into child fibers and threads. ```ruby Fiber[:request_id] = SecureRandom.hex(16) Fiber.new do p Fiber[:request_id] # prints the above request id end ``` The fiber scoped variables are stored and can be accessed: ```ruby Fiber.current.storage # => returns a Hash (copy) of the internal storage. Fiber.current.storage= # => assigns a Hash (copy) to the internal storage. ``` Fiber itself has one new keyword argument: ``` Fiber.new(..., storage: hash, false, undef, nil) ``` This can control how the fiber variables are setup in a child context. To minimise the performance overhead of some of the implementation choices, we are also simultaneously implementing <https://bugs.ruby-lang.org/issues/19077>. ## Examples ### Request loop ```ruby Thread.new do while request = queue.pop Fiber.new(storage: {id: SecureRandom.hex(16)}) do handle_request.call(request) end end end ``` OR ```ruby Thread.new do while request = queue.pop Fiber.current.storage = {id: SecureRandom.hex(16)} handle_request.call(request) end end ``` -- https://bugs.ruby-lang.org/
participants (1)
-
Eregon (Benoit Daloze)