You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I have not encountered a bug related to this. However, it is evident from the way KVO and subscription to NotificationCenter is employed that AVPlayerSubscription is not thread safe. This is because notifications within AVFoundation as well as KVO updates may be called on different threads. Consider this example AVPlayerSubscription.swift:49-66,
rateObserver = player.observe(\.rate, options:[.old,.new]){[weak self] player, change in
guard let oldRate = change.oldValue else{return}
guard let newRate = change.newValue else{return}
if oldRate !=0 && newRate ==0{ // paused
self?.lastPauseTime = player.currentTime()self?.track(MediaPauseEvent())}else if oldRate ==0 && newRate !=0{ // started playing
// when the current time diverges significantly, i.e. more than 1 second, from what it was when last paused, track a seek event
if let lastPauseTime =self?.lastPauseTime {
if abs(player.currentTime().seconds - lastPauseTime.seconds)>1{self?.track(MediaSeekEndEvent())}}self?.lastPauseTime =nilself?.track(MediaPlayEvent())}}
Here self is accessed within the closure passed into the KVO. This closure may be called on any thread AVFoundation wants to. Oftentimes it will be the main thread, however, this is highly dependent on the state of the device on which the code is executing, and thus hard to reproduce. The same goes for this code AVPlayerSubscription.swift:94-105:
Describe the bug
I have not encountered a bug related to this. However, it is evident from the way KVO and subscription to
NotificationCenter
is employed thatAVPlayerSubscription
is not thread safe. This is because notifications withinAVFoundation
as well as KVO updates may be called on different threads. Consider this exampleAVPlayerSubscription.swift:49-66
,Here
self
is accessed within the closure passed into the KVO. This closure may be called on any threadAVFoundation
wants to. Oftentimes it will be the main thread, however, this is highly dependent on the state of the device on which the code is executing, and thus hard to reproduce. The same goes for this codeAVPlayerSubscription.swift:94-105
:Notifications may be sent on different threads. The code in the body of the method above is also thread agnostic and lacking synchronisation.
The text was updated successfully, but these errors were encountered: