Repository: qtile/qtile

Issue:
Firefox cpu usage escalates and then stops responding only when run in qtile
Hi, 

When starting firefox (firefox 69.0-1, qtile 0.14.2-2) its cpu usage starts rising to the point were the interface becomes unresponsive, also only parts of firefox interface are drawn, but like after 5 mins the window is not visible at all, may be the screenshots will make things clearer. 

screenshots with description:
https://imgur.com/a/V17wJrM

Nothing is there in the logs (I only checked .local/share/qtile/qtile.log), this does not happen with me under i3 or bspwm. I also use optirun with my browsers but it does not matter if I start firefox without it. this bug happens every time I use firefox, and I use a lot of gtk3, gtk2, qt4, qt5, electron apps and it only happens with firefox on qtile only.

I am not sure what other information I can provide.



Discussion:
I also tried changing compton backend, or running qtile without a compositor completely because I read in some bug reports here that sometimes compositors cause similar issues, but that didn't help either.. 
I get this too. Initially I thought it was related to firefox addons and whatnot, but I've tried safe-mode or a totally fresh firefox profile and I still get the same issue.

Also, it happens regardless of running compton.
Also reported by another user in a [reddit post on /r/firefox](https://www.reddit.com/r/firefox/comments/cyziu4/ff_6802_severe_input_lag_and_later_freezing_on/).
I can confirm this issue with Firefox (as per the /r post). I recently formatted my computer (again) and installed I3 (with all other major components being the same), which solved the Firefox issue (and gave better overall RAM performance). 
Unfortunately I haven't had time to dig deeper to investigate the problem.
Which Python version are you using to run Qtile, everyone? Also, which graphic driver?
> Which Python version are you using to run Qtile, everyone? Also, which graphic driver?

Hm, unfortunately I can't recall all details of my previous setup. But I believe I was using `Python 3.7` and IIRC the driver was `i915`.
Thank you @ramnes 

* distro: Fedora 30 (Had it with ArchLinux too)
* linux: 5.3.13
* python 3.7.5
* qtile 0.13.0
* graphics driver: i915

Also, how long does it take before Firefox becomes unresponsive? Do I need to browse stuff or will the CPU raise even if I just launch Firefox and leave it alone?
I haven't tried without any user interaction.
For me, it happens in ~5-10 mins of normal usage.

I have observed that typing in text boxes makes it worse, especially when switching the keyboard layout often.
@ramnes Were you able to reproduce it?
unable to reproduce: py3.8 + arch + xinit + firefox71 + no compton + t430
Could the login manager have anything to do with it? FTR I'm on `lightdm`.
No, I could not reproduce so far, but I didn't give it much time.
Interesting. But the issue was present in Archlinux too.
> Which Python version are you using to run Qtile, everyone? Also, which graphic driver?

 It was python 3.7

I use nvidia drivers for the Nvidia graphics card
and `xf86-video-intel` for intel card, in addition to bumblebee and optirun since this is a hybrid graphics card.
I traced things a bit, and found out qtile was spawning `setxkbmap` every second or so, which comes from the `KeyboardLayout` widget. The slowdown possibly comes from the usage of `InLoopPollText`?

In any case, after taking out the widget, firefox doesn't become unresponsive!
> I traced things a bit, and found out qtile was spawning `setxkbmap` every second or so, which comes from the `KeyboardLayout` widget. The slowdown possibly comes from the usage of `InLoopPollText`?
> 
> In any case, after taking out the widget, firefox doesn't become unresponsive!

Great! Thanks for sharing!
Does anyone have any idea on why running setxkbmap every second would make Firefox unresponsive?
> I traced things a bit, and found out qtile was spawning `setxkbmap` every second or so, which comes from the `KeyboardLayout` widget. The slowdown possibly comes from the usage of `InLoopPollText`?

I can confirm I had the same widget on my QTile config file when I had the firefox issue.
I kept this running in a terminal:

```
$ while :; do setxkbmap -verbose 10; sleep 1; done
```

and indeed keyboard input in textboxes in firefox freezes for ~0.5s every second. So does switching tabs.

Also CPU usage for firefox and Xorg is very high during that time:

```
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                   
 1964 root      20   0  416380 150744  96012 R  57.8   0.9  23:33.97 Xorg                                                                                      
 2743 danmilon  20   0 3651304 477288 193020 S  57.5   2.9  23:01.97 firefox                  
```

As soon as the `setxkbmap` loop is stopped, things go back to normal.

Resolving patch:
diff --git a/libqtile/widget/keyboardlayout.py b/libqtile/widget/keyboardlayout.py
--- a/libqtile/widget/keyboardlayout.py
+++ b/libqtile/widget/keyboardlayout.py
@@ -97,7 +97,7 @@ def keyboard(self):
         Examples: "us", "us dvorak".  In case of error returns "unknown".
         """
         try:
-            command = 'setxkbmap -verbose 10'
+            command = 'setxkbmap -verbose 10 -query'
             setxkbmap_output = self.call_process(command.split(' '))
             keyboard = self.get_keyboard_layout(setxkbmap_output)
             return str(keyboard)


Test patch: