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

[Ember Data 4.7] Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to #8262

Closed
mkszepp opened this issue Oct 27, 2022 · 9 comments · Fixed by #8307

Comments

@mkszepp
Copy link
Contributor

mkszepp commented Oct 27, 2022

Reproduction

  1. Use a stored ember data model, which was returned from server
  2. add in a hasMany (which has already some stored models) a new ember model with createRecord without saving
  3. remove added model with destroyRecord
  4. Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to by accessing the function some, filter....

The problem doens't appear when i use in step 3 rollbackAttributes instead of destroyRecord/deleteRecord

Description

For better understanding i have created a new clean ember project with a example (branch identifier-subscribe).
Example repo: https://github.com/mkszepp/ember-stable-identifier/tree/identifier-subscribe

So i have check the difference between rollbackAttributes and destroyRecord i found a possible reason.

The difference is, that the unloadRecord calls immidiatly the unloadRecord while deleteRecord in the next run loop.

rollbackAttributes() {
const { currentState } = this;
const { isNew } = currentState;
storeFor(this)._join(() => {
recordDataFor(this).rollbackAttrs(recordIdentifierFor(this));
this.errors.clear();
currentState.cleanErrorRequests();
if (isNew) {
this.unloadRecord();
}
});
}

deleteRecord(record: RecordInstance): void {
if (DEBUG) {
assertDestroyingStore(this, 'deleteRecord');
}
const identifier = peekRecordIdentifier(record);
const recordData = identifier && this._instanceCache.peek({ identifier, bucket: 'recordData' });
assert(`expected a recordData instance to exist for the record`, recordData);
this._join(() => {
recordData.setIsDeleted(identifier, true);
if (recordData.isNew(identifier)) {
run(() => {
this._instanceCache.unloadRecord(identifier);
});
}
});
}

Is it possible, that the reported bug is a timing problem?

An other problem is, when i create a new record and delete it (with deleteRecord) the model stays in the store and the states (isNew, isDeleted...) are false and dirtyType is empty string. Maybe those two problems are interconnected

Versions

Ember Js: 4.7.0+
Ember Data: 4.7.3

@mkszepp mkszepp changed the title Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to [Ember 4.7] Oct 27, 2022
@mkszepp mkszepp changed the title Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to [Ember 4.7] [Ember 4.7] Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to Oct 27, 2022
@mkszepp mkszepp changed the title [Ember 4.7] Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to [Ember Data 4.7] Error: Assertion Failed: Expected to receive a stable Identifier to subscribe to Oct 27, 2022
@runspired
Copy link
Contributor

I suspect you haven't diagnosed what has gone wrong here quite right yet, the reason it appears there are separate run-loops is because we immediately fully dismantle everything for new records when any of unloadRecord/deleteRecord/rollbackAttributes/destroyRecord are called. Any one of these when called should fully complete the process.

@runspired
Copy link
Contributor

runspired commented Oct 29, 2022

I'm unable to reproduce this using the repo you provided, note: there is one difference in what I tested: npm install resulted in a new package.lock for me because the existing lockfile was generated by an outdated npm

@mkszepp
Copy link
Contributor Author

mkszepp commented Oct 31, 2022

@runspired i'm using nodejs v14

The error occures, when i access to a deleted model, like in the provided repo. Before i'm deleting the record there is necessary to use find or some.

https://github.com/mkszepp/ember-stable-identifier/blob/241d3f25559f98068a465c1c25a565af90f05af6/app/controllers/application.js#L61-L66

@runspired
Copy link
Contributor

@mkszepp yes, what I am saying is that the provided reproduction does not reproduce the error at all for me. If it does for you, then there's likely something wrong with your install as that would be the only difference.

@mkszepp
Copy link
Contributor Author

mkszepp commented Nov 2, 2022

@runspired i have now updated nodejs version to v18.12.0 & npm to v8.19.2, because nodejs v14.x brings only npn v6.14.17

I have now commitet the new package.lock file (i have deleted node_model folder + the old package.lock file before regenerate), but the update has not solved my problem.

grafik

subscribe(identifier: StableRecordIdentifier, callback: NotificationCallback): UnsubscribeToken {
assert(`Expected to receive a stable Identifier to subscribe to`, isStableIdentifier(identifier));
let map = Cache.get(identifier);
if (!map) {
map = new Map();
Cache.set(identifier, map);
}
let unsubToken = DEBUG ? { _tokenRef: tokenId++ } : {};
map.set(unsubToken, callback);
Tokens.set(unsubToken, identifier);
return unsubToken;
}

I have also tested the same code in ember data v4.6.4 by replacing .some with .any (because .some was added in 4.7). In v4.6 it works without errors.
The error starts with v4.7.0

I have activated the identifier debug, maybe this is helpful:
First log line was click on "add page"
all other lines by removing.
grafik

I can reproduce this error also on other computers

@mkszepp
Copy link
Contributor Author

mkszepp commented Nov 11, 2022

I have tried to update to v4.8.3 (removing embroider, cause of adapter problem), but the problem is still present.

@runspired
Copy link
Contributor

still not sure how to replicate this, the repo provided did not replicate this for me with the steps provided. Would be happy to setup a video call to go through it together since probably I'm missing some context.

@mkszepp
Copy link
Contributor Author

mkszepp commented Nov 14, 2022

@runspired Thank you for your patience. Yes this woud be great, so i can show you the bug on my computer. How can we get in contact? Which channel do you prefer?

@mkszepp
Copy link
Contributor Author

mkszepp commented Nov 22, 2022

@runspired it would be great to geat a release with this and also other bugfixes in LTS 4.8.x

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

Successfully merging a pull request may close this issue.

2 participants