From c61f49cce97560dde49d00c66dca4df618e69374 Mon Sep 17 00:00:00 2001 From: Joseph Hale Date: Mon, 2 Mar 2026 14:38:19 -0700 Subject: [PATCH] Extract detached-blob guard into its own file Separate the attachment existence check from the broadcast suppression override so each file has a single responsibility. The guard now lives in ActiveStorageAnalyzeJobSkipDetached with its own documentation explaining the upload-then-delete race condition. Co-Authored-By: Claude Opus 4.6 --- .../active_storage_analyze_job_skip_detached.rb | 17 +++++++++++++++++ ...e_storage_analyze_job_suppress_broadcasts.rb | 2 -- ...e_storage_analyze_job_skip_detached_test.rb} | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 lib/rails_ext/active_storage_analyze_job_skip_detached.rb rename test/lib/rails_ext/{active_storage_analyze_job_suppress_broadcasts_test.rb => active_storage_analyze_job_skip_detached_test.rb} (88%) diff --git a/lib/rails_ext/active_storage_analyze_job_skip_detached.rb b/lib/rails_ext/active_storage_analyze_job_skip_detached.rb new file mode 100644 index 000000000..efa671335 --- /dev/null +++ b/lib/rails_ext/active_storage_analyze_job_skip_detached.rb @@ -0,0 +1,17 @@ +# Skip analysis for blobs whose attachments have already been destroyed. +# +# When an upload is deleted before AnalyzeJob runs, PurgeOnLastAttachment has +# already destroyed the attachment row and enqueued PurgeJob for the blob's S3 +# object. Analyzing at this point hits a missing key. Same check as +# PurgeOnLastAttachment#purge_blob_if_last. +module ActiveStorageAnalyzeJobSkipDetached + def perform(blob) + return unless blob.attachments.exists? + + super + end +end + +ActiveSupport.on_load :active_storage_blob do + ActiveStorage::AnalyzeJob.prepend ActiveStorageAnalyzeJobSkipDetached +end diff --git a/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb b/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb index 030232ee4..ba22603b6 100644 --- a/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb +++ b/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb @@ -4,8 +4,6 @@ # there is currently a bug https://github.com/rails/rails/issues/55144 module ActiveStorageAnalyzeJobSuppressBroadcasts def perform(blob) - return unless blob.attachments.exists? - Board.suppressing_turbo_broadcasts do Card.suppressing_turbo_broadcasts do super diff --git a/test/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts_test.rb b/test/lib/rails_ext/active_storage_analyze_job_skip_detached_test.rb similarity index 88% rename from test/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts_test.rb rename to test/lib/rails_ext/active_storage_analyze_job_skip_detached_test.rb index 1015c957c..5ab5edaff 100644 --- a/test/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts_test.rb +++ b/test/lib/rails_ext/active_storage_analyze_job_skip_detached_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class ActiveStorageAnalyzeJobSuppressBroadcastsTest < ActiveSupport::TestCase +class ActiveStorageAnalyzeJobSkipDetachedTest < ActiveSupport::TestCase test "skips analysis when blob has no attachments" do blob = ActiveStorage::Blob.create_and_upload!( io: StringIO.new("x" * 1024), filename: "orphan.txt", content_type: "text/plain"