Skip to content

fix(linux): choose KMS capture later when no display was lit at startup - #6052

Open
miguelangel-nubla wants to merge 3 commits into
SunshineStream:masterfrom
miguelangel-nubla:fix/linux-capture-redetect
Open

miguelangel-nubla wants to merge 3 commits into
SunshineStream:masterfrom
miguelangel-nubla:fix/linux-capture-redetect

Conversation

@miguelangel-nubla

@miguelangel-nubla miguelangel-nubla commented Nov 5, 2026 •

Copy link
Copy Markdown

Description

On Linux the capture method is chosen once, in platf::init(), and KMS only lists displays that are lit. If Sunshine starts while every screen is blanked (for example by a login screen's power saving, as on a GDM greeter that has gone idle), it logs Unable to initialize capture method and keeps no capture method for the life of the process: every client then gets "Failed to initialize video capture/encoding. Is a display connected and turned on?" until Sunshine is restarted with a screen on, even after the screens are back.

With this change, when the configured capture method is KMS or automatic, platf::init() logs a warning instead of failing, and platf::choose_kms_capture_if_none(), run from display_names() and display() while nothing is chosen, chooses KMS once a display is lit. The selection at startup is unchanged.

Only KMS is checked again. The other methods depend on the session's environment (WAYLAND_DISPLAY, DISPLAY) or on a portal permission, which are settled at startup, and verifying the portal again could restart Sunshine (stale token) or ask for permission again. With another capture method configured, platf::init() fails as before. display() no longer drops the DRM worker's privileges while nothing is chosen, since KMS still needs them once a display is lit.

One consequence for the tests, which I'd like your view on: PlatformTestSuite treats a successful platf::init() as "a display can be captured". On Linux that no longer holds without any display (a headless runner), so the encoder fixture now skips with "No display to capture" when platf::display_names() is empty, instead of failing the software encoder. With a display, as in CI's virtual desktop, nothing changes. If you'd rather keep headless runs failing loudly, I can drop that hunk.

Screenshot

Issues Fixed or Closed

Roadmap Issues

Testing

  • New unit tests in tests/unit/platform/linux/test_misc.cpp (CaptureSources.*), using small test hooks (set_kms_verifier_for_testing, reset_capture_sources_for_testing, kms_capture_chosen_for_testing) that stand in for the KMS display check: KMS is chosen once a display is lit after none was; display_names() makes that choice when nothing is chosen; a choice once made is kept without checking again; with another capture method configured (x11), KMS is never chosen later and the check isn't run.
  • Regression check: with the later choice disabled again (as before this change), the first three fail and the fourth passes, as it should; with this change all four pass.
  • Built on master (c8ea783), together with my two other PRs (retrying the encoder probe before a stream, and the Vulkan quality query on a device without video encode), with the Linux dependencies of the nixpkgs Sunshine package, and ran the whole test_sunshine suite in a container without any display. Compared with plain master in the same container: no new failures (the same 10, which need a display, a network or packaging scripts), and the four platform suites that fail at setup on master without a display (AudioTest, EncoderTest, MouseHIDTest, X11GrabTest) now set up, with the encoder tests skipped as "No display to capture". 897 passed, 15 skipped, 10 failed, against 881, 19 and 10 plus those four on master. I could not run CI's workflow with its virtual desktop.
  • End to end, on Sunshine 2026.1018.5342 with this change: a NixOS VM test with GDM's login screen, its screensaver blanking the screen, Sunshine restarted in that state, then a client requesting a stream. Before this change Sunshine logs Unable to initialize capture method at that restart (the state in which every request is refused); with it the launch answers 200. On real hardware too (an Intel Sandy Bridge iGPU host at a GDM login screen, Moonlight as the client): with every screen blanked and Sunshine restarted, it logs the startup failure and keeps running; the next stream request logs A display is lit now: screencasting with KMS and the client connects.
  • clang-format 23.1.0 (the version in uv.lock) reports no changes for the touched files.
  • Rebased onto master (ab6140e). It conflicted with fix(linux): explain a missing graphical session when no capture method starts #6095 in platf::init(): the missing graphical session hint from fix(linux): explain a missing graphical session when no capture method starts #6095 is kept, and logged on both paths (giving up, and checking KMS again later). That path is now a small helper, continue_without_capture_method(), which keeps init() within SonarCloud's cognitive complexity limit. Built with scripts/linux_build.sh (no CUDA) in an Ubuntu 22.04 arm64 container and ran test_sunshine under Xvfb: 974 tests, 968 passed, 6 skipped, 0 failed.

Type of Change

  • feat: New feature (non-breaking change which adds functionality)
  • fix: Bug fix (non-breaking change which fixes an issue)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc.)
  • refactor: Code change that neither fixes a bug nor adds a feature
  • perf: Code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit
  • BREAKING CHANGE: Introduces a breaking change (can be combined with any type above)

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas
  • Code docstring/documentation-blocks for new or existing methods/components have been added or updated
  • Tests have been added or updated for new or modified functionality, including regression tests for bug fixes

AI Usage

See our AI usage policy.

  • AI tools were used while preparing this PR

@miguelangel-nubla
miguelangel-nubla marked this pull request as ready for review November 6, 2026 19:29
@sonarqubecloud

sonarqubecloud Bot commented Nov 6, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 New issue
1 New Code Smells (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

matteo-pacini added a commit to matteo-pacini/nixos-configs that referenced this pull request Nov 14, 2026
… limit (#478)

* chore(brightfalls): drop dead GDM monitors.xml rule

GDM greeter users now have their home under /run/gdm/home, so nothing
reads /run/gdm/.config/monitors.xml.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(brightfalls): wait for the LUKS passphrase without a time limit

The root volume only appears after the passphrase is typed. After the
default 90 s device timeout stage 1 dropped to emergency mode and killed
the Plymouth prompt. Wait for the device indefinitely, and allow
unlimited passphrase attempts so three wrong ones cannot leave that wait
with no prompt.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* feat(brightfalls): restore Sunshine with KMS-only capture

Sunshine 2026.914 probes the XDG portal at every start when no capture
method is set, which raised GNOME's remote-control dialog at each login.
capture = kms skips that probe. The Plymouth wait comes back because a
KMS-only start with no lit output never recovers; it waits on
`SunshineStream/Sunshine#6052`.

Force the monitors.xml link: GNOME Settings replaces it with a regular
file, and Home Manager refuses to activate once a backup already exists.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
KMS lists only lit displays, and the capture method was chosen once, in
platf::init(). Started while every screen was blanked (a login screen's
power saving), Sunshine logged "Unable to initialize capture method" and
kept no capture method for the life of the process: every client got
"Failed to initialize video capture/encoding" until it was restarted with
a screen on.

When the configured capture method is KMS or automatic, platf::init() now
logs a warning instead of failing, and choose_kms_capture_if_none(), run
from display_names() and display() while nothing is chosen, chooses KMS
once a display is lit. Only KMS is checked again: the other methods depend
on the session's environment or a portal permission, settled at startup,
and verifying the portal can restart Sunshine. With another capture method
configured, init fails as before. display() no longer drops the DRM
worker's privileges while nothing is chosen, as KMS still needs them.

Since init no longer implies a display on Linux, the encoder fixture skips
when no display is listed instead of failing the software encoder.

Tests replace the KMS display check (set_kms_verifier_for_testing): KMS is
chosen once a display is lit, display_names() makes the choice, a choice is
kept, and nothing is checked again with another capture method configured.
…er maybe unused

SonarCloud's findings on this PR.
Keeps init()'s cognitive complexity within SonarCloud's limit, and logs the missing graphical
session hint from SunshineStream#6095 on both paths.
@miguelangel-nubla
miguelangel-nubla force-pushed the fix/linux-capture-redetect branch from 121d867 to 5678869 Compare November 22, 2026 17:14
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant