zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.38k stars 294 forks source link

Program hangs when a client method is called within a goroutine? #199

Closed amirrezapanahi closed 2 years ago

amirrezapanahi commented 2 years ago

I have these goroutines that run a function called "searchAlbums" for different related artists

relatedArtists = array of related Artists objects recommended = buffered channel which has a capacity of 6 c = *gin.Context

    for len(recommended) != cap(recommended) {
        go func() {
            recommended, err = searchAlbums(relatedArtists[:len(relatedArtists)*(1/3)], originalColorScheme, recommended, c)
            if err != nil {
                        //handle error
            }
        }()
        go func() {
            recommended, err = searchAlbums(relatedArtists[len(relatedArtists)*(1/3):len(relatedArtists)*(2/3)], originalColorScheme, recommended, c)
            if err != nil {
                        //handle error  
            }
        }()
        go func() {
            recommended, err = searchAlbums(relatedArtists[len(relatedArtists)*(2/3):], originalColorScheme, recommended, c)
            if err != nil {
                        //handle error  
            }
        }()
    }

My main issue comes from within the searchAlbums function where I invoke the client's getArtistAlbums method

albums, err := client.GetArtistAlbums(spotify.ID(artist.Id))

The program seems to hang once it calls this method and never resolves

When I call the searchAlbums function in the main thread it runs fine. Any idea why this is happening once I start introducing concurrency? Bare in mind I'm still a beginner with concurrency and go, so any insight will be helpful.

strideynet commented 2 years ago

This feels like potentially an issue with your concurrency implementation, rather than our library.

I highly recommend seeking help on the Gopher's slack. I'm usually around on there as well, so I may be able to give a better hand there.