@@ -141,10 +141,18 @@ impl MonitorHandle {
141141 let refresh_rate_millihertz = self . refresh_rate_millihertz ( ) ;
142142 let monitor = self . clone ( ) ;
143143
144- let array = unsafe { CGDisplayCopyAllDisplayModes ( self . display_id ( ) , None ) }
145- . expect ( "failed to get list of display modes" ) ;
146- // SAFETY: `CGDisplayCopyAllDisplayModes` is documented to return an array of display modes.
147- let modes = unsafe { CFRetained :: cast_unchecked :: < CFArray < CGDisplayMode > > ( array) } ;
144+ let array = unsafe { CGDisplayCopyAllDisplayModes ( self . display_id ( ) , None ) } ;
145+ let modes = if let Some ( array) = array {
146+ // SAFETY: `CGDisplayCopyAllDisplayModes` is documented to return an array of
147+ // display modes.
148+ unsafe { CFRetained :: cast_unchecked :: < CFArray < CGDisplayMode > > ( array) }
149+ } else {
150+ // Occasionally, certain CalDigit Thunderbolt Hubs report a spurious monitor during
151+ // sleep/wake/cycling monitors. It tends to have null or 1 video mode only.
152+ // See <https://github.com/bevyengine/bevy/issues/17827>.
153+ warn ! ( monitor = ?self , "failed to get a list of display modes" ) ;
154+ CFArray :: empty ( )
155+ } ;
148156
149157 modes. into_iter ( ) . map ( move |mode| {
150158 let cg_refresh_rate_hertz = unsafe { CGDisplayMode :: refresh_rate ( Some ( & mode) ) } ;
@@ -165,9 +173,14 @@ impl MonitorHandle {
165173 let uuid = self . uuid ( ) ;
166174 NSScreen :: screens ( mtm) . into_iter ( ) . find ( |screen| {
167175 let other_native_id = get_display_id ( screen) ;
168- // Display ID just fetched from live NSScreen, should be fine to unwrap.
169- let other = MonitorHandle :: new ( other_native_id) . expect ( "invalid display ID" ) ;
170- uuid == other. uuid ( )
176+ if let Some ( other) = MonitorHandle :: new ( other_native_id) {
177+ uuid == other. uuid ( )
178+ } else {
179+ // Display ID was just fetched from live NSScreen, but can still result in `None`
180+ // with certain Thunderbolt docked monitors.
181+ warn ! ( other_native_id, "comparing against screen with invalid display ID" ) ;
182+ false
183+ }
171184 } )
172185 }
173186}
0 commit comments