{"action":"created","issue":{"url":"https://api.github.com/repos/gocardless/statesman/issues/228","repository_url":"https://api.github.com/repos/gocardless/statesman","labels_url":"https://api.github.com/repos/gocardless/statesman/issues/228/labels{/name}","comments_url":"https://api.github.com/repos/gocardless/statesman/issues/228/comments","events_url":"https://api.github.com/repos/gocardless/statesman/issues/228/events","html_url":"https://github.com/gocardless/statesman/pull/228","id":180741782,"number":228,"title":"Lock the parent object to prevent deadlocks updating most_recent","user":{"login":"pjungwir","id":660353,"avatar_url":"https://avatars0.githubusercontent.com/u/660353?v=4","gravatar_id":"","url":"https://api.github.com/users/pjungwir","html_url":"https://github.com/pjungwir","followers_url":"https://api.github.com/users/pjungwir/followers","following_url":"https://api.github.com/users/pjungwir/following{/other_user}","gists_url":"https://api.github.com/users/pjungwir/gists{/gist_id}","starred_url":"https://api.github.com/users/pjungwir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjungwir/subscriptions","organizations_url":"https://api.github.com/users/pjungwir/orgs","repos_url":"https://api.github.com/users/pjungwir/repos","events_url":"https://api.github.com/users/pjungwir/events{/privacy}","received_events_url":"https://api.github.com/users/pjungwir/received_events","type":"User","site_admin":false},"labels":[{"id":613667753,"url":"https://api.github.com/repos/gocardless/statesman/labels/breaking%20change","name":"breaking change","color":"0e8a16","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-10-03T20:43:15Z","updated_at":"2017-07-17T23:20:19Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/gocardless/statesman/pulls/228","html_url":"https://github.com/gocardless/statesman/pull/228","diff_url":"https://github.com/gocardless/statesman/pull/228.diff","patch_url":"https://github.com/gocardless/statesman/pull/228.patch"},"body":"In [`unset_old_most_recent`](https://github.com/gocardless/statesman/blob/master/lib/statesman/adapters/active_record.rb#L89-L102) it is easy to get deadlocks from this code:\n\n```\ntransitions_for_parent.update_all(most_recent: false)\n```\n\nThe problem is that it updates several rows, and concurrent updates may not update the rows in the same order. This causes [deadlocks](http://stackoverflow.com/questions/16735950/update-with-order-by) in [Postgres](http://dba.stackexchange.com/questions/68388/optimizing-concurrent-updates-in-postgres).\n\nThis commit fixes the problem by locking the parent object before altering its status transitions.\n\nI wasn't really sure how to write a test for this, although in my own project I could reproduce the problem right away by running this in two shells at once:\n\n```\n# script/c.rb\n# run with `rails runner script/c.rb`\nc = Contact.find 20159\nwhile true do\n  10.times do\n    c.state_machine.trigger(:archive)\n    c.state_machine.trigger(:activate)\n  end\n  print \".\"\n  STDOUT.flush\nend\n```\n"},"comment":{"url":"https://api.github.com/repos/gocardless/statesman/issues/comments/315912768","html_url":"https://github.com/gocardless/statesman/pull/228#issuecomment-315912768","issue_url":"https://api.github.com/repos/gocardless/statesman/issues/228","id":315912768,"user":{"login":"pjungwir","id":660353,"avatar_url":"https://avatars0.githubusercontent.com/u/660353?v=4","gravatar_id":"","url":"https://api.github.com/users/pjungwir","html_url":"https://github.com/pjungwir","followers_url":"https://api.github.com/users/pjungwir/followers","following_url":"https://api.github.com/users/pjungwir/following{/other_user}","gists_url":"https://api.github.com/users/pjungwir/gists{/gist_id}","starred_url":"https://api.github.com/users/pjungwir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjungwir/subscriptions","organizations_url":"https://api.github.com/users/pjungwir/orgs","repos_url":"https://api.github.com/users/pjungwir/repos","events_url":"https://api.github.com/users/pjungwir/events{/privacy}","received_events_url":"https://api.github.com/users/pjungwir/received_events","type":"User","site_admin":false},"created_at":"2017-07-17T23:20:19Z","updated_at":"2017-07-17T23:20:19Z","body":"Here is a thread about it I started myself a few years ago: :-)\r\n\r\nhttps://www.postgresql.org/message-id/CA%2B6hpa%3D9cV9L%2BspfK_yn%2BrTeaQtHWyLLXWtLgpKJi%2BUJYUc2Qw%40mail.gmail.com\r\n"}}
{"actor":{"display_login":"pjungwir"}}