{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/34615","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/34615/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/34615/comments","events_url":"https://api.github.com/repos/rails/rails/issues/34615/events","html_url":"https://github.com/rails/rails/issues/34615","id":387379438,"node_id":"MDU6SXNzdWUzODczNzk0Mzg=","number":34615,"title":"Possible concurrency issue in active record associations","user":{"login":"ashu210890","id":2214010,"node_id":"MDQ6VXNlcjIyMTQwMTA=","avatar_url":"https://avatars3.githubusercontent.com/u/2214010?v=4","gravatar_id":"","url":"https://api.github.com/users/ashu210890","html_url":"https://github.com/ashu210890","followers_url":"https://api.github.com/users/ashu210890/followers","following_url":"https://api.github.com/users/ashu210890/following{/other_user}","gists_url":"https://api.github.com/users/ashu210890/gists{/gist_id}","starred_url":"https://api.github.com/users/ashu210890/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashu210890/subscriptions","organizations_url":"https://api.github.com/users/ashu210890/orgs","repos_url":"https://api.github.com/users/ashu210890/repos","events_url":"https://api.github.com/users/ashu210890/events{/privacy}","received_events_url":"https://api.github.com/users/ashu210890/received_events","type":"User","site_admin":false},"labels":[{"id":5944892,"node_id":"MDU6TGFiZWw1OTQ0ODky","url":"https://api.github.com/repos/rails/rails/labels/JRuby","name":"JRuby","color":"02d7e1","default":false},{"id":107191,"node_id":"MDU6TGFiZWwxMDcxOTE=","url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-12-04T16:46:09Z","updated_at":"2018-12-10T17:13:33Z","closed_at":"2018-12-10T17:13:33Z","author_association":"NONE","body":"### Steps to reproduce\r\nThe setup goes something like this:\r\n```\r\nclass Account < ApplicationRecord\r\n   has_and_belongs_to_many :tags\r\n\r\n  def add_tag(tag)\r\n      self.tags << tag\r\n  end\r\nend\r\n\r\nclass Tag < ApplicationRecord\r\n  has_and_belongs_to_many :accounts\r\nend\r\n\r\nacc = Account.new\r\nacc.save!\r\ntag = Tag.new\r\ntag.save!\r\nacc.add_tag(tag)\r\n\r\n```\r\nThe above runs in multiple threads where `acc.add_tag` can be called for the same account and different tags.\r\n\r\n<!-- (Guidelines for creating a bug report are [available\r\nhere](http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#creating-a-bug-report)) -->\r\n\r\n### Expected behavior\r\nThe expected behaviour is that this should work properly and all the tags should be applied to the given account. \r\n<!-- Tell us what should happen -->\r\n\r\n### Actual behavior\r\nThe following stacktrace shows up intermittently:\r\n```\r\nNoMethodError: undefined method `<' for nil:NilClass\r\n\r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/transactions.rb:425:in `clear_transaction_record_state', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/transactions.rb:498:in `update_attributes_from_transaction_state', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/transactions.rb:491:in `sync_with_transaction_state', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/persistence.rb:232:in `new_record?', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:292:in `null_scope?', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:287:in `scope', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/association.rb:191:in `scope_for_create', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/association.rb:176:in `initialize_attributes', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/association.rb:277:in `block in build_record', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/core.rb:316:in `initialize', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/inheritance.rb:66:in `new', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/reflection.rb:154:in `build_association', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/association.rb:276:in `build_record', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:106:in `build', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/has_many_through_association.rb:60:in `build_through_record', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/has_many_through_association.rb:82:in `save_through_record', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/has_many_through_association.rb:45:in `insert_record', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:438:in `block in concat_records', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:458:in `replace_on_target', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:282:in `add_to_target', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:436:in `block in concat_records', \r\norg/jruby/RubyArray.java:1789:in `each', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:434:in `concat_records', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/has_many_association.rb:116:in `concat_records', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/has_many_through_association.rb:27:in `concat_records', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:119:in `block in concat', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:134:in `block in transaction', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `block in transaction', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:239:in `block in within_new_transaction', \r\n/home/deploy/.rvm/rubies/jruby-9.2.3.0/lib/ruby/stdlib/monitor.rb:226:in `mon_synchronize', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `transaction', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/transactions.rb:212:in `transaction', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:133:in `transaction', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_association.rb:119:in `concat', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/has_many_through_association.rb:21:in `concat', \r\n/home/deploy/.rvm/gems/jruby-9.2.3.0/gems/activerecord-5.2.1/lib/active_record/associations/collection_proxy.rb:1056:in `<<',\r\n/home/deploy/myapp/app/models/account.rb:6:in `add_tag',\r\n...\r\n\r\n```\r\n\r\nWe often see similar issues with activerecord on jruby where a simple `belongs_to` relationship yields `nil` response when resolved even if the  association actually exists. The above error could be of a similar flavour but much more frequent.\r\n\r\n<!-- Tell us what happens instead -->\r\n\r\n### System configuration\r\n**Rails version**:\r\n5.2.1\r\n**Ruby version**:\r\n2.5.1\r\nengine: jruby\r\njruby-version: 9.2.3.0"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/445895081","html_url":"https://github.com/rails/rails/issues/34615#issuecomment-445895081","issue_url":"https://api.github.com/repos/rails/rails/issues/34615","id":445895081,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0NTg5NTA4MQ==","user":{"login":"rafaelfranca","id":47848,"node_id":"MDQ6VXNlcjQ3ODQ4","avatar_url":"https://avatars3.githubusercontent.com/u/47848?v=4","gravatar_id":"","url":"https://api.github.com/users/rafaelfranca","html_url":"https://github.com/rafaelfranca","followers_url":"https://api.github.com/users/rafaelfranca/followers","following_url":"https://api.github.com/users/rafaelfranca/following{/other_user}","gists_url":"https://api.github.com/users/rafaelfranca/gists{/gist_id}","starred_url":"https://api.github.com/users/rafaelfranca/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rafaelfranca/subscriptions","organizations_url":"https://api.github.com/users/rafaelfranca/orgs","repos_url":"https://api.github.com/users/rafaelfranca/repos","events_url":"https://api.github.com/users/rafaelfranca/events{/privacy}","received_events_url":"https://api.github.com/users/rafaelfranca/received_events","type":"User","site_admin":false},"created_at":"2018-12-10T17:13:33Z","updated_at":"2018-12-10T17:13:33Z","author_association":"MEMBER","body":"Rails don't support sharing the same instance of ActiveRecord::Base between two threads. This can't be safe since any state inside that object would need to have need a mutex."}}
{"actor":{"display_login":"rafaelfranca"}}