Here's a patch to fix this issue. It was created on top of the emacs-29 branch, commit 6b60c81. It's based on the suggestion from the cairo mailing list (see the link I sent in my original message here). It ensures that the cairo device associated with the cairo-xcb surfaces in the display is destroyed before closing the display. It can probably be improved. It could even be extended to handle cairo-xlib. In the cairo mailing list, they mentioned one corner case where the xlib device is not properly destroyed: when cairo is unloaded before the X11 connection is closed.