unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Thiago Melo <tmdmelo@gmail.com>
To: Po Lu <luangruo@yahoo.com>
Cc: 63589@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>
Subject: bug#63589: [PATCH] 29.0.91; crash after creating graphical frames via emacsclient when compiled with cairo-xcb
Date: Wed, 24 May 2023 14:16:20 +0000	[thread overview]
Message-ID: <CABpoeKjY6C2K-iZsov=ChPT3JEQ+zamqUFq9v47T83-E_B+cog@mail.gmail.com> (raw)
In-Reply-To: <87v8gisz6u.fsf@yahoo.com>

[-- Attachment #1: Type: text/plain, Size: 1568 bytes --]

> I thought I explained what the problems with trying to fix this in Emacs
> are.  The first is: there's a reference leak in Cairo somewhere, since
> Emacs never allows displays to be closed without each frame being
> destroyed, and destroying each frame will also dereference its Cairo
> surface; thus, it's not actually Emacs's problem.
[...]
> The other problem occurs when `cairo_xcb_surface_create' creates a
> different device from the one that was previously created for the
> display.  So you have only destroyed one of several devices, any one of
> which may rear its ugly head later.  This is also a bug in Cairo.
>
> BTW, it's not necessary to call XFreePixmap, as all resources created
> by the client will be destroyed per the close down mode set earlier.

Welp, I guess it means that all that remains is bringing up the issue
to the Cairo mailing list (again) and waiting for the problem to be
solved from their side. I'm afraid they might just say that Emacs is
"holding it wrong". :(

By the way, I wrote a minimal standalone cairo-xcb c program (see
attached) to trigger this particular bug. It opens a small window via
xcb, draws something via cairo, destroys the window and closes the
display when any key or mouse button is pressed on it, then recreates
everything again... And it repeats until it crashes (Hopefully. At
lest it crashes in my system). Redrawing errors also happen during the
process. It might be an useful example to bring to the Cairo mailing
list and to debug the root of this issue.

Thanks for everything you taught me, Po Lu.

[-- Attachment #2: cairo-xcb-bug.c --]
[-- Type: text/x-csrc, Size: 5729 bytes --]

/* Example program to trigger reference bugs in cairo-xcb
   when reopening the display.
   This program should crash with the following error message:
   ```
   cairo-xcb-screen.c:219: _get_screen_index: Assertion `!"reached"' failed.
   ```
   Compile with:
   gcc -Wall -o cairo-xcb-bug cairo-xcb-bug.c `pkg-config --cflags --libs cairo-xcb x11-xcb` -O0 -g3
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <xcb/xcb.h>
#include <X11/Xlib-xcb.h>

#include <cairo.h>
#include <cairo-xcb.h>

const unsigned int width = 100;
const unsigned int height = 100;

void
draw (cairo_t *cr)
{
  cairo_push_group (cr);
  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_paint (cr);
  cairo_move_to (cr, 0, 0);
  cairo_line_to (cr, width, height);
  cairo_move_to (cr, 0, width);
  cairo_line_to (cr, height, 0);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_stroke (cr);
  cairo_pop_group_to_source (cr);
  cairo_paint (cr);
}

int
main ()
{
  Display *display;
  xcb_connection_t *connection;
  xcb_screen_t *screen;

  xcb_visualtype_t  *visual_type;
  cairo_surface_t *surface;
  //cairo_device_t *device_ref; // part of the ritual needed to avoid a crash
  cairo_t *context;

  printf ("Press any key or mouse button at the X Window to destroy it and recreate it again.\n\n");
  printf ("Press C-c here to exit.\n");
  while (1)
    {
      /* Reset our variables  */
      display = NULL;
      connection = NULL;
      screen = NULL;
      visual_type = NULL;
      surface = NULL;
      //device_ref = NULL;
      context = NULL;

      display = XOpenDisplay (getenv ("DISPLAY"));
      if (!display) {
        perror ("Cannot open display");
        exit (1);
      }

      /* Open the connection to the X server */
      connection = XGetXCBConnection (display);
      if (!connection)
        {
          perror ("Cannot open connection");
          exit (1);
        }

      /* Get the first screen */
      screen = xcb_setup_roots_iterator (xcb_get_setup (connection)).data;

      /* Create a window  */
      xcb_drawable_t window = xcb_generate_id (connection);

      uint32_t mask =  XCB_CW_BACK_PIXMAP | XCB_CW_EVENT_MASK;
      uint32_t values[2] = {screen->black_pixel,
                            XCB_EVENT_MASK_EXPOSURE |
                            XCB_EVENT_MASK_KEY_PRESS |
                            XCB_EVENT_MASK_BUTTON_PRESS};

      xcb_create_window (connection,                    /* connection          */
                         XCB_COPY_FROM_PARENT,          /* depth               */
                         window,                        /* window Id           */
                         screen->root,                  /* parent window       */
                         0, 0,                          /* x, y                */
                         width, height,                 /* width, height       */
                         10,                            /* border_width        */
                         XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
                         XCB_COPY_FROM_PARENT,          /* visual              */
                         mask, values );                /* masks */


      /* Map the window on the screen and flush */
      xcb_map_window (connection, window);
      xcb_flush (connection);

      /* Boilerplate to get xcb visual for cairo  */
      if (screen)
        {
          xcb_depth_iterator_t depth_iter;

          depth_iter = xcb_screen_allowed_depths_iterator (screen);
          for (; depth_iter.rem; xcb_depth_next (&depth_iter))
            {
              xcb_visualtype_iterator_t visual_iter;

              visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
              for (; visual_iter.rem; xcb_visualtype_next (&visual_iter))
                {
                  if (screen->root_visual == visual_iter.data->visual_id)
                    {
                      visual_type = visual_iter.data;
                      break;
                    }
                }
            }
        }
      if (!visual_type) {
        perror ("Bad visual type");
        exit (1);
      }

      surface = cairo_xcb_surface_create (connection,
                                      window,
                                      visual_type,
                                      width, height);

      if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
        {
          perror ("Bad cairo surface");
          exit (1);
        }

      //device_ref = cairo_device_reference (device); // part of the ritual to make this program crash-free

      context = cairo_create (surface);
      if (cairo_status (context) != CAIRO_STATUS_SUCCESS)
        {
          perror ("Bad cairo context");
          exit (1);
        }

      int loop = 1;
      while (loop) {
        xcb_generic_event_t *event;
        event = xcb_wait_for_event (connection);
        switch (event->response_type & ~0x80)
          {
          case XCB_BUTTON_PRESS:
          case XCB_KEY_PRESS:
            loop = 0;
            break;
          case XCB_EXPOSE:
            draw (context);
            cairo_surface_flush (surface); // not needed here, apparently?
            xcb_flush (connection);
            break;
          default:
            /* Unknown event type, ignore it  */
            break;
          }

        free (event);
      }
      usleep (100000);

      cairo_destroy (context);
      cairo_surface_destroy (surface);

      /* Must destroy the device before closing the display
         to avoid bugs with Cairo-XCB  */
      //cairo_device_finish (device_ref);
      //cairo_device_destroy (device_ref);

      XCloseDisplay (display);
      usleep (100000);
    }

  return 0;
}

  reply	other threads:[~2023-05-24 14:16 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-19 11:17 bug#63589: 29.0.91; crash after creating graphical frames via emacsclient when compiled with cairo-xcb Thiago Melo
2023-05-20  1:46 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-20 11:47   ` Thiago Melo
2023-05-21  0:42     ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-21 18:25       ` Thiago Melo
2023-05-20 22:47 ` bug#63589: [PATCH] " Thiago Melo
2023-05-21 13:40   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-21 14:30     ` Eli Zaretskii
2023-05-21 16:10       ` Thiago Melo
2023-05-21 17:42         ` Eli Zaretskii
2023-05-22  0:56           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-22  2:48             ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-22 10:59               ` Eli Zaretskii
2023-05-22 11:17                 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-22 11:40                   ` Eli Zaretskii
2023-05-22 12:07                     ` Thiago Melo
2023-05-22 13:12                       ` Thiago Melo
2023-05-22 19:21                         ` Thiago Melo
2023-05-23  0:30                           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-23 11:37                             ` Eli Zaretskii
2023-05-23 12:08                               ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-23 13:01                                 ` Eli Zaretskii
2023-05-23 13:18                                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-23 14:20                                     ` Eli Zaretskii
2023-05-24  0:22                                       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-24  2:30                                         ` Eli Zaretskii
2023-05-24  3:13                                           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-24  5:15                                             ` Thiago Melo
2023-05-24 11:07                                               ` Eli Zaretskii
2023-05-24 11:54                                                 ` Thiago Melo
2023-05-24 12:15                                                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-24 14:16                                                     ` Thiago Melo [this message]
2023-05-24 15:44                                                     ` Eli Zaretskii
2023-05-25  0:18                                                       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25  3:38                                                         ` Eli Zaretskii
2023-05-25  6:08                                                           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25  7:12                                                             ` Eli Zaretskii
2023-05-25 10:24                                                               ` Thiago Melo
2023-05-25 10:32                                                                 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25 14:06                                                                   ` Thiago Melo
2023-05-25 18:17                                                                     ` Thiago Melo
2023-05-26  0:59                                                                     ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-26  5:06                                                                       ` Thiago Melo
2023-05-26  6:14                                                                       ` Eli Zaretskii
2023-05-25 10:34                                                               ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25 11:33                                                                 ` Eli Zaretskii
2023-05-26  0:23                                                                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-26  6:10                                                                     ` Eli Zaretskii
2023-05-26  8:01                                                                       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-26  8:34                                                                         ` Eli Zaretskii
2023-05-24 11:01                                             ` Eli Zaretskii
2023-05-21 16:09     ` Thiago Melo
2023-05-22  1:05       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-22  5:23         ` Thiago Melo
2023-05-28  3:10 ` bug#63589: " Andrés Ramírez
2023-05-28  3:34 ` Andrés Ramírez
2023-05-28  5:55   ` Eli Zaretskii
2023-05-29 14:51     ` andrés ramírez
2023-05-28 21:23 ` Thiago Melo
2023-05-29 14:58   ` andrés ramírez
2023-05-29 15:21     ` Thiago Melo
2023-05-29 15:37       ` andrés ramírez
2023-05-29 16:10         ` Thiago Melo
2023-05-29 16:21           ` andrés ramírez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABpoeKjY6C2K-iZsov=ChPT3JEQ+zamqUFq9v47T83-E_B+cog@mail.gmail.com' \
    --to=tmdmelo@gmail.com \
    --cc=63589@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=luangruo@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).