adamint / spotify-web-api-kotlin

Spotify Web API wrapper for Kotlin, Java, JS, and Native - Targets JVM, Android, JS (browser), Native (Desktop), and Apple tvOS/iOS. Includes a Spotify Web Playback SDK wrapper for Kotlin/JS, and a spotify-auth wrapper for Kotlin/Android.
https://adamint.github.io/spotify-web-api-kotlin-docs/
MIT License
194 stars 21 forks source link

ClientPlaylistApi#addPlayablesToClientPlaylist() race condition when adding more than 100 tracks #298

Closed Ben2356 closed 2 years ago

Ben2356 commented 2 years ago

According to this method's documentation and the official Spotify Web API documentation, the order the tracks are added is dependent on the order of the tracks that is supplied to the actual API endpoint (and thus it will be written to the body of the actual POST request). I've been investigating an issue where I've noticed that sometimes when calling this endpoint when adding greater than 100 songs the ordering of the songs is incorrect. In my implementation the ordering of the songs is very important so when I was trying to understand what was happening I noticed this segment of code here which explains the problem https://github.com/adamint/spotify-web-api-kotlin/blob/master/src/commonMain/kotlin/com.adamratzman.spotify/http/Endpoints.kt#L52-L57 . The problem is that each of these chunked requests are started in a coroutine that then turns into a race condition as the first chunked request that completes may not necessarily be the first 100 songs and instead it's a first come first serve with which chunked request is available first. This ends up having the potential of rearranging the order of the songs that was supplied to the endpoint thus violating the guarantee on the ordering (at the API library level).

adamint commented 2 years ago

I need to investigate this for other client playlist methods as well.