GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Ractor compatible Active Support Notifications by Edouard-chin · Pull Request #58060 · rails/rails Conversation
|
| def notifier | ||
| return @notifier if ActiveSupport::Ractors.main? | ||
|
|
||
| Ractor[:__notifier] ||= begin |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| Ractor[:__notifier] ||= begin | |
| ActiveSupport::Ractors[:__notifier] ||= begin |
This isn't available in 3.3 AFAIK. Can we make a shim for it?
Context: #58281 (comment)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I don't think a shim make sense for this case and I'd rather add a conditional to the existing guard above (eg Ractors.main? && ractor_supported?). We don't anticipate Ractors to work on Ruby 3.3, and having a shim just adds an uncessary indirection + a new Notifier instantation for no added value.
Also heads up that we already use this pattern on main
| Ractor[@ractor_key] = @cache_keys |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I know, I merged that change. The whole point of the shim is that we don't have to litter the codebase with ractor_supported?. If we need to implement compute_if_absent, I think it is reasonable to implement [] too. Though, I understand that we aren't using it in a case where we could be on 3.3 yet. 🤔
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The whole point of the shim is that we don't have to litter the codebase
Tbh we are already littering the codebase with .freeze and make_shareable. I don't think a couple conditional would harm.
But either way, if you feel strongly I can add it
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We're implementing store_if_absent for 3.3, so we could even use Ractor[]= in all cases here to bypass all the conditionals. That could cause speed regressions though, so I think we need to think about it a little more. This is fine for now.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| def notifier | ||
| return @notifier if ActiveSupport::Ractors.main? | ||
|
|
||
| Ractor[:__notifier] ||= begin |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We're implementing store_if_absent for 3.3, so we could even use Ractor[]= in all cases here to bypass all the conditionals. That could cause speed regressions though, so I think we need to think about it a little more. This is fine for now.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
565026f to
d25b579
Compare
- ### Problem It is currently not possible to instrument code using ActiveSupport::Notifications inside Ractors. The underying notifier used (`AS::Fanout`) isn't Ractor safe. ### Context `AS::Fanout` caches the list of subsribers to notify whenever a notification is sent. The name of the notification can be anything (`sql.activerecord`, `foo.bar`) and can't be computed eagerly. The cache grows at runtime as different controller endpoint get hit and new notification name appear. Because of this, making AS::Fanout Ractor safe isn't possible. ### Solution This commit implements a Notifier *per* Ractor. Whenever a new subscription (whenever code calls `AS::Notifications.subscribe`) is added, we snapshot the list of subscriptions. When a Ractor needs to use a Notifier, one is instantiated and the subscriptions is set based on the frozen snapshot.
d25b579 to
a6b2abf
Compare
There was an error while loading. Please reload this page.
…notifiers Since #58060, `Notifications.record_subscriptions` reached into `Fanout`'s `string_subscribers` / `other_subscribers`, so custom `notifier=` targets had to mimic Fanout's shape or crash. Move it behind a `respond_to?`-gated protocol on the notifier; custom queues become plain targets and may opt into Ractor sharing by implementing it. A shareable `Fanout` would remove the snapshot entirely but needs a COW rewrite of its `Concurrent::Map`-backed state, so this change stays with the snapshot; that refactor can happen independently.
Since rails#58060, a Ractor built its notifier from a snapshot of the subscriptions taken on first use and never looked at it again, so subscriptions added or removed later were invisible to it. The snapshot was also assembled outside `Fanout`'s mutex, and `Notifications.subscribed` never published its temporary subscription. Replace `Fanout`'s `Concurrent::Map` / `Array` pair with `Fanout::Registry`, which `subscribe` / `unsubscribe` replace instead of mutating in place. A Ractor compares identity to tell that the registry it materialized went stale, and adopts the new one.
Since rails#58060, a Ractor built its notifier from a snapshot of the subscriptions taken on first use and never looked at it again, so subscriptions added or removed later were invisible to it. The snapshot was also assembled outside `Fanout`'s mutex. Replace `Fanout`'s `Concurrent::Map` / `Array` pair with `Fanout::Registry`, which `subscribe` / `unsubscribe` replace instead of mutating in place. A Ractor compares identity to tell that the registry it materialized went stale, and adopts the new one.
Since #58060, the snapshot a Ractor materializes its notifier from is a Hash of `Fanout`'s internals, assembled with `keys.zip(values)` outside its mutex and rebuilt into a `Concurrent::Map` on the other side. Replace `Fanout`'s `Concurrent::Map` / `Array` pair with `Fanout::Registry`, which `subscribe` / `unsubscribe` replace instead of mutating in place. Taking the snapshot is then reading one frozen object, and loading it is an assignment.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.