Pkexec is slow when high open file descriptor limit is configured (as it attempts to close all possible FDs)
This comes as a result of digging into https://forum.manjaro.org/t/very-slow-response-of-brightness-controls-in-gnome/74891/17
~ >>> time sudo pkexec echo "1" 1 sudo pkexec echo "1" 0.41s user 0.24s system 98% cpu 0.661 total
pkexec attempts to close all possible file descriptors (see pkexec.c:256). on my system the ulimit was configured by another package (steam) to 1048576. The result is a very slow pkexec, for example:
Apt-cache apparently used to have a similar issue and they solved it by iterating over all open file descriptors (see commit)
As a workaround, I changed the limit to 4096. This improves pkexec runtime a lot.
~ >>> time sudo pkexec echo "1" 1 sudo pkexec echo "1" 0.03s user 0.02s system 78% cpu 0.065 total