Solution to the error "Maximum number of clients reachedUnable to init server" in Linux.

A few days ago, I was experiencing problems with KDE Plasma applications on my Arch Linux machine. It turns out that from time to time, some applications would not open, which is quite rare! Eventually, I reached a point where I could only have one application open at a time because the others were not working.
If I tried to run an application from the terminal, I would get the following error:
Maximum number of clients reachedUnable to init server: Could not connect: Connection refused
Error: cannot open display: :0
In this post, I'll explain why this error occurs and how to fix it!.
Origin
This error is caused because the maximum number of clients for the Xorg window system has been exceeded. Typically, Xorg accepts a maximum of 256 simultaneous clients. One way to verify this is to install the xrestop package on your Linux distro. When we run the command xrestop
, we will get a result like the following:

Notice that in my particular case (Monitoring 252 clients), there are 252 simultaneous Xorg clients and 1 error (XErrors) that occurred when trying to start firefox unsuccessfully. At this point, it is important to mention that an application can consume more than one simultaneous client! And as you know, each tab in your browser is an independent process, so opening a new tab involves starting a new connection to Xorg.
Solution
The solution to this problem is to assign more clients to Xorg. To do this, you must start the system in console mode or stop the Display Manager. In my particular case, I use sddm, but you will probably use gdm or some other.
systemctl stop sddm
I think it goes without saying that you should do this in a tty that is not in the graphical system. You can access one by pressing CTRL+ALT+Digit. Once you have done this, you must enter the Xorg configuration directory, in my case:
cd /etc/X11/xorg.conf.d/
In this directory, you can create your custom Xorg configurations by naming your files with the pattern XX-*.conf. In this particular case, you can create a file named 99-maxclients.conf in which you will enter the following content:
Section "ServerFlags"
Option "MaxClients" "512"
EndSection
It turns out that these files are analyzed by the X server upon boot and merged with the main xorg.conf file. Once you have done this, you can restart the Display Manager and verify that everything is working properly.
systemctl start sddm
You can even run xrestop again and verify that you can have more than 256 connections to Xorg. Fantastic! Until next time!