Patches originally posted at https://lists.x.org/archives/xorg-devel/2019-January/057865.html.
In the case the compositor wants to handle on-demand startup of Xwayland, it would typically set up a display socket on behalf of Xwayland, listen for incoming data, and spawn Xwayland whenever it has to handle incoming requests from a launched X client.
Xwayland usually expects that the compositor sets itself as a X11 compositor beforehand, and client windows in need of a backing
wl(_shell)_surface get mapped after this. However in this situation, the socket is already filled up with client requests, and the compositor cannot beat it at setting itself up. These patches aim to handle either order.
Note: The original patch discussion drifted into allowing an arbitrary set of clients (compositor, xrdb, gnome-settings-daemon, ...) to set themselves up before allowing the spawned client to talk with the server. I think that would be nice to handle eventually (although seems non-trivial), but also feel it's somewhat independent of this event ordering issue, so is not handled ATM.