From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Thiago Melo Newsgroups: gmane.emacs.bugs Subject: bug#63589: [PATCH] 29.0.91; crash after creating graphical frames via emacsclient when compiled with cairo-xcb Date: Thu, 25 May 2023 10:24:30 +0000 Message-ID: References: <83wn10ppa1.fsf@gnu.org> <874jo3yjly.fsf@yahoo.com> <837cszp9cc.fsf@gnu.org> <87cz2rw8pz.fsf@yahoo.com> <83y1lfnqwb.fsf@gnu.org> <87y1lfuqyb.fsf@yahoo.com> <83wn0znn7j.fsf@gnu.org> <87lehevaqz.fsf@yahoo.com> <83r0r6o3zk.fsf@gnu.org> <87edn6v2v3.fsf@yahoo.com> <83h6s2ng1p.fsf@gnu.org> <87v8gisz6u.fsf@yahoo.com> <83bki9ohsc.fsf@gnu.org> <87o7m9tga9.fsf@yahoo.com> <83sfblm66s.fsf@gnu.org> <87h6s1t01w.fsf@yahoo.com> <83jzwwnatg.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2339"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Po Lu , 63589@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu May 25 12:26:27 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q28As-0000LU-Uf for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 25 May 2023 12:26:27 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q28AX-00075z-Vu; Thu, 25 May 2023 06:26:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q28AU-00075Y-SY for bug-gnu-emacs@gnu.org; Thu, 25 May 2023 06:26:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q28AU-0008TW-Ja for bug-gnu-emacs@gnu.org; Thu, 25 May 2023 06:26:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1q28AU-00070V-1J for bug-gnu-emacs@gnu.org; Thu, 25 May 2023 06:26:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Thiago Melo Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 25 May 2023 10:26:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63589 X-GNU-PR-Package: emacs Original-Received: via spool by 63589-submit@debbugs.gnu.org id=B63589.168501031826882 (code B ref 63589); Thu, 25 May 2023 10:26:01 +0000 Original-Received: (at 63589) by debbugs.gnu.org; 25 May 2023 10:25:18 +0000 Original-Received: from localhost ([127.0.0.1]:46013 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q289m-0006zW-58 for submit@debbugs.gnu.org; Thu, 25 May 2023 06:25:18 -0400 Original-Received: from mail-yw1-f172.google.com ([209.85.128.172]:54481) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q289i-0006sz-BO for 63589@debbugs.gnu.org; Thu, 25 May 2023 06:25:16 -0400 Original-Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-561e919d355so5300127b3.0 for <63589@debbugs.gnu.org>; Thu, 25 May 2023 03:25:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685010308; x=1687602308; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=cK+oj0b4uSbb/Ovc7lyZQ5iEXdU8W0jKRZCFuZ9D8qg=; b=GYXSG7ZQhQm6z4Lj6wj94DbnEhD878iuyOi+j/+8smXYSZqeXw0ZNrEM12815fV53W MPDTxxQ4kKIhBa5ts+PpLShlsCErSIwceRB7EZcF+1dG8X3QnMucGNijbfPTH68mPgLv 8FNmvWm3UCa0ivRwhNdtQ8LlY+d5xSSV1GoEkmMDAM8T90/Y/TQeyk7dv9mD7uA9iLrL nT41IS5jG809PZb+SQ64PhspGZ5Ft8eU4LjNihu6nEPQoZTXIFIpuURa64sXIGq6ahSM vTuiOy5rOnZt8KsL+K2xSR1xaAQRG+DYx0UpLAweZtm/4jcG7p0lKZ6ayUiEvhTjdW66 yYkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685010308; x=1687602308; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cK+oj0b4uSbb/Ovc7lyZQ5iEXdU8W0jKRZCFuZ9D8qg=; b=VMrEJtrzMHKOpNGANmJQxZu4VqPL2IHcB5fgY9zCu2gBqXqpuZTSZuSxVfRo3qyL+j CYTfyKqAutZUffPogXOi12eOALg1Fqp4/H84qPLpV7CFgkhNJTgR7PXf0xc9aqEZp7wA TIGLmLbGVaz6RcTpcvey/t0S36STB6sqptOPRNPXxo+Ft2yoPCRtc1WHRdbMAYs9Ey+q /t6uldbt3CQdbyPIyMErbUfzpZMxf5f9Gbd6dYyc49ARoItWvYrVBICro7H1K7IEB+YI c5SbqHpW3/MMQBGZOc2tjyxc5DRl0Bp6v3tyFMs4i28JWdlfewS6qdyjsJRUEJzEBGvH vTSg== X-Gm-Message-State: AC+VfDw4fe2B+DHmicTQHWoSUgLOeuENpoBu6g0ABZ3xzrN/FGzZKpJh KFeSVj3VYmOCQbV5Rv3aByMd/XHjXZ8q26zhyvQ= X-Google-Smtp-Source: ACHHUZ5gcrDOYWt1UBw00ON+qJYVHqD6737xTUcNTRk7YCeAsMrNnQKqYxNURg8DwfAlqEzXlEvCo9vwpl/zUNN1xww= X-Received: by 2002:a81:6d85:0:b0:561:b476:6c4f with SMTP id i127-20020a816d85000000b00561b4766c4fmr23554085ywc.36.1685010308023; Thu, 25 May 2023 03:25:08 -0700 (PDT) In-Reply-To: <83jzwwnatg.fsf@gnu.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:262353 Archived-At: On Thu, May 25, 2023 at 7:12=E2=80=AFAM Eli Zaretskii wrote: > I'm trying to establish if there's anything we could do in the > cairo-xcb configuration to make the crashes more rare, or even prevent > them altogether. Regarding this, before posting at the cairo mailing list, I searched better their archives to check if this issue was already properly addressed. I found this: https://lists.cairographics.org/archives/cairo/2018-November/028791.html Title: cairo_xcb_surface_create() segfaults on second call with different xcb info Where Uli Schlachter (libxcb contributor and main maintainer of Cairo-XCB nowadays) discusses the issue we're having here, the design of Cairo-XCB, how to use it with multiple surfaces and after reopening the display. To highlight, Uli says: | Cairo has to get quite some information from the X11 server. [...] | | [...] querying this all the time would be slow. Thus, cairo caches | this information. Namely, there is an instance of cairo_device_t. This | instance is kept around even when all surfaces using this device are | destroyed. [...] | | [...] when you call xcb_disconnect(), | the cache now contains a dangling pointer. The next call to | xcb_connect() might very well allocate an xcb_connection_t* with the | same pointer. Thus, you now get a cache hit even though there is a new | XCB connection. Bad things happen afterwards Thus, it doesn't matter if Emacs destroys all cairo-xcb surfaces before closing the display, the device reference will always linger around together with all its cairo cache by design. It simply doesn't work like Cairo-XLib at all. Then, Uli says: | If you want to keep the device around for later (i.e. have multiple | surface using the same device), you can save a pointer via: | | cairo_device_t *device =3D cairo_device_reference(....); | | Now, you have to later call cairo_device_destroy() when you no longer | need the reference, but you get a pointer to the cairo_device_t | independent of a cairo xcb surface. | | Oh and: You have to finish the device before you call xcb_disconnect(). So, any application that uses Cairo-XCB with multiple surfaces and wants to reopen displays _must_ save a reference to the device and _must_ finish + destroy it before closing the display. With this, here's another try to improve the initial patch, this time storing the cairo xcb device for the display at `x_term_init': #+begin_src diff --- a/src/xterm.h 2023-05-25 09:43:50.943793850 +0200 +++ b/src/xterm.h 2023-05-25 11:32:03.701771148 +0200 @@ -883,6 +883,13 @@ struct x_display_info clock, or 0 if unknown (if the difference is legitimately 0, server_time_monotonic_p will be true). */ int_fast64_t server_time_offset; + +#if defined USE_XCB && defined USE_CAIRO_XCB + /* Cairo device associated with cairo surfaces in this display. + Required for proper cleanup before closing display connection + in cairo-xcb builds. */ + cairo_device_t *cairo_device; +#endif #endif }; #+end_src #+begin_src diff --- a/src/xterm.c 2023-05-25 09:37:24.811402435 +0200 +++ b/src/xterm.c 2023-05-25 12:18:06.003572028 +0200 @@ -5806,10 +5806,15 @@ x_begin_cr_clip (struct frame *f, GC gc) cairo_surface_t *surface; #ifdef USE_CAIRO_XCB_SURFACE if (FRAME_DISPLAY_INFO (f)->xcb_visual) + { surface =3D cairo_xcb_surface_create (FRAME_DISPLAY_INFO (f)->xcb_conn= ection, (xcb_drawable_t) FRAME_X_RAW_DRAWABLE (f), FRAME_DISPLAY_INFO (f)->xcb_visual, width, height); + if (cairo_surface_status (surface) =3D=3D CAIRO_STATUS_SUCCESS) + eassert (FRAME_DISPLAY_INFO (f)->cairo_device + =3D=3D cairo_surface_get_device (surface)); + } else #endif surface =3D cairo_xlib_surface_create (FRAME_X_DISPLAY (f), @@ -30504,6 +30509,27 @@ x_term_init (Lisp_Object display_name, c unblock_input (); +#ifdef USE_CAIRO_XCB_SURFACE + /* Store reference to the cairo device for this display, to ensure + that it is destroyed before closing connection (Bug#63589). + For that, we create a drawable, an XCB surface for that drawable, + and then we get the device reference from there. */ + Pixmap drawable; + cairo_surface_t *surface; + + drawable =3D XCreatePixmap (dpyinfo->display, dpyinfo->root_window, + 1, 1, dpyinfo->n_planes); + surface =3D cairo_xcb_surface_create (dpyinfo->xcb_connection, drawable, + dpyinfo->xcb_visual, 1, 1); + + if (cairo_surface_status (surface) =3D=3D CAIRO_STATUS_SUCCESS) + { + dpyinfo->cairo_device =3D cairo_device_reference (cairo_surface_get_device (surface)); + cairo_surface_destroy (surface); + } + XFreePixmap (dpyinfo->display, drawable); +#endif + #if defined HAVE_XFIXES && defined USE_XCB SAFE_FREE (); #endif @@ -30783,6 +30809,17 @@ x_delete_terminal (struct terminal *term xim_close_dpy (dpyinfo); #endif +#ifdef USE_CAIRO_XCB_SURFACE + /* Ensure that the cairo device is destroyed before closing + connection (Bug#63589). */ + if (dpyinfo->cairo_device) + { + cairo_device_finish (dpyinfo->cairo_device); + cairo_device_destroy (dpyinfo->cairo_device); + dpyinfo->cairo_device =3D NULL; + } +#endif + /* Normally, the display is available... */ if (dpyinfo->display) { #+end_src