Change PlaybackSession createFromOld to use upsert instead of create #3436
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes #2662
While I'm not in the position to reproduce this (I don't own an iPhone), I'm pretty sure why the crash happens.
It is very likely due to duplicate /session/local requests (retries?) sent in rapid succession from the client and handled asynchronously on the server, causing a race condition.
While Request A is awaiting
Request B is also making the same call.
both receive a null response, so each of them calls create() on the PlaybackSession model, and one of them succeeds while the other fails with UniqueConstraintError because it is trying to create a row with the same primary key.
The fix is simple - since the requests are likely identical, it is safe to use upsert instead of create, which will succeed for both A and B (this of course doesn't prevent the race condition - doing that would involve using transactions, which I think is an overkill in this case)