GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Ractor compatible Active Support Notifications by Edouard-chin · Pull Request #58060 · rails/rails

Conversation

@Edouard-chin

@Edouard-chin Edouard-chin commented Jul 8, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Motivation / Background

It is currently not possible to instrument code using ActiveSupport::Notifications inside Ractors.

The underlying notifier used (AS::Fanout) isn't Ractor safe.

Detail

AS::Fanout caches the list of subscribers 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.

Additional information

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.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added activesupport railties labels Jul 8, 2026
Edouard-chin
Edouard-chin commented Jul 8, 2026
View reviewed changes
Comment thread railties/test/application/initializers/frameworks_test.rb Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Edouard-chin
Edouard-chin force-pushed the ec-notifications-wip branch from f59958c to 3e208c2 Compare July 8, 2026 16:22
@Edouard-chin
Edouard-chin marked this pull request as draft July 8, 2026 16:25
@Edouard-chin
Edouard-chin force-pushed the ec-notifications-wip branch 2 times, most recently from bc5428f to 346fb09 Compare July 8, 2026 16:52
@Edouard-chin
Edouard-chin marked this pull request as ready for review July 8, 2026 17:00
@Edouard-chin Edouard-chin added the Ractor Support Issues and pull requests regarding Ractor support label Jul 9, 2026
@Edouard-chin
Edouard-chin force-pushed the ec-notifications-wip branch 2 times, most recently from 446bd00 to b36068a Compare July 28, 2026 16:19
gmcgibbon
gmcgibbon reviewed Jul 29, 2026
View reviewed changes
Comment thread activesupport/lib/active_support/notifications.rb Resolved

Uh oh!

There was an error while loading. Please reload this page.

gmcgibbon
gmcgibbon reviewed Jul 29, 2026
View reviewed changes
Comment thread activesupport/lib/active_support/ractors.rb Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Edouard-chin
Edouard-chin force-pushed the ec-notifications-wip branch from b36068a to 30aac56 Compare July 30, 2026 07:02
gmcgibbon
gmcgibbon reviewed Jul 31, 2026
View reviewed changes
Comment thread activesupport/lib/active_support/notifications.rb
def notifier
return @notifier if ActiveSupport::Ractors.main?

Ractor[:__notifier] ||= begin

@gmcgibbon gmcgibbon Jul 31, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Edouard-chin Edouard-chin Jul 31, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for 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

rails/activesupport/lib/active_support/key_generator.rb

Line 68 in b83739f

Ractor[@ractor_key] = @cache_keys

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gmcgibbon gmcgibbon Jul 31, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Edouard-chin Edouard-chin Jul 31, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gmcgibbon gmcgibbon Aug 5, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
gmcgibbon
gmcgibbon reviewed Aug 5, 2026
View reviewed changes
Comment thread activesupport/lib/active_support/notifications.rb
def notifier
return @notifier if ActiveSupport::Ractors.main?

Ractor[:__notifier] ||= begin

@gmcgibbon gmcgibbon Aug 5, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread activesupport/lib/active_support/notifications/ractor_compatibility.rb Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Edouard-chin
Edouard-chin force-pushed the ec-notifications-wip branch 3 times, most recently from 565026f to d25b579 Compare August 7, 2026 14:25
@Edouard-chin
Ractor compatible Active Support Notifications:
a6b2abf
- ### 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.
@Edouard-chin
Edouard-chin force-pushed the ec-notifications-wip branch from d25b579 to a6b2abf Compare August 7, 2026 14:43
@github-actions github-actions Bot added the activerecord label Aug 7, 2026
@gmcgibbon
gmcgibbon merged commit e929223 into rails:main Aug 7, 2026
5 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@gmcgibbon
gmcgibbon deleted the ec-notifications-wip branch August 7, 2026 18:05
@etiennebarrie etiennebarrie mentioned this pull request Aug 10, 2026
Keep the string subscribers default when rebuilding a Fanout #58433
Merged
@kamipo kamipo mentioned this pull request Aug 12, 2026
Introduce to_ractor_snapshot / load_ractor_snapshot protocol for notifiers #58460
Merged
kamipo added a commit that referenced this pull request Aug 12, 2026
@kamipo
Introduce to_ractor_snapshot / load_ractor_snapshot protocol for …
bd9ea45
…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.
@kamipo kamipo mentioned this pull request Sep 13, 2026
Make notification subscriptions an immutable registry #58760
Closed
kamipo added a commit to kamipo/rails that referenced this pull request Sep 14, 2026
@kamipo
Make notification subscriptions an immutable registry
56af841
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.
kamipo added a commit to kamipo/rails that referenced this pull request Sep 14, 2026
@kamipo
Make notification subscriptions an immutable registry
b7dad06
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.
kamipo added a commit that referenced this pull request Oct 8, 2026
@kamipo
Make the Ractor snapshot of the subscriptions an immutable registry
0d060b5
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gmcgibbon gmcgibbon gmcgibbon left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

activerecord activesupport Ractor Support Issues and pull requests regarding Ractor support railties

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Edouard-chin @gmcgibbon
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.