| Ticket UUID: | e050c5a2a920caa4a2ecd06e197be6106ccb41f2 | |||
| Title: | Aqua: drawing hangs if idle handlers keep scheduling idle handlers | |||
| Type: | Bug | Version: | ||
| Submitter: | serhiy.storchaka | Created on: | 2026-10-02 06:35:01 | |
| Subsystem: | 66. Aqua Window Operations | Assigned To: | aku | |
| Priority: | 5 Medium | Severity: | Important | |
| Status: | Open | Last Modified: | 2026-10-03 02:25:02 | |
| Resolution: | None | Closed By: | nobody | |
| Closed on: | ||||
| Description: |
The macOS code processes idle handlers until there are none left, in a loop like this:
while (Tcl_DoOneEvent(TCL_IDLE_EVENTS)) {}
If an idle handler schedules a new idle handler every time, directly or through the event handlers it invokes, such a loop never ends and the application hangs. On other platforms the same script only makes [update] and [update idletasks] hang, which also loop until there are no events left. Reproducer (expected: the window can be iconified, deiconified, resized and the menu opened, and the counter keeps growing; on macOS it is expected to hang):
proc p {} {incr ::n; after idle p}
set n 0
after idle p
pack [label .l -textvariable n -width 20]
pack [button .b -text Quit -command exit]
menu .m
menu .m.file
.m add cascade -label File -menu .m.file
.m.file add command -label Quit -command exit
. configure -menu .m
after 500 {wm iconify .}
after 1000 {wm deiconify .}
In 8.6 the application should hang at the first redraw; in 9.0 and 9.1 at [wm deiconify] (TkpWmSetState), when resizing the window live, or while the File menu is open. Another example is the fix for [1813595] backported to 8.6, which made canvas-24.1 hang on macOS: the canvas redisplay, an idle handler, scheduled itself again while Enter and Leave bindings moved the current item away from the pointer and back. That fix was backed out of 8.6. Such loops are in:
Proposed fix in branch [aqua-bounded-idle-loops] (for main; not tested on macOS): a new function TkMacOSXProcessIdleEvents() does at most 10 passes over the idle handlers (each pass runs all idle handlers which existed when it started, so display procedures scheduled by other display procedures still run), and is used for all these loops; _runBackgroundLoop is limited the same way. The remaining idle handlers run later from the event loop. 8.6 needs a separate patch. | |||
| User Comments: |
marc_culler (claiming to be Marc Culler) added on 2026-10-03 02:25:02:
I think it is complicated. Ideally, I think it should never be necessary to run one of those nested Tcl_DoOneEvent loops. Of course, there needs to be one of those loops, which handles all types of events, as well as idle tasks and timer tasks, and that one is the so-called mainloop. But I think the nested loops should be superfluous. However, every attempt to remove one of the remaining nested loops has broken something. And it seems that the breakage always occurs only in highly complex code which can not be easily analyzed to determine the precise cause of the failure. (A typical example is Nicolas Bats' proprietary app, which will develop symptoms like: "The main window is configured while iconified and then deiconified when the layout has finished. Without that nested loop the layout process becomes visible and produces annoying artifacts." The infinite loops may be a different story, though. I could imagine having a dedicated function, as in your proposal, for draining the Tk event queue and/or idle queue in a smarter way than the simple while() loop to ensure that no recursion occurs. That might need to be part of Tcl, rather than Tk, and might involve extending some of the Tcl structs that are used by the Notifier. serhiy.storchaka added on 2026-10-02 18:14:14: I think the original fix for [1813595] was not wrong. It fixed that issue, but the hang can return if the application calls update idletasks (or the Aqua code runs an equivalent loop). That is a different issue, this one. serhiy.storchaka added on 2026-10-02 18:10:45: I agree that this exceeds Claude's capabilities: the proposed fix only kicked the can down the road. It looks to me that there still is a problem. Do you agree? Do you suggest closing the issue, or do you have any ideas how to solve the problem? marc_culler (claiming to be Marc Culler) added on 2026-10-02 17:40:01: I am not sure it makes sense to try to redesign Aqua Tk at this depth without having any access to a macOS computer. I think you may be putting too much trust in Claude's analytic capabilities when you do that. marc_culler (claiming to be Marc Culler) added on 2026-10-02 17:36:32: _runBackgroundLoop is also a completely different animal. That exists to allow timer tasks to get run while a menu is open. Tk does not process any Apple events while a menu is open. Apple manages the menu in a separate runloop. When a menu first opens, Aqua Tk starts a thread which processes timer tasks (only). Before that thread existed, for example, if you started the pendulum demo and then opened a menu the pendulum would stop. With the separate thread running timer tasks, the pendulum continues to swing while a menu is open. marc_culler (claiming to be Marc Culler) added on 2026-10-02 17:30:13: LiveResize is a completely different animal. I think that should be treated as an isolated issue. Getting it to work reliably (e.g with the "A text widget with embedded windows and other features" demo) was a big struggle. marc_culler (claiming to be Marc Culler) added on 2026-10-02 17:25:32: When I run this code interactivley in wish9.1 a window opens
with disabled titlebar buttons, so it cannot be iconified or closed,
and the counter increases steadily. The window can be moved with the
mouse. The Quit button and the menus are not responsive. I never see
the window getting iconified so it is not possible to deiconify it.
When I run it as a script with wish9.1, no window appears at all. The
menus appear in the menubar, but they are not responsive.
(Iconifying and deiconifying windows on macOS are time-consuming
operations because of the animations which make the window swoop into and
out of the dock. I am not sure if that animation can be done in half
of a second, or what Apple does if a window is deminiaturized while its
miniaturization animation is in progress.)
If I modify your code by changing line 3:
after idle p -> after 10000 p
and run it as a script I see the window swoop into the dock, and then
swoop out of the dock. The Quit button works, as does the Quit menu.
The counter remains at 0
If I run that modified code interactively by pasting the code into
the terminal, the window gets iconified, but does not get deiconified.
I can click the dock icon to deiconify it. That produces an error dialog
that says:
`ambiguous command name "p": pack package panedwindow pid place proc puts pwd
while executing
"p"
("after" script)`
Since waiting 10 seconds makes such a huge difference I think the weird
behaviors are primarily related to Tk initialization. Drastic things are
happening before Apple is ready for them
In any case, all of the behaviors I see are quite different from your
predictions.
serhiy.storchaka added on 2026-10-02 17:04:44: I have no macOS and cannot check it myself. Could you please run the reproducer from the description on macOS with 9.0 or main and check whether it hangs: after wm deiconify, when resizing the window live, or while the File menu is open? 9.x has similar loops (in updateLayer, generateExposeEvents: during a live resize, TkpWmSetState() and _runBackgroundLoop), so I suspect there may be the same issue there. Filtering like this is also already done for idle handlers: every pass runs only the handlers which existed before it. The hang may be in the loop which repeats these passes. A complete redraw can need many passes: on X11 it is about 2 passes per level of nested geometry managers (13 for 5 levels, 83 for 40). So the limit should be large, and I do not know whether it is enough on macOS. I have no macOS to experiment. For 8.6, I agree. marc_culler added on 2026-10-02 16:28:52: Let's focus on generateExposeEvents in Tk 8.6.
The loop in generateExposeEvents is a core feature of Tk 8.6, dating to
the original Aqua port. It was a way of arranging for redraws of damaged
parts of a window which was forced the strange limitations of drawRect.
Now (meaning in 9.X) drawRect is no longer used, and that code has been
removed from Tk entirely.
That function is (ab)using Expose events in a special way, completely
separate from how X11 expects Expose events to be used. It generates a
bunch of Expose events which are processed immediately, with an explicit
guard to prevent any other event from being processed. Every Expose event
generated in that function is given the same serial number, not used by
any other events, and it then processes events in a nested event loop
that uses Tk_RestrictEvents to ensure that only events with that specific
serial number get processed. The comment says:
/*
* Use the ExposeRestrictProc to process only the expose events. This
* will create idle drawing tasks, which we handle before we return.
*/
The issue that you are raising applies to the step where those idle drawing
tasks are processed. That happens in a Tcl_DoOneEvent loop that restricts
to idle tasks.
If that loop were removed from 8.6, all windows would be black. And, in fact,
they were all black when macOS 10.14 (Mojave) was released. It was a major
effort to recover from that, which involved re-organizing that code among
many other things.
The use of drawRect, and the many side effects of that choice, such as
the way generateExposeEvents works, was a design error. Tk 9.0 provided
an opportunity to correct that design error, and luckily Christopher Chavez
had already written a working prototype for an alternative based on
updateLayer instead of drawRect.
I don't know of any way to "fix" this aspect of Tk 8.6 for Aqua, short of a
complete redesign such as was already done for 9.0. And that would be a
complete waste of time since 8.6 is obsolete and should disappear as soon
as possible. If the hangs can be avoided by using the chatbots' favorite
trick of adding a loop counter with an arbitrary max value, like 10, and
breaking from the loop when the counter hits the threshold, then so be it.
We only need to keep 8.6 functional until it goes out of use. Whatever
hacks are needed to keep it working until then are fine with me.
erikleunissen added on 2026-10-02 09:30:56: Regarding this issue and the commit message for [a0eb4e7a8c]:
"Aqua: do not hang if idle handlers keep scheduling idle handlers."
Ticket [591829e948], part A, questions the practice of "idle handlers keep scheduling idle handlers"
more fundamentally; at least for one of the instances that the present ticket also addresses.
It shows for one of the entry points where such iterative or recursive processing
of idle tasks is induced, that the iterative or recursive approach is not necessary.
Branch bug-591829e948-A addresses the problem at a higher level in the call graph, thus
disentangling the needlessly intertwined code, resulting in elimination of that
case (i.e. that particular entry into recursive processing of idle tasks).
Lack of overview over the involved call sequences has probably lead to the current
intertwined state of the code. I wouldn't be surprised that simplification
of the call sequences, like in branch bug-591829e948-A would prove to be a solution
for the present ticket also.
| |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki