"X11 could not be opened" then "X11 canceled restart" I occasionally run into the following first-world nightmare, usually when trying to run open source software (in this case Inkscape on a Mac Pro running Lion, though I've seen this error before as well, I think with Wine and OpenOffice if I remember right): 




First, it fails to open with this error:



  The application X11 could not be opened.
  
  An error occurred while starting the X11 server: "Cannot establish any listening sockets - Make sure an X server isn't already running"
  
  Click Quit to quit X11. Click Report to see more details or send a report to Apple.
  
  [Report...] [Quit]



Clicking 'Quit' just causes the exact same error to endlessly pop up again. Force Quitting the application that caused the error (Inkscape) or X11 itself has the same result. As does quitting from the dock.
If I try to just ignore it, it refreshes itself after a few minutes or so, and the icon keeps bouncing in the dock.
Attempting to restart to break the cycle fails with this sadistic error:



  The application X11 canceled restart.
  
  To try again, quit X11 and choose Restart from the Apple menu.


But the reason I want to restart is to make X11 quit and stop it from reopening itself!

Report... causes X11 to crash with an "X11 quit unexpectedly" error - but it still automatically re-opens itself and it still cancels shutdown / restart in exactly the same way. Shut down has the same error as restart: "The application X11 canceled shut down".



Any ideas how to:


Get X11 to go away so I can restart and continue what I was doing? (without just pulling the plug)
(for bonus points) Stop things like this from happening? There's no clues an average user like me can make sense of in any of the above for how to debug and fix the underlying problem.
You can force X11 to quit, which will allow for a normal reboot but won't fix whatever problems are causing X11 to fail to launch / hang / otherwise misbehave.

Open terminal and type:

 killall X11


You might use sudo to kill any X1 processes that you don't own, but use care with root and killing processes.