Repository: electron
Issue: useSystemPicker when toggled on/off makes getDisplayMedia hang/crash
Labels: platform/macOS,bug :beetle:,33-x-y,34-x-y,35-x-y

### Preflight Checklist

- [x] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/main/CONTRIBUTING.md) for this project.
- [x] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [x] I have searched the [issue tracker](https://www.github.com/electron/electron/issues) for a bug report that matches the one I want to file, without success.

### Electron Version

33.3.1

### What operating system(s) are you using?

macOS

### Operating System Version

15.2

### What arch are you using?

arm64 (including Apple Silicon)

### Last Known Working Electron version

N/A

### Expected Behavior

We should be able to change the `setDisplayMediaRequestHandler` as many times as we wish, with different options, without it breaking `getDisplayMedia`.

Sometimes, but not always, it triggers a SIGTRAP:

```
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x10c051e0f00> was mutated while being enumerated.'
*** First throw call stack:
(
        0   CoreFoundation                      0x0000000190062e80 __exceptionPreprocess + 176
        1   libobjc.A.dylib                     0x000000018fb4acd8 objc_exception_throw + 88
        2   CoreFoundation                      0x0000000190109c34 -[__NSSingleObjectEnumerator init] + 0
        3   ScreenCaptureKit                    0x00000001f31da658 -[SCContentSharingPicker contentPickerDidSelectFilter:forStream:] + 436
        4   ReplayKit                           0x00000001da3c7208 -[RPScreenRecorder contentPickerDidSelectFilter:forStream:] + 84
        5   ReplayKit                           0x00000001da3d5944 -[RPDaemonProxy contentPickerDidSelectFilter:forStream:] + 224
        6   CoreFoundation                      0x000000018ffd0f94 __invoking___ + 148
        7   CoreFoundation                      0x000000018ffd0e0c -[NSInvocation invoke] + 428
        8   ReplayKit                           0x00000001da3d0ea8 -[RPDaemonProxy connection:handleInvocation:isReply:] + 316
        9   Foundation                          0x0000000191b685a4 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:reply:flags:] + 1108
        10  Foundation                          0x0000000191b69e10 message_handler_message + 88
        11  Foundation                          0x0000000191184840 message_handler + 152
        12  libxpc.dylib                        0x000000018fc173ac _xpc_connection_call_event_handler + 144
        13  libxpc.dylib                        0x000000018fc15b00 _xpc_connection_mach_event + 1120
        14  libdispatch.dylib                   0x000000018fd55674 _dispatch_client_callout4 + 20
        15  libdispatch.dylib                   0x000000018fd71c88 _dispatch_mach_msg_invoke + 464
        16  libdispatch.dylib                   0x000000018fd5ca38 _dispatch_lane_serial_drain + 352
        17  libdispatch.dylib                   0x000000018fd729dc _dispatch_mach_invoke + 456
        18  libdispatch.dylib                   0x000000018fd5ca38 _dispatch_lane_serial_drain + 352
        19  libdispatch.dylib                   0x000000018fd5d764 _dispatch_lane_invoke + 432
        20  libdispatch.dylib                   0x000000018fd689a0 _dispatch_root_queue_drain_deferred_wlh + 288
        21  libdispatch.dylib                   0x000000018fd681ec _dispatch_workloop_worker_thread + 540
        22  libsystem_pthread.dylib             0x000000018ff043d8 _pthread_wqthread + 288
        23  libsystem_pthread.dylib             0x000000018ff030f0 start_wqthread + 8
)
```

### Actual Behavior

After toggling `useSystemPicker` on/off/on again, all calls to `getDisplayMedia` using the system picker hang indefinitely. This is important that this feature works, as it's used for loopback audio, until chrome eventually implements it.

### Testcase Gist URL

_No response_

### Additional Information

Toggling...

```
session.defaultSession.setDisplayMediaRequestHandler(() => {}, {useSystemPicker: true});
getDisplayMedia()
^ -- end it
session.defaultSession.setDisplayMediaRequestHandler(() => {}, {useSystemPicker: false});
getDisplayMedia()
^ -- end it
session.defaultSession.setDisplayMediaRequestHandler(() => {}, {useSystemPicker: true});
getDisplayMedia()
^ -- hangs or crashes
```