Repository: electron
Issue: Inconsistent Behavior of desktopCapturer.getSources in Electron 32 on macOS 12+
Labels: platform/macOS,bug :beetle:,component/desktopcapturer,32-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

32.2.6

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

macOS

### Operating System Version

Mac OS 14

### What arch are you using?

x64

### Last Known Working Electron version

27.0.0

### Expected Behavior

Expected Behavior
The desktopCapturer.getSources method should return a list of screen sources (e.g., available screens and windows) consistently on the first call.
The Electron app should remain active and not close unexpectedly.
The code should work identically across all supported platforms (macOS, Linux, and Windows) and macOS versions (10.15 and above).

### Actual Behavior

Actual Behavior
On macOS 12 and later:
The first or second call to desktopCapturer.getSources does not return any sources.
The Electron app closes unexpectedly during these initial calls.
After 2–3 attempts, the method starts working as intended, returning the sources and keeping the app active.
On macOS 10.15, Linux, and Windows:
The desktopCapturer.getSources method works correctly without any issues, even on the first call.

### Testcase Gist URL

_No response_

### Additional Information


**Issue Summary:**

After upgrading from Electron 27 to Electron 32, the `desktopCapturer.getSources` method behaves inconsistently on macOS versions 12 and later. The issue does not occur on Linux or Windows, and the same code worked correctly on all macOS versions when using Electron 27.

**Problem Details:**
1. **Initial Behavior:**
   - On the first or second call to `desktopCapturer.getSources`, no sources are returned, and the Electron app unexpectedly closes.
   - Adding a log statement before the call results in the logs being printed, but no response is returned, and the app closes.

2. **Subsequent Behavior:**
   - After 2–3 attempts, the method starts working as expected:
     - The sources are printed.
     - The app remains active and does not close.

3. **Observations:**
   - This issue only occurs on macOS versions 12 and above. 
   - On macOS 10.15, Linux, and Windows, the code works without any issues.
   - The issue is specific to the `desktopCapturer.getSources` method when upgraded to Electron 32.

**Code Example:**
```javascript
(async () => {
  try {
    const sources = await desktopCapturer.getSources({ types: ["screen"] });
    logger.info("Printing sources");
    logger.info(sources);
    if (sources.length > 0) {
      event.sender.send(
        "desktopSourceId",
        sources,
        JSON.stringify(screenSettings)
      );
    }
  } catch (error) {
    logger.info("Error fetching desktop sources:", error);
  }
})();
```

**Expected Behavior:**
- The `desktopCapturer.getSources` method should consistently fetch sources and return them on the first call.
- The Electron app should not close unexpectedly during this process.

**Actual Behavior:**
- On macOS 12 and later, the app closes unexpectedly on the first or second call, and no sources are returned.
- After 2–3 attempts, the method works correctly, and the app does not close.

---