mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[GH-ISSUE #937] Hanging track search #562
Labels
No labels
api-bug
bug
dependencies
documentation
duplicate
enhancement
external-ide
headless-mode
implicit-grant-flow
invalid
missing-endpoint
pr-welcome
private-api
pull-request
question
spotipy3
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotipy#562
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Rsalganik1123 on GitHub (Feb 1, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/937
Describe the bug
Sometimes, the
sp.track()just hangs without an error and without a return but at the same time,sp.audio_features()works properly.Your code
And when I call
sp.audio_features(uri)it responds immediately but when I callsp.track(uri)it can hang up to more than 3 minutes before I ctrl+C out of the code even though I set a timeout value.After reading a few GitHub issues (see #913 , #862, #844, etc) it seems that there may be an issue with the spotify token going stale but I am not sure how to fix this since I am following the methodology of the README for initializing my credentials. I know that the URI is valid because it was working for about 30 minutes and then it stopped. Since I doing an augmentation for a large dataset, I need to be able to refresh my token.
Expected behavior
Throwing either an error or returning the search query.
Output
None and I'm not sure how to debug for one.
Environment:
Additional context
Add any other context about the problem here.
@stephanebruckert commented on GitHub (Feb 1, 2023):
I'm not sure why you have
because spotipy is supposed to refresh the token automatically without it.
It would be good to have a complete code that we could run to show the problem. Even if's doing a loop over the same track and it takes 30 minutes. Any chance you can provide that?
@Rsalganik1123 commented on GitHub (Feb 1, 2023):
Yes, of course! I was trying to give you a simple MVP. But if you'd like my full code, here it is.
Given a csv with spotify uris I load the albums and artists associated with them and then also scrape info about those albums/artists.
I separated the code into a few files.
In
org_info.py:in
utils/spotify_connector.py:And finally in launch.py:
@stephanebruckert commented on GitHub (Feb 1, 2023):
Thanks! Would you have a sample csv too please?
@Rsalganik1123 commented on GitHub (Feb 1, 2023):
Yes! It can be downloaded from this link. Select the option
spotify-uris.tsv.bz2.The loop itself runs for about 30 minutes and then cuts out. As evidenced by the output here:
The code doesn't exit, just stays hanging.
Thank you for your help!
@stephanebruckert commented on GitHub (Feb 1, 2023):
@Rsalganik1123 sorry this is a bit complex to run. There is no main function.
Please can you provide a minimal reproducible example?
For example something that would be as simple as this and would reproduce the issue for you:
Btw I have been running this ^ for 1.5 hours and it's still running well. Anything that would change on your side?
Also, your code contains
refresh_spotify(). As mentionned above, you shouldn't manually refresh it as it is done automatically by spotipy.@Rsalganik1123 commented on GitHub (Feb 1, 2023):
Hello, using the MVE that you suggested, I will place the code below, I am able to reproduce the error.
the output is:
And now it's hanging. Could it be because I'm running the code from a distributed server?
Thank you!
@stephanebruckert commented on GitHub (Feb 1, 2023):
Interesting! What do you mean exactly by distributed server?
@Rsalganik1123 commented on GitHub (Feb 1, 2023):
I'm running on an academic server which uses a SLURM scheduler to dispatch jobs to various compute nodes. To be honest, I'm not super familiar with the appropriate terminology to describe the architecture.
@stephanebruckert commented on GitHub (Feb 1, 2023):
Really not sure, it still should be one job running on one node with internet access. Perhaps there are some firewalls on the way or the requests are blocked. It would be great if you could try your code outside this server (for example locally) and/or with another internet connection.
It's really strange that it stops after 2 requests only? Did you say the same was not happening with
sp.audio_features(uri)?We can try to show debug logs, just add this to the top of the file:
It should show something like the following (just make sure to remove your access token after
'Authorization': 'Bearer...):Also, when you cancel it with
Ctrl+Cdoes it show a stack trace? If yes please share it.@Rsalganik1123 commented on GitHub (Feb 1, 2023):
I can try it locally and report back later today.
In the meantime, regarding all your other suggestions:
And then it remains hanging.
If I run it using
sp.audio_features()there is no problem - like the output is as expected - and I stop it afteri=100When I cancel with Ctrl + C the stack trace is:
@stephanebruckert commented on GitHub (Feb 1, 2023):
Ha! It sounds like this is related https://github.com/spotipy-dev/spotipy/issues/892
Seeing it stopped at
sleep_for_retry(), the library is definitely sleeping/waiting and the reason could be a timezone difference. We are reading the token value "expires_at" which is a unix timestamp (UTC). My guess is that your servers likely don't use UTC, so the time that was given to the library doesn't make sense. Either it could be waiting a few extra hours for the time difference, or indefinitely depending on the timezone (+/-UTC) and the type of bug.I'll have a better look to try and confirm this.
@Rsalganik1123 commented on GitHub (Feb 1, 2023):
Interesting! But then why would that differ from sp.audio_features?
Thank you for your help!
@stephanebruckert commented on GitHub (Feb 1, 2023):
Actually that was a false lead. It's hanging while trying to retry which is unrelated to the token expiry time.
This discussion is quite interesting https://github.com/spotipy-dev/spotipy/issues/766. It mentions the possibility that the Spotify API can request to retry in 3600 seconds. I'm not sure why it would be so high, maybe a bug on their side.
In the meantime you could try to use this answer https://github.com/spotipy-dev/spotipy/issues/766#issuecomment-1005102900 to force a failure, but still wait 1 sec before trying again.
Notice it prints the "retry-after" value given by the API. What value do you get?
Perhaps for
audio_features, the Spotify API doesn't return a "retry_after" value that is so high. Can you try it and check if the "retry-after" value looks different?@Rsalganik1123 commented on GitHub (Feb 2, 2023):
Yes! Thank you, I will try it later today and report back.
@Rsalganik1123 commented on GitHub (Feb 2, 2023):
This works.
A sample of the output for running
sp.tracks()(with the exact copy of the code above):Trying the same thing by replacing
sp.tracks()withsp.audio_features()doesn't cause any exceptions so it never prints the retry-value.@Rsalganik1123 commented on GitHub (Feb 2, 2023):
Also, running with my local machine doesn't have any issues with latency or hanging. So I'm not sure why but it's something about running on the nodes that's causing problems. Could it be because I have an allocation of 2 cpus and there is some sort of multi-threading happening that causes the time out?
@stephanebruckert commented on GitHub (Feb 2, 2023):
No I don't think anything that elaborate is happening.
Your
retry-afterheader is telling you to wait about 12 minutes, so it's not a bug but just spotify telling you that your token has been / is being over-used. I would try to start fresh from a new token. On your node/server, try to delete the.cachefile in the same folder as your code (it contains your token). Just dorm .cachefrom a terminal.Make sense!
@Rsalganik1123 commented on GitHub (Feb 2, 2023):
I see. So for now the only solution is to: A. clear the cache before scraping, B. manually set the retry-after to 1 second ? Is that correct?
@Rsalganik1123 commented on GitHub (Feb 2, 2023):
Unfortunately, the error is back. Despite cleaning my cache I am getting a 429 error and a 'retry-after' value: 63397
@stephanebruckert commented on GitHub (Feb 3, 2023):
At this point it's not really a spotipy error/bug anymore as you are just being rate-limited by Spotify. Why such a high retry-after value I am not sure but it could be related to the node. Here are other ideas you could experiment with:
time.sleep()a few milliseconds before each spotify call, to see if helps reducing the retry-after.Let us know what your findings are or if you have any other questions, but for now I think it's fair to close this as there is not much we can do inside spotipy.
@BAndriuss commented on GitHub (Feb 22, 2023):
I have a function that retrieves me all songs from a playlist and it does it succesfully when called. But when try to use other function that includes sp.album() or sp.album_tracks() the program gets stuck whenever executing album() or album_tracks(). It cant be due to rate-limit cause everything works when not using album() or album_tracks() nor with token/keys.
@stephanebruckert commented on GitHub (Mar 30, 2023):
Let's continue this discussion in https://github.com/spotipy-dev/spotipy/issues/913