Bug 16702 - x11 plotting misses ConfigureNotify event in tiling window managers; can't interrupt plotting
Summary: x11 plotting misses ConfigureNotify event in tiling window managers; can't in...
Status: UNCONFIRMED
Alias: None
Product: R
Classification: Unclassified
Component: Graphics (show other bugs)
Version: R 3.2.3
Hardware: Other Linux
: P5 minor
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-02-06 03:25 UTC by Frederick Eaton
Modified: 2016-07-07 23:53 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Frederick Eaton 2016-02-06 03:25:57 UTC
I sent the following bug to i3 (https://github.com/i3/i3/issues/2195):

    I'm not sure if this is a bug with i3 or R (the statistics package). When I open a plot window under R, it opens in a new i3 window, as I want it to. However, the plot is drawn small, perhaps to match some initially-requested window size. It stays small until I press a key (such as Alt, my Mod1). Then R notices that the window is full-screen, and redraws the plot to take up the whole window. If the plot is time-consuming to draw, this can be especially annoying.

    The problem can be reproduced by e.g. running R and making a simple plot:

        $ R
        > with(cars, plot(dist~speed))

    I'll submit a bug to R if this is an R bug, but I guess the i3 people would have a better idea what needs to be fixed in R's x11 code, so I'm asking here first. Thanks in advance!

        $ i3 --version
        i3 version 4.11 (2015-09-30, branch "4.11") © 2009 Michael Stapelberg and contributors

        $ R --version
        R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"

The reply was:

    R's window needs to redraw itself on the ConfigureNotify correctly. As you suspect, I think this is a bug in R. i3 is not involved in the client's rendering and the notify event is sent by the X server.

----------------

In interpreting this reply, I infer that there are two problems: (A) the ConfigureNotify event which arrives soon after the window is created is not handled before the plot starts drawing; (B) the event is not handled after the plot is done drawing.

I see no reason why fixing (B) should require any big changes in the R code. I've run into the same problem when creating small X11 applications, and I recall the fix being pretty simple.

Since R's X11 plot drawing code seems to be single-threaded (?), perhaps (A) will be difficult to solve. However, if the code were made multi-threaded, or otherwise changed to be able to respond to external events while the plot is drawing, then this would fix some other outstanding problems as well. One problem (call it (C)) I run into a lot is where I accidentally try to create a plot with millions of points, and have to wait until the plot finishes drawing before I can close it (or I'm faced with the choice of waiting several minutes for the plot to finish, versus killing R and losing my data).

I note that most R computations can be terminated with Control-C in the R prompt. However, plotting seems to be an exception. And even when I press Control-C during a plot() command, if the plot window gets resized and decides to redraw (for example as mentioned in my i3 issue), then I have to wait for it to finish drawing all over again, before I can continue to try to fix the situation at the R prompt (with e.g. dev.off()...).

Given these observations, I think it would be good if problem (B) were fixed together with problems (A) and (C), by rewriting the plot code in such a way that it can respond to external events while drawing. This might be fairly easy, and might not require multithreading at all - for instance, in between drawing commands, check for a SIGINT signal or a ConfigureNotify or WM_DELETE_WINDOW event.
Comment 1 Frederick Eaton 2016-06-28 05:44:21 UTC
The problem with new plot windows missing ConfigureNotify seems related to revision number 16974 by user 'pd':

    $ svn log -v -r 16974
    ------------------------------------------------------------------------
    r16974 | pd | 2001-11-29 10:59:13 -0800 (Thu, 29 Nov 2001) | 2 lines
    Changed paths:
       M /trunk/src/modules/X11/devX11.c
       M /trunk/src/unix/devices.c

    X11 segfault fix - I hope

    ------------------------------------------------------------------------
    $ svn diff -r 16973:16974
    Index: src/unix/devices.c
    ===================================================================
    --- src/unix/devices.c  (revision 16973)
    +++ src/unix/devices.c  (revision 16974)
    @@ -169,6 +169,7 @@
            if (!(dev = (NewDevDesc *) calloc(1, sizeof(NewDevDesc))))
                return 0;
            /* Do this for early redraw attempts */
    +       dd->newDevStruct = 1;
            dev->displayList = R_NilValue;
            if (!ptr_GTKDeviceDriver ((DevDesc*)dev, display, width, height, ps)) {
                free(dev);
    @@ -204,6 +205,7 @@
            if (!(dev = (NewDevDesc *) calloc(1, sizeof(NewDevDesc))))
                return 0;
            /* Do this for early redraw attempts */
    +       dd->newDevStruct = 1;
            dev->displayList = R_NilValue;
            if (!ptr_GnomeDeviceDriver((DevDesc*)dev, display, width, height, ps)){
                free(dev);
    Index: src/modules/X11/devX11.c
    ===================================================================
    --- src/modules/X11/devX11.c    (revision 16973)
    +++ src/modules/X11/devX11.c    (revision 16974)
    @@ -1773,7 +1773,10 @@
         }

         Rf_setNewX11DeviceData((NewDevDesc*)(dd), xd);
    +
    +#if BUG
         R_ProcessEvents((void*) NULL);
    +#endif

         return TRUE;
     }

Removing the "#if BUG" lines from devX11.c fixes the problem, I checked in gdb that ConfigureNotify is received immediately after the plot window opens (and this is apparent visually as well). I haven't figured out yet how to dig around in the old code, but searching for 'newDevStruct' in the current R repository turns up only one line, in "src/include/R_ext/GraphicsEngine.h":

     * Version 5:  Clean up 1.4.0/2.0.0 changes!
     *             Remove newDevStruct from GEDevDesc and NewDevDesc.

So it's possible that the changes to devX11.c and devices.c that I'm looking at in revision 16974 were meant to complement each other in some way, and that the bug I reported was due to a later change, either when newDevStruct was removed or sometime before that.

Anyway, I'm using a version without the "#if BUG" lines in devX11.c, and I'll report back if I don't get any segfaults. I'm not sure exactly what else I should do to test, since revision 16974 doesn't point us to a bug report. I guess I could run R in valgrind...

Anyone know who user 'pd' is?
Comment 2 Mark O'Connell 2016-06-29 17:44:22 UTC
I would guess Peter Dalgaard