From a811edb236cdb59659299bf61019912db52b3a17 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Sat, 15 Feb 2020 10:03:11 -0800 Subject: [PATCH] Fix race condition for notification stream count when stopping playback --- plexpy/notification_handler.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 2901bb80..1afbe47e 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -536,8 +536,15 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m ap = activity_processor.ActivityProcessor() sessions = ap.get_sessions() - stream_count = len(sessions) user_sessions = ap.get_sessions(user_id=session.get('user_id')) + + # Filter out the session_key from the database sessions for playback stopped events + # to prevent race condition between the database and notifications + if notify_action == 'on_stop': + sessions = [s for s in sessions if str(s['session_key']) != notify_params['session_key']] + user_sessions = [s for s in user_sessions if str(s['session_key']) != notify_params['session_key']] + + stream_count = len(sessions) user_stream_count = len(user_sessions) # Generate a combined transcode decision value