Repository: electron/electron (language: cpp)

Issue:
desktopCapturer crashing on linux w/XDP when the capture portal is dismissed/closed
### 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.0.0, 33.0.0, 34.0.0

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

Other Linux

### Operating System Version

Linux bakery 6.12.9-zen1-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Fri, 10 Jan 2025 00:39:35 +0000 x86_64 GNU/Linux

### What arch are you using?

x64

### Last Known Working Electron version

31.3.1 - No crashing but cannot open another portal request. 31.4.0 & later crashes

### Expected Behavior

Doesn't crash & allows following requests

### Actual Behavior

When the user closes the portal picker electron will crash.

### Testcase Gist URL

https://www.electronjs.org/docs/latest/api/desktop-capturer the example provided is a simple repro

### Additional Information

When attempting this from chromium the same errors will be spit out but there is no crash and can make following pick attempts so I believe theyre unrelated. Other than that theres no logs.
```
[10356:10356:0114/163419.616405:ERROR:screencast_portal.cc(367)] Failed to start the screen cast session.
[10356:10356:0114/163419.616432:ERROR:base_capturer_pipewire.cc(81)] ScreenCastPortal failed: 2
```


Discussion / hints:
<!-- blocked/need-repro -->

Hello @Covkie. Thanks for reporting this and helping to make Electron better!

Would it be possible for you to make a standalone testcase with only the code necessary to reproduce the issue? For example, [Electron Fiddle](https://www.electronjs.org/fiddle) is a great tool for making small test cases and makes it easy to publish your test case to a [gist](https://gist.github.com) that Electron maintainers can use.

Stand-alone test cases make fixing issues go more smoothly: it ensure everyone's looking at the same issue, it removes all unnecessary variables from the equation, and it can also provide the basis for automated regression tests.

Now adding the https://github.com/electron/electron/labels/blocked%2Fneed-repro label for this reason. After you make a test case, please link to it in a followup comment. This issue will be closed in 10 days if the above is not addressed.
https://gist.github.com/7335c2f02a56b67e19c0941491e5e6a3

Here's the example on the docs in gist form
Commenting to confirm that this happens on Flatpak apps using Electron as well.
Could you please attach a crash dump to help us get more information? You can collect them by adding the following snippet to your main process code, before `app.whenReady`:

```js
const { app, crashReporter } = require('electron')
console.log(app.getPath('crashDumps'))
crashReporter.start({ submitURL: '', uploadToServer: false })
```

Then reproduce the crash, zip up the crash dumps directory and attach it here. Thanks! 
@codebytere 

[Crashpad.tar.gz](https://github.com/user-attachments/files/18514931/Crashpad.tar.gz)

Fix patch:
diff --git a/shell/browser/api/electron_api_desktop_capturer.cc b/shell/browser/api/electron_api_desktop_capturer.cc
index b7d7177bf47a9..1fcb7fbaa54c7 100644
--- a/shell/browser/api/electron_api_desktop_capturer.cc
+++ b/shell/browser/api/electron_api_desktop_capturer.cc
@@ -15,6 +15,7 @@
 #include "chrome/browser/media/webrtc/desktop_media_list.h"
 #include "chrome/browser/media/webrtc/thumbnail_capturer_mac.h"
 #include "chrome/browser/media/webrtc/window_icon_util.h"
+#include "content/public/browser/browser_thread.h"
 #include "content/public/browser/desktop_capture.h"
 #include "gin/handle.h"
 #include "gin/object_template_builder.h"
@@ -252,7 +253,8 @@ void DesktopCapturer::DesktopListListener::OnSourceThumbnailChanged(int index) {
 }
 
 void DesktopCapturer::DesktopListListener::OnDelegatedSourceListDismissed() {
-  std::move(failure_callback_).Run();
+  content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE,
+                                               std::move(failure_callback_));
 }
 
 void DesktopCapturer::StartHandling(bool capture_window,


Test patch: