Skip to content

winit panicks failing to find x11 monitor #507

Closed

Description

@spearman

copied from rust-windowing/glutin#1018:

When I iterate over available monitors with events loop, nothing is returned, and attempting to call get_primary_monitor() will crash:

extern crate glium;
use glium::glutin;

fn main () {
  {
    let events_loop = glutin::EventsLoop::new();
    let mut available_monitors_count = 0; 
    for i in events_loop.get_available_monitors() {
      available_monitors_count += 1;
    }
    println!("avilable monitors count: {}", available_monitors_count);  // prints '0'
    let primary_monitor = events_loop.get_primary_monitor(); // crash 1
  }
  let events_loop = glutin::EventsLoop::new();  // crash 2
}

crash 1 with:

thread 'main' panicked at '[winit] Failed to find any x11 monitor', libcore/option.rs:916:5

If crash 1 is commented out, then code will reach crash 2 with:

thread 'main' panicked at 'Failed to call XInternAtom: XError { description: "BadRequest (invalid request code or no such operation)", error_code: 1, request_code: 140, minor_code: 42 }', libcore/result.rs:945:5

Previously the iter loop would show some available monitors, and the primary monitor would be returned, but I'm not sure exactly what has changed since the last time it worked. This is on end of life LTS Ubuntu (14.04) so it may be something out of date.

Activity

  1. francesca64 commented on May 9, 2018

    @francesca64
    Member

    Can you tell me the output of xrandr --version?

  2. added
    B - bug
    DS - x11
    C - needs investigation
    on May 9, 2018
  3. spearman commented on May 9, 2018

    @spearman
    ContributorAuthor

    $ xrandr --version
    xrandr program version 1.4.1
    Server reports RandR version 1.4

  4. francesca64 commented on May 9, 2018

    @francesca64
    Member

    Versions of XRandR earlier than 1.5 use a different code path, but should still work. It's just, as you might guess, it tends not to get tested.

    ...wait, actually, what version of winit are you running? I just noticed the "BadRequest (invalid request code or no such operation)" error, which would seem to suggest a protocol issue, and distinction between XRandR 1.5 and earlier versions wasn't implemented until relatively recently.

  5. spearman commented on May 10, 2018

    @spearman
    ContributorAuthor

    Ah, I was getting the dependency from glium which has a lagging dependency on glutin which in turn has a lagging dependncy on winit 12.0.

    Updating both to use winit 14.0 does fix the 2nd panic (creating another event loop after the first has been dropped), but the 1st panic remains.

  6. b-r-u commented on May 10, 2018

    @b-r-u
    Contributor

    Just another data point:

    $ xrandr --version
    xrandr program version       1.4.2
    Server reports RandR version 1.4

    With the glium code, the following winit code and with one or two connected monitors I could not observe any crashes.

    extern crate winit;
    
    fn main () {
      {
        let events_loop = winit::EventsLoop::new();
        let mut available_monitors_count = 0;
        for i in events_loop.get_available_monitors() {
          available_monitors_count += 1;
        }
        println!("avilable monitors count: {}", available_monitors_count);  // prints '0'
        let primary_monitor = events_loop.get_primary_monitor(); // crash 1
      }
      let events_loop = winit::EventsLoop::new();  // crash 2
    }
  7. francesca64 commented on May 10, 2018

    @francesca64
    Member

    xrandr --listmonitors --verbose might contain some interesting information.

  8. spearman commented on May 10, 2018

    @spearman
    ContributorAuthor
    $ xrandr --listmonitors --verbose
    xrandr: unrecognized option '--listmonitors'
    Try 'xrandr --help' for more information.
    

    this seems to show some kind of display information, not sure how it's different from 'monitors':

    $ xrandr -q
    Screen 0: minimum 8 x 8, current 1600 x 900, maximum 16384 x 16384
    VGA-0 disconnected primary (normal left inverted right x axis y axis)
    LVDS-0 connected 1600x900+0+0 (normal left inverted right x axis y axis) 309mm x 174mm
       1600x900       60.0*+   40.0  
    DP-0 disconnected (normal left inverted right x axis y axis)
    DP-1 disconnected (normal left inverted right x axis y axis)
    DP-2 disconnected (normal left inverted right x axis y axis)
    DP-3 disconnected (normal left inverted right x axis y axis)
    DP-4 disconnected (normal left inverted right x axis y axis)
    DP-5 disconnected (normal left inverted right x axis y axis)
    
  9. b-r-u commented on May 10, 2018

    @b-r-u
    Contributor
    $ xrandr -q
    Screen 0: minimum 8 x 8, current 3286 x 1080, maximum 32767 x 32767
    LVDS1 connected primary 1366x768+0+0 (normal left inverted right x axis y axis) 260mm x 140mm
       1366x768      59.97*+
       1360x768      59.96  
       1280x720      60.00  
       1024x768      60.00  
       1024x576      60.00  
       960x540       60.00  
       800x600       60.32    56.25  
       864x486       60.00  
       640x480       59.94  
       720x405       60.00  
       680x384       60.00  
       640x360       60.00  
    DP1 disconnected (normal left inverted right x axis y axis)
    HDMI1 connected 1920x1080+1366+0 (normal left inverted right x axis y axis) 530mm x 300mm
       1920x1080     60.00*+
       1680x1050     59.88  
       1600x900      59.98  
       1280x1024     75.02    60.02  
       1280x800      59.91  
       1152x864      75.00  
       1280x720      59.97  
       1024x768      75.08    60.00  
       832x624       74.55  
       800x600       75.00    60.32  
       640x480       75.00    60.00  
       720x400       70.08  
    VGA1 disconnected (normal left inverted right x axis y axis)
    VIRTUAL1 disconnected (normal left inverted right x axis y axis)
  10. francesca64 commented on May 10, 2018

    @francesca64
    Member

    Ah, sorry, I guess --listmonitors must be a 1.5 thing.

    @b-r-u unless you have a burning desire to trawl through the RandR spec, I'll take a crack at fixing this.

  11. b-r-u commented on May 10, 2018

    @b-r-u
    Contributor

    @francesca64 Please do, I have very little experience with the RandR protocol!

  12. francesca64 commented on May 10, 2018

    @francesca64
    Member

    @spearman this branch adds some debug messages: https://github.com/francesca64/winit/tree/x11-randr

    @b-r-u XRRGetScreenResources is apparently really slow, so I'm going to look into caching the monitor list and using XRRSelectInput to receive events when the state changes. That's totally unrelated to this problem; I just enjoy caching things.

  13. spearman commented on May 11, 2018

    @spearman
    ContributorAuthor

    @francesca64 Compiling against the x11-randr branch actually detects 1 monitor and doesn't crash at all:

         Running `target/debug/examples/crash`
    PRIMARY 636
    CRTC_COUNT 2
    > CRTC #0
    ·> ID 634
    ·> WIDTH 1600
    ·> HEIGHT 900
    ·> OUTPUT_COUNT 1
    ·> IS_VALID true
    > CRTC #1
    ·> ID 635
    ·> WIDTH 0
    ·> HEIGHT 0
    ·> OUTPUT_COUNT 0
    ·> IS_VALID false
    avilable monitors count: 1
    PRIMARY 636
    CRTC_COUNT 2
    > CRTC #0
    ·> ID 634
    ·> WIDTH 1600
    ·> HEIGHT 900
    ·> OUTPUT_COUNT 1
    ·> IS_VALID true
    > CRTC #1
    ·> ID 635
    ·> WIDTH 0
    ·> HEIGHT 0
    ·> OUTPUT_COUNT 0
    ·> IS_VALID false
    PRIMARY 636
    CRTC_COUNT 2
    > CRTC #0
    ·> ID 634
    ·> WIDTH 1600
    ·> HEIGHT 900
    ·> OUTPUT_COUNT 1
    ·> IS_VALID true
    > CRTC #1
    ·> ID 635
    ·> WIDTH 0
    ·> HEIGHT 0
    ·> OUTPUT_COUNT 0
    ·> IS_VALID false
    
    
  14. francesca64 commented on May 11, 2018

    @francesca64
    Member

    Wait, really? All I did was refactor the monitor module. Well, I updated the branch to be ready for merging (besides the fact that I still need to impl Debug for MonitorId on all the other platforms) so if things still work after you pull that, I guess this is fixed.

    I plan on putting out a 0.14.1 release soon, which will include this.

    (Since this was so quick, I'll save the caching for future work.)

  15. spearman commented on May 12, 2018

    @spearman
    ContributorAuthor

    Hmm, yeah now it finds 0 monitors again and crashes on the call to get_primary_monitor

  16. francesca64 commented on May 12, 2018

    @francesca64
    Member

    The only way it could've changed is if it's somehow a timing issue that the println! calls accidentally fixed, and since all the XRandR calls we make are synchronous, that shouldn't be possible.

    It seems like it only worked once as a fluke. If you want to check, you can step your way through the commits on that branch using an interactive rebase.

  17. francesca64 commented on May 12, 2018

    @francesca64
    Member

    ...though, I guess that's specifically what git bisect is for, but I've never used it.

  18. francesca64 commented on May 12, 2018

    @francesca64
    Member

    I've pushed another commit which will print any X11 errors that occur, so if it's a protocol issue we'll find out quite easily.

  19. spearman commented on May 12, 2018

    @spearman
    ContributorAuthor

    Well I don't see any X11 errors printing out, but this time it detects 1 monitor, but it's not the primary monitor, and crashes when trying to get primary (I added a debug print of the 1 available monitor):

         Running `target/debug/examples/crash`
    available monitor: MonitorId { inner: X(MonitorId { id: 634, name: "LVDS-0", dimensions: (1600, 900), position: (0, 0), primary: false, hidpi_factor: 1.3333334 }) }
    avilable monitors count: 1
    thread 'main' panicked at '[winit] Failed to find any x11 monitor', libcore/option.rs:914:5
    
    
  20. francesca64 commented on May 12, 2018

    @francesca64
    Member

    Ha, there's definitely something funky happening here. The thing is, if there's no primary monitor detected, get_primary_monitor just returns the first monitor in the list, and get_primary_monitor calls get_available_monitors internally. This means you're getting inconsistent results between calls.

  21. added
    H - help wanted
    D - hard
    P - high
    on May 19, 2018
  22. francesca64 commented on May 20, 2018

    @francesca64
    Member

    Is it consistently the second call that fails? Note that on master, the monitor list is now cached, so you'd want to disable that: https://github.com/tomaka/winit/blob/cebd15bfd1c5f9915fa5a97cfe0025bc462089e9/src/platform/linux/x11/monitor.rs#L19

    Can you try capturing some logs for me using xtrace (it's probably called x11trace, depending on the package manager)?

  23. spearman commented on May 20, 2018

    @spearman
    ContributorAuthor

    Running with the current master for some reason it takes the xrandr 1.5 path, although the version is 1.4:

    000:<:0034: 12: RANDR-Request(140,0): QueryVersion major-version=1 minor-version=5
    000:<:0035:  8: RANDR-Request(140,8): GetScreenResources window=0x0000028a
    000:>:0034:32: Reply to QueryVersion: major-version=1 minor-version=4
    000:>:0035:152: Reply to GetScreenResources: timestamp=0x00006fee config-timestamp=0x00006fee crtcs=0x0000027a,0x0000027b; outputs=0x0000027c,0x0000027d,0x00000280,0x00000281,0x00000282,0x00000283,0x00000284,0x00000285; modeinfos={id=0x0000027e width[pixels]=1600 height[pixels]=900 dot_clock=110000000 h_sync_start=1664 h_sync_end=1706 h_total=2010 h_skew=0 v_sync_start=903 v_sync_end=906 v_total=912 name_length=8 mode_flags=HSyncNegative,VSyncNegative},{id=0x0000027f width[pixels]=1600 height[pixels]=900 dot_clock=73330000 h_sync_start=1664 h_sync_end=1706 h_total=2010 h_skew=0 v_sync_start=903 v_sync_end=906 v_total=912 name_length=8 mode_flags=HSyncNegative,VSyncNegative}; mode names='1600x9001600x900'
    000:<:0036: 12: RANDR-Request(140,42): UNKNOWN opcode=0x8c opcode2=0x2a unparsed-data=0x8a,0x02,0x00,0x00,0x01,0x00,0x00,0x00;
    000:>:0036:Error 1=Request: major=140, minor=42, bad=650
    [winit X11 error] XError {
        description: "BadRequest (invalid request code or no such operation)",
        error_code: 1,
        request_code: 140,
        minor_code: 42
    }
    monitor count: -1
    thread 'main' panicked at 'capacity overflow', liballoc/raw_vec.rs:741:5
    

    If I set FORCE_RANDR_COMPAT to true here's the output of xtrace:
    https://gist.github.com/spearman/f5d443ae9186fa584cdb6f6a972d7081

  24. francesca64 commented on May 21, 2018

    @francesca64
    Member

    Running with the current master for some reason it takes the xrandr 1.5 path, although the version is 1.4

    That's really weird. The condition should work the same as it did before: https://github.com/tomaka/winit/blob/v0.13.0/src/platform/linux/x11/monitor.rs#L28

    I could just query the extension version instead of basing it on library availability, which is probably more correct anyway.

    (and at bare minimum, an assert that monitor_count isn't negative...)

    Anyway, I found the problem, and it's amusing. I pretty much never use iterators, so when I refactored, I didn't realize that calling next after calling find like that would always result in None (the previous code used two copies of the monitor list, so I'd removed one). This is much better than the conclusion that this is caused by some esoteric race condition.

    New branch: https://github.com/francesca64/winit/tree/x11-unregress-monitors

  25. spearman commented on May 21, 2018

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    B - bugC - needs investigationD - hardDS - x11H - help wantedP - high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions