rust-windowing / rust-windowing/winit

`MonitorHandle` and `VideoMode` can become invalid leading to panics

Open
#3,258 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

S - api
Dominant language
Rust
Stars
6.2k
Forks
1.3k
Avg merge
2d 19h
Merged PRs (30d)
9

Description

This is an issue in MonitorHandle and VideoMode. In https://github.com/rust-windowing/winit/pull/3303#discussion_r1436573528 @madsmtm has pointed out that on MacOS these handles are not connected to EventLoop in any way, so they should be fine. We should try and cover all backends and make sure handle method can't and can live after the event loop is exited or otherwise come up with an alternative, e.g. adding a lifetime.

  • Rename VideoMode to VideoModeHandle, similar to other types, to represent that they don't just hold static data: #3328.
  • Rename it back into VideoMode, but make it hold only data: https://github.com/rust-windowing/winit/pull/4060.
  • Both MonitorHandle and VideoMode can become invalid, e.g. a monitor gets disconnected. So all methods should return an error to handle that.
  • AFAIK on Linux these types are connected to the EventLoop so we have to come up with a good solution. This is a similar problem to #3317.

Cc #971, https://github.com/rust-windowing/winit/issues/3102 and #2646.


Original Windows only issue

The MonitorHandle on Windows holds a HMONITOR, which can become invalid if e.g. the monitor is disconnected. Or to be more precise, the Windows documentation recommends to listen to WM_DISPLAYCHANGE, which can potentially invalidate any HMONITOR.

All of the MonitorHandle methods call the GetMonitorInfo function and just unwrap(), which would cause a panic if a HMONITOR has become invalid.

See #3255, which only fixed MonitorHandle::video_modes().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Trace the MonitorHandle and VideoMode implementations across the platform backends, starting with the methods that call GetMonitorInfo and the EventLoop relationships described here. Compare backend behavior when monitors disconnect or the event loop exits, then define coverage that ensures invalid handles are handled without panics and methods report errors consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
desktop-dev, operating-systems
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.