Tk Source Code

View Ticket
Login
2026-09-23
20:41 • Ticket [bf65b83d] Tk crash: XIO: fatal IO error 88 (Socket operation on non-socket) status still Open with 3 other changes artifact: 607477d8 user: serhiy.storchaka
19:14 • Ticket [923174ff] Can't catch loss of X server displaying slave toplevel status still Open with 3 other changes artifact: 9d217507 user: serhiy.storchaka
19:00
INCOMPLETE experiment, do not merge: close the connection to a display which is not used any more [923174]

Tk opened a display when the first window was created on it, but never closed it, even after all windows on that display were destroyed. If the X server was terminated then, the next XFlush() on the dead socket killed the whole application, although no window of it was displayed there any more. Here the last Tk_DestroyWindow() on a display which no Tk application uses schedules a check at idle time, and the check closes the display if no window has been created on it in the meantime.

This fixes the reported case, but it is not enough: creating a window again on a display which was closed fails in the font layer, because TkCloseDisplay() does not release borders, bitmaps, colors, cursors and fonts. See [923174]. closed check-in: 7caf2758 user: serhiy.storchaka tags: x11-close-unused-display

2023-12-10
17:29 • Ticket [923174ff] Can't catch loss of X server displaying slave toplevel status still Open with 3 other changes artifact: 271b6591 user: chw
15:49 • Ticket [c23f79ef] Xft text is unusable for 32-bit visual when default visual is 24-bit status still Open with 3 other changes artifact: 698ce72c user: fvogel
15:39 • Ticket [923174ff] Can't catch loss of X server displaying slave toplevel status still Open with 7 other changes artifact: 075d0373 user: sbron
2005-12-01
14:44 • Ticket [923174ff]: 4 changes artifact: 69092ff3 user: hobbs
14:44 • Ticket [923174ff]: 1 change artifact: 7dee0f3b user: hobbs
2004-03-25
23:14 • Ticket [923174ff]: 4 changes artifact: be6acbbb user: jenglish
15:08 • New ticket [923174ff]. artifact: 90a8fba0 user: johnseal

Ticket UUID: 923174
Title: Can't catch loss of X server displaying slave toplevel
Type: Bug Version: obsolete: 8.3.4
Submitter: johnseal Created on: 2004-03-25 15:08:47
Subsystem: 67. Unix Window Operations Assigned To: nobody
Priority: 4 Severity: Minor
Status: Open Last Modified: 2026-09-23 19:14:16
Resolution: None Closed By: nobody
    Closed on:
Description:
If a Tcl app on one host is displaying a slave toplevel
on another host, and the remote host logs out (thus
yanking the X server out from under the window), then
the Tcl app dies with XIO error 131 (Connection reset
by peer).  Any WM_DELETE_WINDOW protocol or <Configure>
binding on the window does not fire.  There does not
seem to be any way in Tcl to catch and handle this
event.  I happened to be using ActiveTcl 8.3.4.1, but I
think it's an intrinisic Xlib problem that would affect
any version.

This was discussed on c.l.t and in the Tcl chatroom.
User Comments: serhiy.storchaka added on 2026-09-23 19:14:16:

Both cases still happen in 9.1b1 on Linux: with a toplevel on -screen :96, killing that X server kills the application, and it does so also after the toplevel was destroyed, as sbron reported.

I tried the second case: the last Tk_DestroyWindow() on a display which no Tk application uses schedules a check at idle time, and that check closes the display. It has to be deferred, because the code after it still uses the display — the order dependency which kept the old NOT_YET branch disabled. With it the application survives the death of :96, and the test suite gives the same 28 failures as without it.

It is not enough though. If a window is created on that display again after it was closed, a frame works, but a label fails on the third round:

X Error of failed request:  BadLength
  Major opcode of failed request:  51 (X_SetFontPath)

so the font layer keeps a reference to the closed display. TkCloseDisplay() cleans the clipboard, atoms, error handlers and GCs, but not borders, bitmaps, colors, cursors and fonts, and it says so itself: "There is more to clean up, we leave it at this for the time being." That cleanup is needed before a display can be closed while the application keeps running, so I am not proposing the change yet.

The first case is untouched: libX11 1.7 added XSetIOErrorExitHandler(), which would allow an orderly shutdown instead of Xlib calling exit(), but surviving the loss needs unwinding out of an arbitrary Xlib call.


chw added on 2023-12-10 17:29:31:
Before trying to even modestly fix this issue I suggest for Schelte's
CloudTk setup to exercise the XCloseDisplay() code paths in a multi
threaded scenario. Rationale: the display cleanup happens in a thread
exit handler, since displays are managed per thread. So a Tcl thread
may drive one or more Tk applications with one ore more displays. Upon
proper tear down of the thread, the thread exit handler performs the
XCloseDisplay() on all related display connections. Only if this works
reliably (i.e. by not taking down all other threads) it should be
considered to reform the management of multiple display connections.

sbron added on 2023-12-10 15:39:41:

Nearly 20 years later this has become relevant again with CloudTk. I found that even if all windows on a remote- or virtual X server have already been destroyed before the X server gets terminated, the Tcl app still dies with an XIO error:

XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":1"
      after 42 requests (41 known processed) with 0 events remaining.
It seems that once a display has been used, Tk never closes the connection to that display until the application ends. However, if the X server terminates earlier than the application, DisplayCheckProc() is invoked, which in turn calls XFlush(). But because XFlush() encounters EOF on the connection, it throws the XIO error.

I am hopeful that if Tk would be changed to close a display connection when the last widget is destroyed, the original problem may also go away. Because, according to Xlib manual chapter 2.6, when the X server’s connection to a client is closed either by an explicit call to XCloseDisplay or by a process that exits, it destroys all windows created by the client (among other things).

I readily admit that may very well be wishful thinking. But even if the situation of terminating an X server that has Tk windows on it would still be problematic, fixing the case where the windows have been cleanly destroyed before the X server is taken offline would be useful for CloudTk.

I have taken the liberty to reassign this ticket back to "nobody", because I'm doubtless that jenglish isn't going to work on it anymore.


hobbs added on 2005-12-01 14:44:17:
Logged In: YES 
user_id=72656

Is WM_SAVE_YOURSELF something the user can use here?

jenglish added on 2004-03-25 23:14:45:
Logged In: YES 
user_id=68433

Notes: 

The root cause of the problem is that XSetFatalErrorHandler
is Broken As Designed: the fatal error handler (which is
called when the server connection is closed unexpectedly) is
not allowed to return; and if it does, Xlib exit()s anyway.

It *might* be possible to detect the loss of the server
connection in the notifier prior to calling XNextEvent. 
Need to investigate further how to clean up the resources
associated with the display if this occurs.  I suspect it
will be extraordinarily tricky, due to the complexity of
Tk's shutdown logic.