Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix duplicate event logging across Zeitwerk reloads (#13922)

Damian Legawiecb0e79afcd86b
diff --git a/spree/core/app/subscribers/spree/event_log_subscriber.rb b/spree/core/app/subscribers/spree/event_log_subscriber.rb
index 667195d8da..ff295e049e 100644
--- a/spree/core/app/subscribers/spree/event_log_subscriber.rb
+++ b/spree/core/app/subscribers/spree/event_log_subscriber.rb
@@ -16,25 +16,27 @@ module Spree
class << self
def attach_to_notifications
- # Always detach first to ensure clean state after code reload
+ # Always detach first to ensure clean state after code reload.
+ # The subscription reference is stored on Spree::Events (in lib/, not
+ # reloaded by Zeitwerk) so repeated reloads in development do not leak
+ # stale AS::N subscriptions when this class is reloaded.
detach_from_notifications
- @subscription = ActiveSupport::Notifications.subscribe(/\.#{NAMESPACE}$/) do |name, start, finish, _id, payload|
+ Spree::Events.log_subscription = ActiveSupport::Notifications.subscribe(/\.#{NAMESPACE}$/) do |name, start, finish, _id, payload|
log_event(name, start, finish, payload)
end
- @attached = true
Rails.logger.info "[Spree Events] Event logging enabled"
end
def detach_from_notifications
- ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription
- @subscription = nil
- @attached = false
+ subscription = Spree::Events.log_subscription
+ ActiveSupport::Notifications.unsubscribe(subscription) if subscription
+ Spree::Events.log_subscription = nil
end
def attached?
- @attached || false
+ !Spree::Events.log_subscription.nil?
end
private
diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rb
index 0c9bb831ee..6ea35d72fd 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -289,13 +289,10 @@ module Spree
Spree::Addresses::PhoneValidator
]
- # Attach event log subscriber if enabled
- if Spree::Config.events_log_enabled
- Spree::EventLogSubscriber.attach_to_notifications
- end
-
# Add core event subscribers
# Other engines add their subscribers in their own after_initialize blocks
+ # Note: Spree::EventLogSubscriber is attached in to_prepare (below) so it
+ # survives Zeitwerk code reloads in development.
Spree.subscribers.concat [
Spree::ExportSubscriber,
Spree::ReportSubscriber,
diff --git a/spree/core/lib/spree/events.rb b/spree/core/lib/spree/events.rb
index 7bbc6b8a32..d42c30f9b6 100644
--- a/spree/core/lib/spree/events.rb
+++ b/spree/core/lib/spree/events.rb
@@ -28,6 +28,12 @@ module Spree
#
module Events
class << self
+ # Reference to the AS::N subscription created by Spree::EventLogSubscriber.
+ # Stored here (on a module in lib/, not reloaded by Zeitwerk) so that code
+ # reloads in development don't orphan the subscription and cause events to
+ # be logged multiple times.
+ attr_accessor :log_subscription
+
# Publish an event to all matching subscribers
#
# @param name [String] The event name (e.g., 'order.complete')
diff --git a/spree/core/spec/subscribers/spree/event_log_subscriber_spec.rb b/spree/core/spec/subscribers/spree/event_log_subscriber_spec.rb
index 322eabfef5..aaca561642 100644
--- a/spree/core/spec/subscribers/spree/event_log_subscriber_spec.rb
+++ b/spree/core/spec/subscribers/spree/event_log_subscriber_spec.rb
@@ -27,6 +27,31 @@ RSpec.describe Spree::EventLogSubscriber do
described_class.attach_to_notifications
expect(described_class.attached?).to be true
end
+
+ # Regression test: in development, Zeitwerk reloads classes under app/, wiping
+ # any class-level ivars. If the subscription reference lived on the subscriber
+ # class, detach_from_notifications would silently do nothing after a reload and
+ # stale AS::N subscriptions would accumulate, causing events to be logged
+ # multiple times. The reference is instead stored on Spree::Events (in lib/,
+ # not reloaded), so detach/re-attach across "reloads" must still log exactly once.
+ it 'does not leak AS::N subscriptions when the subscriber class is reloaded' do
+ described_class.attach_to_notifications
+
+ # Simulate a Zeitwerk reload: any class-level ivars would be wiped on a
+ # fresh class object. We just verify that the reattach path goes through
+ # the non-reloaded Spree::Events.log_subscription storage.
+ described_class.attach_to_notifications
+ described_class.attach_to_notifications
+
+ event = Spree::Event.new(name: 'order.complete', payload: { 'id' => 1 }, metadata: {})
+
+ log_calls = 0
+ allow(Rails.logger).to receive(:info) { log_calls += 1 }
+
+ ActiveSupport::Notifications.instrument('order.complete.spree', event: event) {}
+
+ expect(log_calls).to eq(1)
+ end
end
describe '.detach_from_notifications' do