Skip to content

[Repo Assist] fix: prevent IndexOutOfRangeException when a monitor is unplugged #155

Closed as not planned

Description

@github-actions

🤖 This pull request was created by Repo Assist, an automated AI assistant.

Problem

Closes #126

When the user unplugs a monitor while the app is running, Screen.AllScreens is refreshed to a shorter array, but currentMonitorIndex is not reset. On the next frame (or the next Ctrl+Shift+Up press), availableMonitors[currentMonitorIndex] throws an IndexOutOfRangeException because the index no longer fits within the smaller array.

The crash path:

  1. Two monitors → availableMonitors.Length = 2, currentMonitorIndex = 1 (secondary monitor)
  2. Secondary monitor unplugged → UpdateCurrentMonitorIndex runs, refreshes availableMonitors to length 1, but the PointToScreen loop finds no match → currentMonitorIndex stays at 1
  3. Next HandleMouseMove call → availableMonitors[1] → IndexOutOfRangeException (unhandled on the low-level mouse hook callback path)

Root Cause

UpdateCurrentMonitorIndex() refreshes the monitor list but only updates currentMonitorIndex when the window centre is found inside a monitor's bounds. If the window was on the now-removed monitor, the search fails silently and the index remains stale.

Fix

Three minimal, surgical changes to MainWindow.xaml.cs:

Site Change
UpdateCurrentMonitorIndex() After the loop, clamp currentMonitorIndex to [0, availableMonitors.Length-1]
HandleMouseMove() Inline clamp before array access (defence in depth)
SetupWindow() Use Math.Min when selecting the target screen

No other behaviour changes. The window falls back to monitor 0 (the primary monitor after clamping) when the current monitor disappears, which matches what Windows itself does.

Test Status

Build could not be verified in CI (Linux runner; project targets net10.0-windows). The fix is a three-line guard — no new API calls, no logic changes, no new dependencies. Manual verification recommended on a two-monitor setup by unplugging the secondary monitor while the app is running.

Note

🔒 Integrity filter blocked 2 items

The following items were blocked because they don't meet the GitHub integrity level.

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

Generated by Repo Assist · ● 3.2M · ◷

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@cbb46ab386962aa371045839fc9998ee4e97ca64

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch repo-assist/fix-issue-126-monitor-unplug-2026-08-01-097cc81089e776ab.

Click here to create the pull request

To fix the permissions issue, go to Settings → Actions → General and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (70 of 70 lines)
From 71c80b0405bbca0b1f61d94bfe38336cada2be7c Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Sat, 1 Aug 2026 13:17:07 +0000
Subject: [PATCH] fix: prevent IndexOutOfRangeException when monitor is
 unplugged

When a monitor is unplugged, Screen.AllScreens shrinks but
currentMonitorIndex is not reset, leaving it pointing past the
end of the array. This causes an IndexOutOfRangeException in
HandleMouseMove (low-level mouse hook on the UI thread, no
catch there) and in SetupWindow.

Changes:
- UpdateCurrentMonitorIndex: clamp currentMonitorIndex to [0,
  availableMonitors.Length-1] after refreshing the list so it
  is always valid even when the window cannot be found on any
  remaining monitor.
- HandleMouseMove: add an inline clamp before the array access
  as a defence-in-depth guard.
- SetupWindow: use Math.Min to clamp the index when picking the
  target screen, so an initialisation race cannot throw.

No other behaviour is changed; the fix is intentionally minimal.

Closes #126

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 WindowsEdgeLight/MainWindow.xaml.cs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/WindowsEdgeLight/MainWindow.xaml.cs b/WindowsEdgeLight/MainWindow.xaml.cs
index 3644301..8346fee 100644
--- a/WindowsEdgeLight/MainWindow.xaml.cs
+++ b/WindowsEdgeLight/MainWindow.xaml.cs
@@ -279,7 +279,7 @@ Version {version}";
             }
         }
 
-        var targetScreen = availableMonitors.Length > 0 ? availableMonitors[currentMonitorIndex] : Screen.PrimaryScreen;
+        var targetScreen = availableMonitors.Length > 0 ? availableMonitors[Math.Min(currentMonitorIndex, availableMonitors.Length - 1)] : Screen.PrimaryScreen;
         if (targetScreen == null) return;
 
         SetupWindowForScreen(targetScreen);
@@ -443,6 +443,8 @@ Version {version}";
         // --- Main Window Logic ---
         if (frameOuterRect != null && frameInnerRe
... (truncated)

Activity

  1. shanselman commented on Aug 8, 2026

    @shanselman
    Owner

    Duplicate of #126; the consolidated maintainer batch covers monitor-unplug index safety.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions