Skip to content

fix(windows): apply settings while all initial devices are unavailable - #330

Open
MrRussLuther wants to merge 3 commits into
LizardByte:masterfrom
MrRussLuther:fix/apply-without-initial-devices
Open

MrRussLuther wants to merge 3 commits into
LizardByte:masterfrom
MrRussLuther:fix/apply-without-initial-devices

Conversation

@MrRussLuther

@MrRussLuther MrRussLuther commented Nov 21, 2026 •

Copy link
Copy Markdown

Description

I hit this on a desktop whose monitors sit behind a KVM switch, streaming to a virtual display with ensure_only_display. The PC was shut down while a stream was still up, so the revert never ran and the persisted state kept the two monitors as the initial topology. On the next boot the KVM was on another machine and only the virtual display was enumerated. The revert failed, which is expected, but every later applySettings() also failed with "Enumerated device list does not contain ANY of the devices from the initial state!" until I reset the persistence by hand. Streams still started, just at whatever mode the virtual display was left in.

#298 keeps recovery pending when there's no original display or panel to fall back to. This doesn't change that. It only changes apply: when none of the cached initial devices are available, the current topology is used as the base for the new one. A request that would need a topology change is still refused in that state, because the interim layout couldn't be restored afterwards. The cached initial state is still what gets persisted, so the original layout comes back once those displays return.

This is separate from SunshineStream/Sunshine#5942. That one is about the session layout not sticking when changes aren't saved to the Windows database. This one also happens on v2026.914.233613, which is where I hit it, and a pending revert shouldn't block a new session whichever way #5942 is resolved.

The two do share a cause: the persisted record and Windows' saved layout can disagree once the set of connected displays changes. If saving to the database comes back, I'd like to follow up with a PR that keeps a topology-only record pending until the original displays return (keepUnavailableSettings() drops it today). That should cover the redock case SunshineStream/Sunshine#5625 was after without save_to_database = false. I'm happy to wait for a direction on #5942 before starting on it.

Screenshot

Issues Fixed or Closed

Roadmap Issues

Testing

  • Added PrepareTopology, InitialDevicesUnavailable (plus its failure branch) and UndockRecovery.StreamCanBeConfiguredWhileRecoveryIsPending, which runs apply, a revert with the display still away, then a revert after it returns.
  • Both scenario tests fail on master and pass with the change.
  • Added UndockRecovery.TopologyIsNotSwitchedWhileRecoveryIsPending after review: EnsureOnlyDisplay on one member of an active clone group is refused while the original displays are away.
  • Ran test_libdisplaydevice.exe --gtest_filter='*Mocked*:UndockRecovery.*' on a Windows x86_64 build cross-compiled from Linux (clang, not UCRT64): 287 tests pass.
  • Ran both builds of the library against the real leftover state on the affected machine, with the monitors still behind the KVM and only the virtual display enumerated, using a small harness around SettingsManager. Master returns DevicePrepFailed. This branch returns Ok and switches the virtual display from 3180x2384 to the requested 1920x1080. A revert with the monitors still away puts 3180x2384 back and stays pending, with the original initial topology left in the state file. After switching the KVM back, the next revert returns Ok: both monitors are active again, the virtual display is off and the state file is cleared.
  • I didn't run the hardware suites, and I haven't run a patched Sunshine build.
Harness output (trimmed, mode JSON shortened to one line)

master (b40ce11):

INFO:    Trying to apply display device settings. API is available: true
ERROR:   Enumerated device list does not contain ANY of the devices from the initial state!
applySettings result: DevicePrepFailed

this branch:

INFO:    Trying to apply display device settings. API is available: true
WARNING: None of the devices from the initial topology are available. Will use the current topology as a base until they return:
  - initial topology: [["{ab021fd8-048a-5734-b411-a1b2a544b87d}"],["{ee1b2c2f-6bbf-54c0-9901-71de8ce4f08a}"]]
  - current topology: [["{9acddf6d-43cc-576e-9aff-0c5fc80b4cc8}"]]
INFO:    Newly computed display device topology data:
  - topology: [["{9acddf6d-43cc-576e-9aff-0c5fc80b4cc8}"]]
  - change is needed: false
INFO:    Changing display modes to:
  "{9acddf6d-43cc-576e-9aff-0c5fc80b4cc8}": 1920x1080 @ 120/1
applySettings result: Ok

INFO:    Trying to revert applied display device settings. API is available: true
INFO:    Trying to change back the display modes to:
  "{9acddf6d-43cc-576e-9aff-0c5fc80b4cc8}": 3180x2384 @ 120/1
ERROR:   Device {ab021fd8-048a-5734-b411-a1b2a544b87d} does not exist in the available path source data!
revertSettings result: SwitchingTopologyFailed

this branch, after the monitors returned:

INFO:    Trying to revert applied display device settings. API is available: true
revertSettings result: Ok
topology: [["{ab021fd8-048a-5734-b411-a1b2a544b87d}"],["{ee1b2c2f-6bbf-54c0-9901-71de8ce4f08a}"]]

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

AI assistance was used to trace the failure through the logs and code, and to draft the patch and tests.

@CLAassistant

CLAassistant commented Nov 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ReenigneArcher ReenigneArcher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single-display KVM scenario now passes in the Windows UCRT64 mocks. Please limit the fallback to settings changes within the active topology while recovery is pending. The guard below passed all 287 mocked and undock tests, including the new clone-group regression.

Review completed by Codex | Model: gpt-6.2-sol | Thinking: xhigh

// and hope that we are still "safe" to make further changes (to be determined by computeNewTopologyAndMetadata call below).
const auto stripped_initial_state {win_utils::stripInitialState(new_state.m_initial, devices)};
// The new topology is based on the initial state, which is the current topology if the cached one is of no use.
const auto stripped_initial_state {computeBaseInitialState(new_state.m_initial, cached_state.has_value(), topology_before_changes, devices)};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve the interim display layout while recovery is pending. With every original device absent and no built-in panel, EnsureOnlyDisplay can now split a temporary clone group. The saved m_initial still describes the absent layout, so the failed revert falls back to extending every enumerated display. I reproduced this with [["stream","clone"]] and one inactive output. Apply succeeded, then disconnect left [["stream"],["clone"],["inactive"]] with the original recovery still pending.

Add this at the start of if (change_is_needed) in prepareTopology(), before reverting previous settings or switching topology.

      if (cached_state && isTopologyUnavailable(new_state.m_initial.m_topology, devices)) {
        DD_LOG(error) << "Cannot switch display topology while all original devices are unavailable!";
        return std::nullopt;
      }

In tests/unit/windows/test_settings_manager_undock.cpp, use the layout above to check that applying EnsureOnlyDisplay to stream leaves the clone group intact and recovery pending. The existing single-display regression verifies that the same request still applies the requested mode when the active topology already matches.

Review completed by Codex | Model: gpt-6.2-sol | Thinking: xhigh

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. The interim layout isn't recorded anywhere, so there's nothing to put it back from. Done in 8351756: a topology change is refused while all the original devices are away, and mode/HDR changes within the active topology still go through.

I put the check in a small helper (isTopologyChangeBlocked) and call it just before if (change_is_needed) instead of inside it. Inline, it adds 3 to prepareTopology() (a nested if plus the &&), which takes it from 24 to 27, over Sonar's cognitive complexity limit of 25. As a top-level call it adds 1.

Added UndockRecovery.TopologyIsNotSwitchedWhileRecoveryIsPending with your layout. It fails on the previous commit (apply returns Ok and leaves [["stream"]]) and passes now. 287 mocked and undock tests pass here.

One thing I noticed while testing: with the guard the apply is refused and the clone group survives, but a revertSettings() afterwards still ends at [["stream"],["clone"],["inactive"]]. The topology guard in revertSettings() extends every enumerated display whenever the revert fails, and Sunshine (2026.914 at least) calls revert on disconnect even after a failed apply. This PR doesn't touch that path, so I left it alone, but I can look at it in the follow-up if you want.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Russ. I checked the rebased head (013e7f0): the apply guard and clone-group regression address the apply-side finding, and mode/HDR changes can still proceed when the topology already matches.

Yes, please take the disconnect behavior in a separate follow-up. revertSettings() can still use a fully extended topology when recovery fails, even if apply left the layout alone. A regression test could start with the original monitors absent, an active [["stream", "clone"]] group, and a third inactive output. After a refused apply and a call to revertSettings(), assert that the clone group stays intact, the third output stays inactive, and the saved recovery stays pending. Then reconnect the originals and check that the original layout is restored. Please compare the clone groups explicitly, not just the set of active IDs.

I checked the source, not a live Windows setup. This follow-up doesn’t require changing whether Sunshine saves temporary layouts in #5942.

Review completed by Codex | Model: unavailable | Thinking: unavailable

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking the rebased head. I'll leave the revertSettings() disconnect behavior out of this PR. It comes from the same cause as #5942, a saved record that disagrees with the displays that are connected, so I'd rather fix it there than patch the revert path on its own. I'll use your scenario as the regression test: the originals absent, an active [["stream", "clone"]] group with a third output inactive, a refused apply, then revertSettings(). It will compare the clone groups explicitly and check that the originals' layout comes back when they return.

A persisted state whose initial devices are all gone (e.g. displays
behind a KVM switch after an unclean shutdown) made every applySettings()
call fail until the persistence was reset by hand.

Use the current topology as the base in that case. The cached initial
state is still persisted, so the original layout is restored once the
devices return.
Keeps prepareTopology under the cognitive complexity limit (S3776).
The cached initial state still describes the absent layout, so the
active topology could not be restored after changing it. Mode and HDR
changes within the active topology are still applied.
@MrRussLuther
MrRussLuther force-pushed the fix/apply-without-initial-devices branch from 8351756 to 013e7f0 Compare November 22, 2026 04:35
@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.

3 participants