Skip to content

Fix NullPointerException in MprisReceiverPlayer

Daniel Tang requested to merge danielt/kdeconnect-android:fix-mpris-npe into master

Summary

Today, KDE Connect crashed when I had NewPipe open and tapped to play a new music video in the background. This MR attempts to fix the problem.

The problem is caused by a TOCTOU getting the PlaybackState. The problematic code was using a different style of doing things than the other methods. By making it use the logic the other methods use (without the TOCTOU), we should be able to avoid the NPE.

Test Plan

Before:

I got the following stack trace. However, the problem is not consistently reproducible.

01-26 23:36:15.562 14578 14578 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.media.session.PlaybackState.getPosition()' on a null object reference
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at org.kde.kdeconnect.Plugins.MprisReceiverPlugin.MprisReceiverPlayer.getPosition(MprisReceiverPlayer.java:151)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at org.kde.kdeconnect.Plugins.MprisReceiverPlugin.MprisReceiverPlugin.sendMetadata(MprisReceiverPlugin.java:218)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at org.kde.kdeconnect.Plugins.MprisReceiverPlugin.MprisReceiverCallback.onPlaybackStateChanged(MprisReceiverCallback.java:34)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at android.media.session.MediaController$MessageHandler.handleMessage(MediaController.java:1203)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:233)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8010)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
01-26 23:36:15.562 14578 14578 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)

After:

KDE Connect should no longer crash on this. Since this is a fix for a race condition, which cannot be consistently reproduced, I can say for certain whether the problem is fixed. I didn't analyze the root cause, but logically analyzing the surrounding code and the stack trace, this change should fix it.

Merge request reports

Loading