Crash removing time observer from player

Hi,

We have a tvOS App with a custom player and we're getting some crashes trying to remove a periodicTimeObserver on a player instance:

Incident Identifier: 3FE68C1C-126D-4A16-BBF2-9F8D1E395548
Hardware Model:      AppleTV6,2
Process:             MyApp [2516]
Path:                /private/var/containers/Bundle/Application/B99FEAB0-0753-48FE-A7FC-7AEB8E2361C1/MyApp.app/MyApp
Identifier:          pt.appletv.bundleid
Version:             4.9.5 (2559)
AppStoreTools:       15A240a
AppVariant:          1:AppleTV6,2:16
Beta:                YES
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           pt.appletv.bundleid [317]

Date/Time:           2023-09-21 18:49:39.0241 +0100
Launch Time:         2023-09-21 18:38:34.6957 +0100
OS Version:          Apple TVOS 16.6 (20M73)
Release Type:        User
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: SIGNAL 6 Abort trap: 6
Terminating Process: MyApp [2516]

Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                	0x1914c12c8 __exceptionPreprocess + 160 (NSException.m:202)
1   libobjc.A.dylib               	0x190cfc114 objc_exception_throw + 56 (objc-exception.mm:356)
2   AVFCore                       	0x1c432b89c -[AVPlayer removeTimeObserver:] + 176 (AVPlayer.m:0)
3   CustomPlayer                  	0x10549f670 MyPlayerViewController.removePlayerObservers(_:) + 248 (MyPlayerViewController.swift:252)
4   CustomPlayer                  	0x10549c978 closure #1 in MyPlayerViewController.player.didset + 68 (MyPlayerViewController.swift:98)
5   CustomPlayer                  	0x10549be60 thunk for @escaping @callee_guaranteed () -> () + 28 (<compiler-generated>:0)
6   libdispatch.dylib             	0x190e5eef4 _dispatch_call_block_and_release + 24 (init.c:1518)
7   libdispatch.dylib             	0x190e60784 _dispatch_client_callout + 16 (object.m:560)
8   libdispatch.dylib             	0x190e6dd34 _dispatch_main_queue_drain + 892 (queue.c:7794)
9   libdispatch.dylib             	0x190e6d9a8 _dispatch_main_queue_callback_4CF + 40 (queue.c:7954)
10  CoreFoundation                	0x19142b038 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 (CFRunLoop.c:1780)
11  CoreFoundation                	0x19142569c __CFRunLoopRun + 2080 (CFRunLoop.c:3147)
12  CoreFoundation                	0x191424a3c CFRunLoopRunSpecific + 584 (CFRunLoop.c:3418)
13  GraphicsServices              	0x1980cab0c GSEventRunModal + 160 (GSEvent.c:2196)
14  UIKitCore                     	0x1da6fe6ec -[UIApplication _run] + 868 (UIApplication.m:3782)
15  UIKitCore                     	0x1da702bc4 UIApplicationMain + 148 (UIApplication.m:5372)
16  MyApp                    	    0x104418268 main + 176 (main.swift:12)
17  dyld                          	0x1ddd81744 start + 1832 (dyldMain.cpp:1165)

Thread 0 name:
Thread 0 Crashed:
0   libsystem_kernel.dylib        	0x0000000190fe69a8 __pthread_kill + 8 (:-1)
1   libsystem_pthread.dylib       	0x000000019109e440 pthread_kill + 208 (pthread.c:1670)
2   libsystem_c.dylib             	0x0000000190f5f8dc __abort + 124 (abort.c:155)
3   libsystem_c.dylib             	0x0000000190f5f860 abort + 132 (abort.c:126)
4   libc++abi.dylib               	0x0000000190da1fe0 abort_message + 128 (:-1)
5   libc++abi.dylib               	0x0000000190d92be8 demangling_terminate_handler() + 300
6   libobjc.A.dylib               	0x0000000190cda7d4 _objc_terminate() + 124 (objc-exception.mm:498)
7   FirebaseCrashlytics           	0x0000000105118754 FIRCLSTerminateHandler() + 340 (FIRCLSException.mm:452)
8   libc++abi.dylib               	0x0000000190da15c0 std::__terminate(void (*)()) + 12 (:-1)
9   libc++abi.dylib               	0x0000000190da1570 std::terminate() + 52
10  libdispatch.dylib             	0x0000000190e60798 _dispatch_client_callout + 36 (object.m:563)
11  libdispatch.dylib             	0x0000000190e6dd34 _dispatch_main_queue_drain + 892 (queue.c:7794)
12  libdispatch.dylib             	0x0000000190e6d9a8 _dispatch_main_queue_callback_4CF + 40 (queue.c:7954)
13  CoreFoundation                	0x000000019142b038 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 (CFRunLoop.c:1780)
14  CoreFoundation                	0x000000019142569c __CFRunLoopRun + 2080 (CFRunLoop.c:3147)
15  CoreFoundation                	0x0000000191424a3c CFRunLoopRunSpecific + 584 (CFRunLoop.c:3418)
16  GraphicsServices              	0x00000001980cab0c GSEventRunModal + 160 (GSEvent.c:2196)
17  UIKitCore                     	0x00000001da6fe6ec -[UIApplication _run] + 868 (UIApplication.m:3782)
18  UIKitCore                     	0x00000001da702bc4 UIApplicationMain + 148 (UIApplication.m:5372)
19  MyApp                    	    0x0000000104418268 main + 176 (main.swift:12)
20  dyld                          	0x00000001ddd81744 start + 1832 (dyldMain.cpp:1165)

The code is:

    @objc
    public dynamic var player: AVPlayer? {
        willSet {
            removeThumbnails()
        }

        didSet {
            DispatchQueue.main.async { [weak self] in
                guard let self else {
                    return
                }
                self.removePlayerObservers(oldValue)
                self.addPlayerObservers(self.player)
            }
        }
    }

    func removePlayerObservers(_ player: AVPlayer?) {
        if let periodicTimeObserver = periodicTimeObserver {
            player?.removeTimeObserver(periodicTimeObserver)
            self.periodicTimeObserver = nil
        }
    }

What could be the problem?

Thank you

Crash removing time observer from player
 
 
Q