Tk Source Code

View Ticket
Login
Ticket UUID: f1fe739a4fbb046d66e20cc82ecdf9fa72106662
Title: Embedded application exits with an X error when its container is destroyed while it draws or claims the focus
Type: Bug Version: 8.6.18, 9.0.5, 9.1.0
Submitter: serhiy.storchaka Created on: 2026-10-09 07:03:43
Subsystem: 74. Application Embedding Assigned To: aku
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2026-10-12 05:29:46
Resolution: None Closed By: nobody
    Closed on:
Description:

An embedded application still exits with an X error if its container is destroyed while it is drawing or setting the focus, although [4a25f316f7] made ErrorProc() ignore errors for windows which no longer exist. Destroy the container while the embedded application does not process events, then let it redraw an entry or call focus -force (as in unixEmbed-9.3):

X Error of failed request:  BadDrawable (invalid Pixmap or Window parameter)
  Major opcode of failed request:  70 (X_PolyFillRectangle)

X Error of failed request:  BadWindow (invalid Window parameter)
  Major opcode of failed request:  25 (X_SendEvent)

An entry (like other double-buffered widgets) draws into a pixmap created for its window. Creating the pixmap fails, and this error is ignored, but the pixmap does not exist, so the following requests which draw into it fail with errors for the pixmap, which are not ignored; with Xft, drawing text also fails with RenderBadPicture, since the XRender picture for the pixmap is not created either. focus -force sends an event to the container, which is not protected by an error handler.

This makes test unixEmbed-5.4 of branch x11-embed-focus-return fail intermittently in CI (the embedded entry keeps the focus and redraws its blinking insertion cursor after the container is destroyed), which kills the embedded application and makes the following unixEmbed tests fail with "broken pipe".

User Comments: chw added on 2026-10-12 05:29:46:
So far so good. Now I wonder, if the logic around "deletedWinTable" is needed
at all. Given that BadWindow and BadDrawable errors are dealt with in the
tkError.c module (i.e. masked as long as the Window identifier is known),
and that at known places e.g. in tkUnixFont.c and friends specific other
errors are enclosed with Tk_Create/DeleteErrorHandler, nothing should be left
requiring a bookkeeping heuristic of recently destroyed window identifiers.

Remember, the latest Pixmap and GC changes decoupled their allocation from
(possibly invalid) Window identifiers, since these resources are bound to
the Display/Screen combination anyway. There's still Picture from
Xft/XRender, which can have a direct relation to a Window, but this can
be specifically dealt with using the Tk_ErrorHandler mechanism in the
font rendering functions in tkUnixRFont.c.

serhiy.storchaka added on 2026-10-10 09:07:13:

Thank you, Christian. I tried this in branch embed-container-destroyed-root-pixmap: pixmaps are created for the root window, so tracking the pixmaps which could not be created is not needed, and it is simpler than embed-container-destroyed-pixmap.

GCs have the same problem: Tk_GetGC() and Tk_GCForColor() created them for the widget's window or its double-buffer pixmap. A GC created after the container was destroyed is invalid, and since GCs are shared, drawing with it fails also in live windows. Both branches now create GCs for the root window or a pixmap of the right depth. Text drawn by Xft directly into a window (e.g. by a message widget) still needs a handler for RenderBadPicture in both.

New tests unixEmbed-9.6 and 9.7.


chw added on 2026-10-10 05:34:21:
Maybe the BadPixmap error can be avoided entirely. According to the
man page of XCreatePixmap, the Drawable "d" argument has to specify
a drawable of the screen of the display the pixmap is to be used on.
Serendipitously, the root window of the screen of the display is such
a drawable and always available, even if the X11 window of the respective
Tk_Window/TkWindow has been (asynchronously) destroyed.

So in cases where a Tk_Window/TkWindow is involved, this Drawable can
be retrieved by a macro like

  #define Tk_DrawableForPixmap(tkwin) \
      RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin))

serhiy.storchaka added on 2026-10-09 07:08:20:

Proposed fix in branch embed-container-destroyed-pixmap: ErrorProc() remembers a pixmap whose creation failed because the window no longer exists, and ignores the errors for it until it is freed; Xft text drawing ignores RenderBadPicture errors; the events sent to the window of the other application are protected by error handlers. New tests unixEmbed-9.4 and 9.5.