{"action":"created","issue":{"url":"https://api.github.com/repos/emberjs/data/issues/4507","repository_url":"https://api.github.com/repos/emberjs/data","labels_url":"https://api.github.com/repos/emberjs/data/issues/4507/labels{/name}","comments_url":"https://api.github.com/repos/emberjs/data/issues/4507/comments","events_url":"https://api.github.com/repos/emberjs/data/issues/4507/events","html_url":"https://github.com/emberjs/data/issues/4507","id":172735210,"number":4507,"title":"hasMany relationship getting out of sync","user":{"login":"aaronbhansen","id":1092833,"avatar_url":"https://avatars.githubusercontent.com/u/1092833?v=3","gravatar_id":"","url":"https://api.github.com/users/aaronbhansen","html_url":"https://github.com/aaronbhansen","followers_url":"https://api.github.com/users/aaronbhansen/followers","following_url":"https://api.github.com/users/aaronbhansen/following{/other_user}","gists_url":"https://api.github.com/users/aaronbhansen/gists{/gist_id}","starred_url":"https://api.github.com/users/aaronbhansen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaronbhansen/subscriptions","organizations_url":"https://api.github.com/users/aaronbhansen/orgs","repos_url":"https://api.github.com/users/aaronbhansen/repos","events_url":"https://api.github.com/users/aaronbhansen/events{/privacy}","received_events_url":"https://api.github.com/users/aaronbhansen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-08-23T15:44:42Z","updated_at":"2016-10-13T13:49:30Z","closed_at":null,"body":"We have a hasMany relationship that appears to be getting into a race condition. We are saving multiple of the child relationships at the same time and then the parent has the incorrect number of associated records with it. Here is an example of the parent calling the child association of **selections**.\r\n\r\n![example](https://cl.ly/1V2B0x3u101e/Screen%20Shot%202016-08-23%20at%209.37.37%20AM.png)\r\n\r\nWithout changing anything other than calling flushCanonical again, the record length and currentState changes to reflect the correct number of associations. It starts at 1, but the correct number of selections should be 3, which is reflected after flushCanonical is called.\r\n\r\nSince it seems to be happening randomly (most the time it works, then occasionally it doesn't), I'm thinking it's a race condition between the various model states. Trying to get an example so it's repeatable, but not having luck having it consistently replicated. \r\n\r\nIs this a known issue, or any ideas on how to fix it? It is happening for sure between Ember data 2.5-2.7.\r\n\r\n**Update**\r\n\r\nI added some logging to get a better idea of what is happening. \r\n\r\nThe logging code is \r\n\r\n```\r\nDS.ManyArray.reopen({\r\n    flushCanonical() {\r\n      let canonicalStateLengthBefore = this.canonicalState.length;\r\n      let toSetBefore = this.canonicalState.filter((internalModel) => !internalModel.isDeleted());\r\n      let notDeletedLengthBefore = toSetBefore.length;\r\n      let currentStateLengthBefore = this.currentState.length;\r\n      let newRecordsLengthBefore = this.currentState.filter((internalModel) => internalModel.isNew() && toSetBefore.indexOf(internalModel) === -1).length;\r\n      this._super(...arguments);\r\n      let canonicalStateLengthAfter = this.canonicalState.length;\r\n      let toSetAfter = this.canonicalState.filter((internalModel) => !internalModel.isDeleted());\r\n      let notDeletedLengthAfter = toSetAfter.length;\r\n      let currentStateLengthAfter = this.currentState.length;\r\n      let newRecordsLengthAfter = this.currentState.filter((internalModel) => internalModel.isNew() && toSetAfter.indexOf(internalModel) === -1).length;\r\n      Ember.Logger.info(`${this.type.modelName}: canonicalState ${canonicalStateLengthBefore} / ${canonicalStateLengthAfter} - currentState ${currentStateLengthBefore} / ${currentStateLengthAfter} - Not Deleted ${notDeletedLengthBefore} / ${notDeletedLengthAfter} - New ${newRecordsLengthBefore} / ${newRecordsLengthAfter}`);\r\n    }\r\n  });\r\n```\r\n\r\nWhen it gets the error and the counts are off the first time the information is:\r\n\r\n`order/selection: canonicalState 4 / 4 - currentState 4 / 3 - Not Deleted 3 / 3 - New 0 / 0`\r\n\r\nThen after manually running flushCanonical again, the logging is:\r\n\r\n`order/selection: canonicalState 4 / 4 - currentState 3 / 4 - Not Deleted 3 / 3 - New 1 / 1`\r\n\r\nIt appears that the new records aren't getting synced in the first time. \r\n\r\nStarting to think the issue is coming down to saving multiple new child records at the same time. If they are all saving and then calling flushCanonical at the same time as others, the internal collections are getting called and set multiple times. Which could cause errors in the collection. Maybe flushCanonical needs to be scheduled like in the situation of belongsTo"},"comment":{"url":"https://api.github.com/repos/emberjs/data/issues/comments/253518333","html_url":"https://github.com/emberjs/data/issues/4507#issuecomment-253518333","issue_url":"https://api.github.com/repos/emberjs/data/issues/4507","id":253518333,"user":{"login":"aaronbhansen","id":1092833,"avatar_url":"https://avatars.githubusercontent.com/u/1092833?v=3","gravatar_id":"","url":"https://api.github.com/users/aaronbhansen","html_url":"https://github.com/aaronbhansen","followers_url":"https://api.github.com/users/aaronbhansen/followers","following_url":"https://api.github.com/users/aaronbhansen/following{/other_user}","gists_url":"https://api.github.com/users/aaronbhansen/gists{/gist_id}","starred_url":"https://api.github.com/users/aaronbhansen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaronbhansen/subscriptions","organizations_url":"https://api.github.com/users/aaronbhansen/orgs","repos_url":"https://api.github.com/users/aaronbhansen/repos","events_url":"https://api.github.com/users/aaronbhansen/events{/privacy}","received_events_url":"https://api.github.com/users/aaronbhansen/received_events","type":"User","site_admin":false},"created_at":"2016-10-13T13:49:30Z","updated_at":"2016-10-13T13:49:30Z","body":"The deleted records are expected to remain until they are saved. That was a change in 2.0 so you could reference them still. \r\n\r\nBut we've definitely keep hitting situations where we have flush the canonicalState after all child saves have been completed or we have missing records from the collections.  "}}
{"actor":{"display_login":"aaronbhansen"}}