Bug 327547

Summary: Last.fm Invalid session key error not handled: scrobbles silently lost until amarok restarts
Product: [Applications] amarok Reporter: kiwiiii
Component: Services/Last.fmAssignee: Amarok Bugs <amarok-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: matej, phil.tgun, sam
Priority: NOR    
Version First Reported In: 2.8.90 (2.9 beta)   
Target Milestone: 2.9   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description kiwiiii 2013-11-13 08:37:03 UTC
When not using amarok for a long period of time (several weeks), but still having it open (in my case it was a suspend to disk of 2 weeks), last.fm returns an error code="9" "Invalid session key - Please re-authenticate", but amarok doesn't seem to handle this.
As a result, currently played tracks are silently lost and never correctly saved in last.fm: no user notification is shown for the error, and I lost 2 others weeks of scrobbling before realizing it didn't work. After a restart of amarok, everything worked well again.

.xsession-error:
QMap(("album", "xxxx")("albumArtist", "xxxx")("artist", "xxxx")("context", "")("duration", "519")("method", "Track.updateNowPlaying")("track", "xxxx")) 
"<?xml version="1.0" encoding="utf-8"?>
<lfm status="failed">
<error code="9">
    Invalid session key - Please re-authenticate
</error>
</lfm>
" 
QMetaObject::invokeMethod: No such method App::onWsError(lastfm::ws::Error)
"
    Invalid session key - Please re-authenticate
" 9 


Reproducible: Didn't try

Steps to Reproduce:
1. Start amarok, start scrobbling a track.
2. Suspend the computer for 2 weeks
3. Resume the computer and play a new track
Actual Results:  
Track is not scrobbled, silent error code 9 "Invalid session key - Please re-authenticate" ignored.

Expected Results:  
Handle the error code 9 and re-authenticate.
For other unhandled errors: at least notify the user something went wrong.
Comment 1 Myriam Schweingruber 2013-11-13 08:44:58 UTC
Please upgrade to Amarok 2.8 and then try again, 2.7.1 is just too far away in terms of changes.
Comment 2 kiwiiii 2013-11-18 00:37:51 UTC
It's not easy to reproduce the issue: I can't just let my amarok process not used for two weeks..

In this example of liblastfm they explain a little more the error handling:
https://github.com/lastfm/liblastfm/blob/master/demos/demo2.cpp

private slots:
    void onWsError( lastfm::ws::Error e )
    {
        // QNetworkReply will invoke this slot on application level errors
        // mostly this is only stuff like Ws::InvalidSessionKey and
        // Ws::InvalidApiKey    
        qWarning() << e;
    }


From the liblastfm code (XmlQuery.cpp):
    if ( d->error.enumValue() != lastfm::ws::NoError )
    {
        qDebug() << bytes;

        switch ( d->error.enumValue() )
        {
            case lastfm::ws::OperationFailed:
            case lastfm::ws::InvalidApiKey:
            case lastfm::ws::InvalidSessionKey:
                // NOTE will never be received during the LoginDialog stage
                // since that happens before this slot is registered with
                // QMetaObject in App::App(). Neat :)
                QMetaObject::invokeMethod( qApp, "onWsError", Q_ARG( lastfm::ws::Error, d->error.enumValue() ) );
                break;
            default:
                //do nothing
            break;
        }
    }



And finally from Amarok git master: 
- no onWsError, this explains the log "QMetaObject::invokeMethod: No such method App::onWsError(lastfm::ws::Error)".
- no InvalidSessionKey, so the error code isn't properly handled, so the issue is probably still there.
- error handling in src/services/lastfm/ScrobblerAdapter.cpp only returns a generic error in scrobble(), and logs a warning in slotScrobblesSubmitted(). This hasn't changed much since tag v2.7.1.

In liblastfm (Audioscrobbler.cpp) the api error code is set via mTrack.setScrobbleError(), so we could check for that in ScrobblerAdapter::slotScrobblesSubmitted(), and restart the session there.
Or we could add onWsError to avoid the warning anyway.
liblastfm keeps the tracks in its cache when a temporary error like that happens, so we only need to do a submit() again after the re-authentication.

Maybe other parts of amarok that use liblastfm could gain from that too.
Comment 3 Mark Kretschmann 2013-11-18 06:30:13 UTC
Would you be interested in writing a patch for Amarok to fix these issues?
Comment 4 kiwiiii 2013-11-18 08:42:21 UTC
Yes, if you tell me which solution to implement, as I'm not familiar at all with Amarok source code.

The onWsError could help catch every invalid session key error, not just the scrobbling, and start a re-authentication (but where should I add this code in amarok?)
And  ScrobblerAdapter::slotScrobblesSubmitted could check the error and resubmit in case of Invalid session key, if we know the onWsError has already finished it's re-authentication. Or we could just do a scrobbler.submit() in LastFmService::continueReconfiguring() (it does nothing if the cache is empty, so no network overhead in the usual flow). The later solution seems better to me.
Comment 5 Matěj Laitl 2013-11-18 18:33:49 UTC
(In reply to comment #4)
> Yes, if you tell me which solution to implement, as I'm not familiar at all
> with Amarok source code.
> 
> The onWsError could help catch every invalid session key error, not just the
> scrobbling, and start a re-authentication (but where should I add this code
> in amarok?)

Please add that method to class App (src/App.h/cpp), guarded by #ifdef HAVE_LIBLASTFM. It would be a little glue that would call appropriate method in ScrobblerAdapter.

> And  ScrobblerAdapter::slotScrobblesSubmitted could check the error and
> resubmit in case of Invalid session key, if we know the onWsError has
> already finished it's re-authentication.

No, above solution is better.

> Or we could just do a scrobbler.submit() in LastFmService::continueReconfiguring() (it does
> nothing if the cache is empty, so no network overhead in the usual flow).

Yes please (I assume continueReconfiguring() gets caller after successful re-auth)
Comment 6 Myriam Schweingruber 2014-08-12 12:15:16 UTC
What is the status of this report?
Comment 7 kiwiiii 2014-08-12 14:40:23 UTC
(In reply to Myriam Schweingruber from comment #6)
> What is the status of this report?

I still have this on my to do list but I havn't started yet. I will hopefully have some time in September, but if someone else wants to implement the fix: do it, I don't want to block anything.
Comment 8 Myriam Schweingruber 2014-08-12 16:13:33 UTC
No problem, September is coming soon anyway :)
Comment 9 Philip 2016-05-28 18:41:59 UTC
Any updates on this bug? Apparently I have the same bug, but it happens at the start of amarok, and no tracks are scrobbled. I might have started having it after I updated by Kubuntu from 15 to 16, but I am not sure.

Amarok ver 2.8.80
Comment 10 Myriam Schweingruber 2016-05-28 20:22:17 UTC
(In reply to Philip from comment #9)
> Any updates on this bug? Apparently I have the same bug, but it happens at
> the start of amarok, and no tracks are scrobbled. I might have started
> having it after I updated by Kubuntu from 15 to 16, but I am not sure.
> 
> Amarok ver 2.8.80

Sadly no, never heard back from the dev who wanted to implement it :-(
Comment 11 Tuomas Nurmi 2024-07-11 21:55:25 UTC
Git commit 9c17c0f5af71c1ebe0150039c4d8c3204992158d by Tuomas Nurmi.
Committed on 11/07/2024 at 21:46.
Pushed by nurmi into branch 'master'.

Add general handler for last.fm errors

InvalidSessionKey handled specifically and user is notified, other errors don't
look like having any specific handling for them is necessary.

M  +2    -1    ChangeLog
M  +20   -0    src/App.cpp
M  +9    -0    src/App.h
M  +4    -0    src/services/gpodder/CMakeLists.txt
M  +4    -0    tests/CMakeLists.txt

https://invent.kde.org/multimedia/amarok/-/commit/9c17c0f5af71c1ebe0150039c4d8c3204992158d