Skip to content

fix(windows): harden revert on inactive stream displays and preserve database-backed redock recovery - #334

Open
pperanich wants to merge 3 commits into
LizardByte:masterfrom
pperanich:fix/windows-revert-and-redock-persistence
Open

pperanich wants to merge 3 commits into
LizardByte:masterfrom
pperanich:fix/windows-revert-and-redock-persistence

Conversation

@pperanich

Copy link
Copy Markdown

Description

This PR hardens Windows SettingsManager revert behavior when a stream-only (e.g., virtual) display is deactivated externally before or during revert, and adds database-backed pending topology recovery so callers can safely save Windows CCD changes to the display database without regressing laptop undock/redock recovery.

Summary of Changes

  1. Empty query guards during revert (revertModifiedHdrStates, revertModifiedDisplayModes):
    • Check for empty results from getCurrentHdrStates() / getCurrentDisplayModes() instead of proceeding into setHdrStates() / setDisplayModes() or arming rollback guards against disappeared paths (which previously triggered Failed to get all duplicated devices! followed by failed to revert HDR states in hdrStateGuardFn!).
  2. Prune inactive stream-only devices during revert:
    • When a stream-only device (absent from m_initial.m_topology) drops out of active Windows paths before or during HDR/mode/primary revert, prune only the inactive stream-only device(s) and disarm stale rollback guards if all modified devices are gone (or continue reverting remaining active initial devices in mixed topologies).
    • If a purely stream-only modified topology fails setTopology() during reactivation even while still enumerated, skip modified-settings cleanup for those stream-only devices so revert can proceed cleanly to the initial topology.
  3. Preserve already-restored initial topology in topology_prep_guard:
    • When revertSettings() enters with current_topology already equivalent to m_initial.m_topology, failure restores current_topology (only if revert switched away) instead of calling createFullExtendedTopology() and force-enabling all available displays into Extended mode.
  4. Database-backed pending topology persistence (savesToDatabase(), m_pending_topology_devices, m_activated_devices):

Screenshot

Issues Fixed or Closed

Roadmap Issues

Testing

Automated Unit Tests

Added regression tests covering every new code path:

  • tests/unit/windows/test_settings_manager_revert.cpp:
    • RevertModifiedSettings_EmptyHdrQueryFailsWithoutCallingSetHdrStates
    • RevertModifiedSettings_EmptyModeQueryFailsWithoutCallingSetDisplayModes
    • RevertModifiedSettings_StreamOnlyDeviceDropsBeforeHdrRevertSkipsModifiedCleanup
    • RevertModifiedSettings_StreamOnlyDeviceDropsBeforeModeRevertRestoresInitialWithoutHdrGuard
    • RevertModifiedSettings_StreamOnlyDeviceDropsBeforePrimaryRevertClearsPrimaryFields
    • RevertModifiedSettings_StreamOnlyTopologyReactivationFailureSkipsModifiedCleanup
    • RevertModifiedSettings_MixedTopologyPrunesInactiveStreamOnlyDeviceAndRevertsInitialDevice
    • TopologyGuard_PreservesInitialTopologyOnFailureInsteadOfEnablingAllDisplays
    • TopologyGuard_PreservesInitialTopologyLogsFailureWhenRestoreFails
  • tests/unit/windows/test_settings_manager_undock.cpp:
    • DatabaseBackedTopologyStaysPendingUntilClosedLidRedock
    • DatabaseBackedPendingTopologySurvivesUndockedStreamAndClearsOnOpenLidRedock
    • DatabaseBackedPartialRedockKeepsOnlyStillMissingDisplaysPending
  • tests/unit/windows/test_win_display_device_general.cpp: SavesToDatabase_ReflectsConstructorFlag
  • tests/unit/windows/test_json_converter.cpp: round-trip serialization of m_pending_topology_devices and m_activated_devices

Verification runs:

  • Cross-compiled Windows x64 test_libdisplaydevice.exe (MinGW GCC 16.2.0) and ran all non-hardware/mocked suites natively on Windows 11: 426/426 passed.
  • gcov line coverage on modified files: json_serializer.cpp 100%, types.h 100%, settings_manager_apply.cpp 100% of changed lines (98.27% overall), win_display_device_general.cpp 100% of changed lines (98.36% overall), settings_manager_revert.cpp 98.22% overall.
  • Strict Doxygen (WARN_AS_ERROR = YES via Dockle config): 0 warnings.
  • clang-format --style=file --dry-run --Werror on all modified files: 0 violations.

Live Windows Hardware Validation

Linked into a local MSYS2 UCRT64 Sunshine build with save_to_database = true on Windows 11 Pro (RX 6800 + DisplayPort 4K@144Hz HDR monitor + virtual display):

  • Mid-stream external deactivation (virtual display deactivated back to physical monitor while still enumerated): disconnect cleanly kept the physical-only topology and deleted display_device.state with zero Failed to get all duplicated devices!, hdrStateGuardFn, or Enabling all of the available devices errors.
  • Mid-stream hot-unplug (virtual display removed during stream): disconnect logged Some modified devices are unavailable, reverting settings for the remaining topology:, kept the physical-only topology, and deleted display_device.state.
  • Deep-sleep / pending redock recovery: when the physical DP monitor entered deep sleep (avail=0) during a 2+ minute stream, display_device.state persisted across disconnect and a Sunshine service restart, then automatically reverted to physical-only and cleared display_device.state as soon as the monitor woke. (Note: reconnecting a new stream while all initial devices are asleep still hits the separate stripInitialState() path addressed by open PR fix(windows): apply settings while all initial devices are unavailable #330.)

Live display-mutating unit tests (WinDisplayDeviceTopology.*, WinDisplayDeviceModes.*, WinDisplayDevicePrimary.*, WinDisplayDeviceHdr.*) were not run locally to avoid mutating active host monitors and are left to CI.

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

This pull request was generated by Antigravity (Gemini, high thinking level)

…database-backed redock recovery

Guard empty HDR and display mode queries during revert, prune stream-only devices that become inactive externally before or during revert, and preserve an already-restored initial topology in topology_prep_guard instead of force-enabling all displays into Extended mode.

Also expose WinDisplayDeviceInterface::savesToDatabase() and persist optional pending topology and activated stream-only device metadata so database-backed CCD writes keep missing original displays pending until redock.
@pperanich

Copy link
Copy Markdown
Author

This pull request was generated by Antigravity (Gemini, high thinking level)

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Extract topology group filtering, pending-device collection, revert topology guard fallback, modified-topology switch recovery, and unified stream-only pruning/retry helpers in settings_manager_revert.cpp to reduce cognitive complexity and resolve SonarCloud findings, and share mock setup helpers in test_settings_manager_revert.cpp.
@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.

2 participants