Skip to content

Conversation

@adrianna-chang-shopify
Copy link

@adrianna-chang-shopify adrianna-chang-shopify commented Oct 24, 2025

Summary

This PR updates IdentityCache's callback override mechanism to be compatible with updates to Rails' callback handling code, as of rails/rails@207a254. The change in Rails introduces a "fast path" for callbacks and no-ops the _run_#{callback}_callbacks methods up until the point where a callback is actually defined, at which point Rails aliases _run_#{callback}_callbacks to a ! version of the method.

Rather than patching the ! version of the callbacks, we patch the run_callbacks method. This way, we don't need to worry about callbacks potentially being defined before we include the IdentityCache gem, which can lead to Rails aliasing the non-patched version of the _run_commit_callbacks! methods. Plus, run_callbacks is public API, and it makes more sense for us to invalidate the cache any time callbacks are run (and not just when they're run via a call to committed! within Active Record transaction code.)

Changes

  • Patch run_callbacks instead of _run_commit_callbacks: Rails 8.1 made _run_commit_callbacks a no-op and now uses run_commit_callbacks! internally (rails/rails@207a254).
    Overriding run_callbacks is more robust and avoids ordering issues when the IdentityCache module is included.
  • [Temporary] Add ignore_override parameter: Introduces a temporary compatibility flag to allow callers to bypass the cache expiration behavior. This provides backwards compatibility for existing code that calls run_callbacks directly,
    enabling safer rollout.

TODO: Remove ignore_override before shipping, after we verify the change doesn't cause any unintended side effects in Shopify's Core monolith.

Ref: rails/rails@207a254

_run_commit_callbacks is now a no-op; Rails aliases it to a `run_commit_callbacks!` implementation.
We could patch the `!` version, but it can lead to issues depending on when callbacks are defined and
when the `IdentityCache` module is included. Overriding `run_callbacks` is a more robust solution.
Allows us to ignore the cache reset behaviour while we roll this out in Core.
There are a few spots where we're calling `run_callbacks` directly in app
code, and this is a change in behaviour, so we'll provide an option
temporarily to enable backwards compatibility.
@adrianna-chang-shopify
Copy link
Author

Tests for this pass locally for me with all three gemfiles.
CI seems to be borked: https://github.com/Shopify/identity_cache/actions/runs/18788007562/job/53611097875 (here's a no-op commit on a fresh branch that's failing CI)

if destroyed? || transaction_changed_attributes.present?
expire_cache
if ActiveRecord.version >= Gem::Version.new("7.1")
def run_callbacks(kind, type = nil, ignore_override: false)
Copy link
Member

@Edouard-chin Edouard-chin Oct 24, 2025

Choose a reason for hiding this comment

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

Where does the ignore_override come from ?

I should RTFM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants