electron: [Bug]: Desktop capturer segmentation Fault

Preflight Checklist

Electron Version

19.0.17

What operating system are you using?

Ubuntu

Operating System Version

Weston wayland

What arch are you using?

arm64 (including Apple Silicon)

Last Known Working Electron version

No response

Expected Behavior

In the file background.ts (main process) I am calling desktopCapturer to retrieve the screen id:

const a = await desktopCapturer.getSources({ types: ['screen'] });
console.dir(a);

Actual Behavior

However when this line is invoked it prints Segmentation fault and the program crashes. I believe the problem may be related to the types array because when I set it empty it works without problems.

Testcase Gist URL

No response

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 26 (2 by maintainers)

Most upvoted comments

What about the patch in https://github.com/electron/electron/issues/36660#issuecomment-1447436256 ? Is it a realistic fix or a workaround ?

@gza As of workaround that doesn’t require patching Electron, this is what I’ve been doing myself for quite a long time when my app detects Wayland environment and PipeWire capturer flag used:

https://github.com/SpacingBat3/WebCord/blob/f26e95bf17281d82a25bee82a4805136bd3f042b/sources/code/main/windows/main.ts#L503-L510

          // Workaround #328: Segfault on `desktopCapturer.getSources()` since Electron 22
          }) : Promise.resolve([{
            id: "screen:1:0",
            appIcon: nativeImage.createEmpty(),
            display_id: "",
            name: "Entire Screen",
            thumbnail: nativeImage.createEmpty()
          } satisfies Electron.DesktopCapturerSource]);

Somehow, hard-coding a source this way to avoid using desktopCapturer.getSources() looks like as a viable workaround – from my tests PipeWire capturer is triggered correctly when using this source.

I’ve also didn’t test if desktopCapturer.getSources() still breaks on newer versions, so I don’t confirm this issue is still relevant.

As @jrose-signal said, you can only share x11 application with this so-called “fix”

@mgonzalezg9 As the use case is clearly the screen sharing of a wayland environment, I’m not sure one can consider the problem as fixed because one can share the few old apps not supporting wayland (purpose of xwayland).

What about the patch in https://github.com/electron/electron/issues/36660#issuecomment-1447436256 ? Is it a realistic fix or a workaround ?

I dug into this a bit on Electron main and got past the crash with this change to WebRTC’s desktop_media_list_base.cc:

diff --git a/chrome/browser/media/webrtc/desktop_media_list_base.cc b/chrome/browser/media/webrtc/desktop_media_list_base.cc
index 7809273017..a956f3c2dc 100644
--- a/chrome/browser/media/webrtc/desktop_media_list_base.cc
+++ b/chrome/browser/media/webrtc/desktop_media_list_base.cc
@@ -72,6 +72,11 @@ void DesktopMediaListBase::StartUpdating(DesktopMediaListObserver* observer) {
 void DesktopMediaListBase::Update(UpdateCallback callback, bool refresh_thumbnails) {
   DCHECK_CURRENTLY_ON(BrowserThread::UI);
   DCHECK(sources_.empty());
+
+  // If there is a delegated source list, it may not have been started yet.
+  if (IsSourceListDelegated())
+    StartDelegatedCapturer();
+
   DCHECK(!refresh_callback_);
   refresh_callback_ = std::move(callback);
   Refresh(refresh_thumbnails);

The general problem is that Electron’s DesktopCapturer does not seem to “start” the “delegated capturer” (Wayland’s native capture window) before calling this Update method. “Delegated” capturers that show their own UI follow slightly different code paths than the old style of capturers, and of course Chromium does something different from DesktopCapturer.

With this change I can get a toy example to show the Wayland picker and let me share the screen on Ubuntu 22.04.2 (though the picker comes up twice, possibly once each for the main and renderer processes, I didn’t check). I don’t think it’s quite as simple as that, because sometimes when I quit I see the system still showing the “you are sharing your screen” status icon in the top right. I’m not a strong enough Pipewire, WebRTC, or Electron engineer to say whether this is the correct fix, but maybe an Electron person can now make progress here.

Fixed the issue using the app with xwayland, desktopCapturer.getSources is not crashing this way. This quick demo works. Closing 😃

That might fix the crash (unsure), but we still eventually do need the Wayland capturer, or we’ll only be able to capture a subset of the local windows.