Skip to content
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 attribute conflicts with save by optimistic locking #31542

Closed
swordray opened this issue Dec 22, 2017 · 4 comments
Closed

ActiveStorage attribute conflicts with save by optimistic locking #31542

swordray opened this issue Dec 22, 2017 · 4 comments
Milestone

Comments

@swordray
Copy link

Steps to reproduce

class User < ActiveRecord::Base
  has_one_attached :avatar
end

user_params = { 
  avatar: ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new, filename: 'a.txt'),
  updated_at: Time.now,
}

user = User.first
user.attributes = user_params
user.save # => ActiveRecord::StaleObjectError: Attempted to touch a stale object: User.

System configuration

Rails version: 5.2.0.beta2@0901755

Ruby version: 2.5.0rc1

@georgeclaghorn georgeclaghorn added this to the 5.2.0 milestone Jan 1, 2018
@georgeclaghorn
Copy link
Contributor

I can’t reproduce in a fresh application running latest master. Can you build a small sample application that reproduces the error?

@swordray
Copy link
Author

swordray commented Jan 2, 2018

Sure. Check out https://github.com/swordray/demo. It is a rails master project with User model and controller. While I send a users#update request from browser, I got following logs.

Started PATCH "/users/1.json" for 127.0.0.1 at 2018-01-02 15:24:51 +0800
Processing by UsersController#update as JSON
  Parameters: {"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007ffa45ddcc68 @tempfile=#<Tempfile:/var/folders/_n/d6lznfcd4jq_xtj4dcrdv0_c0000gn/T/RackMultipart20180102-84816-1itylp3.txt>, @original_filename="a.txt", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"a.txt\"\r\nContent-Type: text/plain\r\n">, "updated_at"=>"2018-01-01"}, "id"=>"1"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
  ActiveStorage::Attachment Load (0.1ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 1], ["record_type", "User"], ["name", "avatar"], ["LIMIT", 1]]
  ActiveStorage::Blob Load (0.1ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
   (0.0ms)  begin transaction
  ActiveStorage::Attachment Destroy (0.4ms)  DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = ?  [["id", 2]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Disk Storage (1.0ms) Uploaded file to key: eKSnxxW7UqvYsYicgMsSZLXg (checksum: 1B2M2Y8AsgTpgAmY7PhCfg==)
  ActiveStorage::Blob Create (0.2ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "byte_size", "checksum", "created_at") VALUES (?, ?, ?, ?, ?, ?)  [["key", "eKSnxxW7UqvYsYicgMsSZLXg"], ["filename", "a.txt"], ["content_type", "text/plain"], ["byte_size", 0], ["checksum", "1B2M2Y8AsgTpgAmY7PhCfg=="], ["created_at", "2018-01-02 07:24:51.499106"]]
  ActiveStorage::Attachment Create (0.1ms)  INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES (?, ?, ?, ?, ?)  [["name", "avatar"], ["record_type", "User"], ["record_id", 1], ["blob_id", 3], ["created_at", "2018-01-02 07:24:51.500812"]]
  User Update All (0.1ms)  UPDATE "users" SET "updated_at" = '2018-01-02 07:24:51.497410', "lock_version" = 4 WHERE "users"."id" = ? AND "users"."lock_version" = ?  [["id", 1], ["lock_version", 3]]
   (6.5ms)  commit transaction
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 389fe784-c349-4c22-9e24-deb1d316706c) to Async(default) with arguments: #<GlobalID:0x00007ffa43e683a0 @uri=#<URI::GID gid://demo/ActiveStorage::Blob/3>>
[ActiveJob] Enqueued ActiveStorage::PurgeJob (Job ID: 1597eda4-3868-437a-afa1-261ff9ee3c23) to Async(default) with arguments: #<GlobalID:0x00007ffa44c17f78 @uri=#<URI::GID gid://demo/ActiveStorage::Blob/2>>
   (0.1ms)  begin transaction
  ActiveStorage::Blob Load (0.2ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  User Update All (0.1ms)  UPDATE "users" SET "updated_at" = '2018-01-01 00:00:00', "lock_version" = 4 WHERE "users"."id" = ? AND "users"."lock_version" = ?  [["id", 1], ["lock_version", 3]]
   (0.1ms)  rollback transaction
  ActiveStorage::Blob Load (0.1ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
Completed 409 Conflict in 22ms (ActiveRecord: 8.1ms)



[ActiveJob] [ActiveStorage::AnalyzeJob] [389fe784-c349-4c22-9e24-deb1d316706c] Performing ActiveStorage::AnalyzeJob (Job ID: 389fe784-c349-4c22-9e24-deb1d316706c) from Async(default) with arguments: #<GlobalID:0x00007ffa43ea2b18 @uri=#<URI::GID gid://demo/ActiveStorage::Blob/3>>
[ActiveJob] [ActiveStorage::AnalyzeJob] [389fe784-c349-4c22-9e24-deb1d316706c]    (0.1ms)  begin transaction
[ActiveJob] [ActiveStorage::AnalyzeJob] [389fe784-c349-4c22-9e24-deb1d316706c]   ActiveStorage::Blob Update (0.3ms)  UPDATE "active_storage_blobs" SET "metadata" = ? WHERE "active_storage_blobs"."id" = ?  [["metadata", "{\"analyzed\":true}"], ["id", 3]]
[ActiveJob] [ActiveStorage::PurgeJob] [1597eda4-3868-437a-afa1-261ff9ee3c23] Performing ActiveStorage::PurgeJob (Job ID: 1597eda4-3868-437a-afa1-261ff9ee3c23) from Async(default) with arguments: #<GlobalID:0x00007ffa4a8c1670 @uri=#<URI::GID gid://demo/ActiveStorage::Blob/2>>
ActiveRecord::StaleObjectError (Attempted to update a stale object: User.):
[ActiveJob] [ActiveStorage::AnalyzeJob] [389fe784-c349-4c22-9e24-deb1d316706c]    (1.0ms)  commit transaction

[ActiveJob] [ActiveStorage::AnalyzeJob] [389fe784-c349-4c22-9e24-deb1d316706c] Performed ActiveStorage::AnalyzeJob (Job ID: 389fe784-c349-4c22-9e24-deb1d316706c) from Async(default) in 9.41ms
app/controllers/users_controller.rb:7:in `update'
[ActiveJob] [ActiveStorage::PurgeJob] [1597eda4-3868-437a-afa1-261ff9ee3c23]   Disk Storage (0.3ms) Deleted file from key: g6V6KsDUYVDTEDWiYvysyBFo
[ActiveJob] [ActiveStorage::PurgeJob] [1597eda4-3868-437a-afa1-261ff9ee3c23]    (0.1ms)  begin transaction
[ActiveJob] [ActiveStorage::PurgeJob] [1597eda4-3868-437a-afa1-261ff9ee3c23]   ActiveStorage::Attachment Load (0.2ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 2], ["record_type", "ActiveStorage::Blob"], ["name", "preview_image"], ["LIMIT", 1]]
[ActiveJob] [ActiveStorage::PurgeJob] [1597eda4-3868-437a-afa1-261ff9ee3c23]   ActiveStorage::Blob Destroy (0.5ms)  DELETE FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ?  [["id", 2]]
[ActiveJob] [ActiveStorage::PurgeJob] [1597eda4-3868-437a-afa1-261ff9ee3c23]    (2.6ms)  commit transaction
[ActiveJob] [ActiveStorage::PurgeJob] [1597eda4-3868-437a-afa1-261ff9ee3c23] Performed ActiveStorage::PurgeJob (Job ID: 1597eda4-3868-437a-afa1-261ff9ee3c23) from Async(default) in 43.43ms

@georgeclaghorn
Copy link
Contributor

@swordray, can you confirm that 93e6a0e fixes your issue?

@swordray
Copy link
Author

swordray commented Jan 9, 2018

@georgeclaghorn Yes. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants