Closed dssjoblom closed 1 year ago
So I did some digging into this. It looks like a classic race-condition. In alchemy-dragonfly-s3:
module Alchemy
module Dragonfly
module S3
class CreatePictureThumb
def self.call(variant, signature, uid)
# create the thumb before uploading
# to prevent db race conditions
thumb = nil
if variant.picture.valid?
thumb = Alchemy::PictureThumb.create!(
picture: variant.picture,
signature: signature,
uid: uid,
)
end
begin
# fetch and process the image
image = variant.image
# upload the processed image
image.store(path: uid)
rescue RuntimeError, Excon::Error => e
ErrorTracking.notification_handler.call(e)
# destroy the thumb if processing or upload fails
thumb&.destroy
end
end
end
end
end
end
Shouldn't this use something like https://apidock.com/rails/ActiveRecord/Relation/create_or_find_by ?
We only write if the thumbnail does not exist, as you can see here https://github.com/AlchemyCMS/alchemy_cms/blob/0e13375c362a60d1e65eec05f4c357a440fddff8/app/models/alchemy/picture/url.rb#L39
Maybe there is a more thorough way of doing it? Want to try finding a fix?
@dssjoblom would please give
# Gemfile
gem "alchemy_cms", git: "https://github.com/tvdeyen/alchemy_cms", branch: "6.1-write-thumbs-on-writing-db"
a try?
@dssjoblom and for alchemy-dragonfly-s3
please use this PR https://github.com/AlchemyCMS/alchemy-dragonfly-s3/pull/20
# Gemfile
gem "alchemy-dragonfly-s3", git: "https://github.com/AlchemyCMS/alchemy-dragonfly-s3", branch: "fix-concurrency-issues"
Steps to reproduce
When upgrading from an older 6.0.0-pre version to 6.1.1, it seems that there are duplicate key violations while rendering the templates. Strangely enough, reloading the page makes the error go away.
Update: I get this also in the admin portion. If I upload an image and use it in an element, the preview always generates the same error. When I reload the preview, the image is there and everything works normally.
Stacktrace:
Expected behavior
There should be no duplicate key violations. I'd also not expect a GET request to cause database writes (like thumbnail creation), see #2428
Actual behavior
I get a duplicate key violation. It's also mysterious that once the same page is loaded again the error disappears.
System configuration