New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveStorage::Attached::Many#attach
may erase files uploaded concurrently
#42941
Comments
The only workaround that I think would work is using the exclusive lock, e.g.: Parallel.each(1..50) do
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute("LOCK #{ActiveStorage::Blob.table_name} IN ACCESS EXCLUSIVE MODE")
User.first.files.attach(io: File.open("README.md"), filename: "README.md")
end
end
end |
ActiveStorage::Attached::Many#attach
is not "thread safe"ActiveStorage::Attached::Many#attach
may erase files uploaded concurrently
Is there any reason why https://github.com/rails/rails/blob/main/activestorage/lib/active_storage/attached/many.rb#L51-L52 replaces the collection instead of creating new objects for attachment(s)? |
Another workaround is to create an attachment manually: blob = ActiveStorage::Blob.create_and_upload!(io: File.open("README.md"), filename: "README.md")
attachment = ActiveStorage::Attachment.create!(blob: blob, name: "files", record_type: "User", record_id: 1) I did not check the consequences like error handling yet, but at least it does not require a table-wide exclusive lock. |
Due to awkward implementation-sharing between |
@smt116 I do confirm |
We also need to consider how appends should stack on top of other changes—we can’t just keep one change per reflection anymore. If you call the setter followed by |
I see. I will use #42941 (comment) as the workaround for now. Please let me know If you are aware of any bad consequences of such a solution or a better approach. Also, I can try to help with fixing the underlying issue, but I would need some hints on where to start. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
@rails-bot still is still relevant. |
I encountered the same concurrency issue by doing a basic multi file upload form through rails api mode. Sending 9 images at the same time results in missing images 95% of the time. I tried setting the config below as per #35817 but it does not fix the problem.
|
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Still relevant. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Still relevant —Maciej Małeckihttp://smefju.pl wt., 28 cze 2022 16:04, rails-bot[bot] ***@***.***> napisał(a):
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
still relevant. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Still relevant. niedz., 25 gru 2022 17:01, rails-bot[bot] ***@***.***> napisał(a):
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Still relevant |
Steps to reproduce
First setup the sample application:
gem install rails -v6.1.4
rails new ConcurrentAttachTestApp --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-action-cable --skip-sprockets --skip-spring --skip-listen --skip-javascript --skip-turbolinks --skip-jbuilder --skip-test --skip-system-test --skip-bootsnap
apply the following diff:
bundle install
bin/rails active_storage:install
bin/rails db:setup
Now try the following code in the
bin/rails console
:Note that
User.first.files.count
may return a smaller number than 10.Expected behavior
Each
Object#attach
appends the new file and does not touch existing attachments.Actual behavior
Object#attach
may purge files that are uploaded concurrently.System configuration
Rails version: 6.1.4
Ruby version: 3.0.1
The text was updated successfully, but these errors were encountered: