You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've ran into a race condition on attaching a file to a has_many_attached association. It tries to create 2 ActiveStorage::Attachment records, even though I attach only 1 file, so the other file is actually an ActiveStorage::Blob that was already created by another request.
I've done a little research and found, that when the model gets saved the attachment_changes['images'].send(:subchanges) has 2 records:
Changes::CreateOneOfMany for the existing ActiveStorage::Blob
Changes::CreateOneOfMany for the new ActionDispatch::Http::UploadedFile
I think, the problem is that when the subchanges get built, it loads record.attachments right before the blob is created in another request, and after the blob is created in another request, it loads all blobs for the subchanges. So in the end, it perceives the blob as a completely new attachment.
My specific case is super simple. I don't need to know anything about already existing attachments, so the only thing I need is to correctly save and upload the file. So I came up with the next workaround:
When we're doing multiple requests simultaneously, all the images should get saved successfully, in other words only 1 pair of ActiveStorage::Attachment and ActiveStorage::Blob is created.
I've ran into a race condition on attaching a file to a
has_many_attached
association. It tries to create 2ActiveStorage::Attachment
records, even though I attach only 1 file, so the other file is actually anActiveStorage::Blob
that was already created by another request.I've done a little research and found, that when the model gets saved the
attachment_changes['images'].send(:subchanges)
has 2 records:Changes::CreateOneOfMany
for the existingActiveStorage::Blob
Changes::CreateOneOfMany
for the newActionDispatch::Http::UploadedFile
I think, the problem is that when the subchanges get built, it loads
record.attachments
right before the blob is created in another request, and after the blob is created in another request, it loads all blobs for the subchanges. So in the end, it perceives the blob as a completely new attachment.My specific case is super simple. I don't need to know anything about already existing attachments, so the only thing I need is to correctly save and upload the file. So I came up with the next workaround:
Steps to reproduce
Expected behavior
When we're doing multiple requests simultaneously, all the images should get saved successfully, in other words only 1 pair of
ActiveStorage::Attachment
andActiveStorage::Blob
is created.Actual behavior
System configuration
Rails version: 7.1.1
Ruby version: 3.2.2
The text was updated successfully, but these errors were encountered: