| Ticket UUID: | f85877eac26cf26521b764cd217f622c3bd4ae2b | |||
| Title: | Erratic window ordering in all versions of Tk on all platforms | |||
| Type: | Support | Version: | ||
| Submitter: | marc_culler | Created on: | 2026-11-03 22:13:56 | |
| Subsystem: | 65. Generic Window Operations | Assigned To: | marc_culler | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Pending | Last Modified: | 2026-11-18 15:04:49 | |
| Resolution: | Remind | Closed By: | nobody | |
| Closed on: | ||||
| Description: |
When a new toplevel is created it is moved to the top of the stacking order and assigned as the key window (the one which receives key events). However, Consider these two scripts: fastwin1.tcl
fastwin2.tcl
If you run the first script on the current tip of main, you see:
So the order of the windows is reversed from what it should be (except for the root). This is not a failure to of the wm stackorder command; if you look at the windows you see that they are reversed. The result of running the second script is:
which is what we expect. So when windows are created in rapid succession the Apple AppKit may ignore calls to makeKeyAndOrderFront and place the new windows on (or near) the bottom of the stack. But if we wait for each window to actually become visible, things work better (with the new code for generating VisibilityNotification events from the fix for [c878c896b3]). Our wm-stackorder tests assume that windows will be stacked in their creation order by default. The fact that this does not always happen causes failures of those tests. Using tkwait visibility in the tests helps quite a bit. But failures still occur, such as:
where the first newly created toplevel .t ends up below the root window, even after waiting for it to become visible. | |||
| User Comments: |
marc_culler (claiming to be Marc Culler) added on 2026-11-18 15:04:49:
Hi Erik, I was hoping to hear from people who have been around longer than I have about why this was done back in 1998 and what the correct policy should be regarding nested event loops, especially in the core generic code. Feel free to bump the category and severity levels if you think they are wrong. Maybe it will get someone's attention. erikleunissen added on 2026-11-18 14:02:29: Oh, and I now see that this ticket's type is "Support". @Marc: don't you think this is a bug? erikleunissen added on 2026-11-18 13:54:25: Two remarks/questions:
* Does the issue of the reversed toplevel order exhibit only at the
script level? Or are users who create a sequence of toplevels using
the Tk C-library (Tk_CreateWindow) also affected?
In the latter case, addressing the issue at the script level by
requiring that users insert "update idletasks" after "toplevel"
when needed is no solution for users of the C-library.
* I'm a bit sorry for this issue to have made it only to "Minor" in the
severity rankings. Offending algorithmic principles[1] and having its
corollaries exposed to the user/programmer deserves better.
[1] reversing the processing order behind the user's back.
marc_culler (claiming to be Marc Culler) added on 2026-11-15 22:51:25: OK. But there is nothing specific to macOS here. This is generic code. serhiy.storchaka added on 2026-11-15 21:56:02: I don't know of a case where the nested event loop in MapFrame is needed. Removing it may be the better fix, but I have no Mac to test it, so I leave this to you. It may be worth checking frame-3.9: in my branch an embedded toplevel (-use) had to be mapped at once, so that its container gets its size from it. marc_culler (claiming to be Marc Culler) added on 2026-11-15 20:35:53: Thanks Serhiy. I think there is an alternative way to fix MapFrame, which is to not create a nested event loop at all. That would have the advantage of removing one more nested event loop, which is something that I thought you were in favor of. The comments in MapFrame suggest that the nested event loop is good because it solves the problem that a new toplevel might get displayed before it has been configured. But I have no evidence of that being a real problem which is solved by the nested event loop in MapFrame. Do you have any? That nested event loop in MapFrame goes back to 1998. I wrote a long message to the core list about it, and how it causes the bug that windows can be displayed out of order. I was hoping to get some context about why the nested event loop was created in 1998 and whether people feel that it is undesirable to have a nested event loop there. There was very little reponse - Erik Leunissen agreed with me that it is a bug, but that was about it. serhiy.storchaka added on 2026-11-15 19:01:41: You are right, it is not Aqua specific: on X11 without a window manager, five toplevels created in a loop come out as .t5 .t4 .t3 .t2 .t1 . in wm stackorder, for the reason you explained. I agree that update idletasks in the setup is the right fix for the tests. It could also be fixed in MapFrame(). Proposed fix in branch mapframe-creation-order: a MapFrame() called from the nested event loop of another one only queues its frame, and the outer one maps the queued frames in the order of their creation, each after processing the idle events as before. An embedded toplevel is still mapped at once, so that its container gets its size from it before it is mapped (frame-3.9). With it, the update idletasks after every toplevel in the wm-stackorder tests is not needed: one after the last toplevel is enough, and only in the tests which query the stacking order before the event loop runs. raiseDelay after raise, lower, iconify and withdraw is still needed. marc_culler (claiming to be Marc Culler) added on 2026-11-15 02:42:31: Hi Serhiy,
No, it is definitely not Aqua-specific and it is not a hack.
Windows created in rapid succession appear in reverse order on all
platforms. This is explained below in my comment from 2026-11-06.
Adding update idletasks after the call to toplevel prevents the
nested event loops described in that comment on all platforms, and
makes the wm-stackorder tests pass on all platforms.
The nested event loops are created in generic code. All platforms
have them, as explained in my 2026-11-06 comment. And it was done
intentionally (but is quite possibly misguided).
The hack is the previous attempt to correct this with raiseDelay.
That was a nefarious hack, It was working for a while. Then, for a
while, it worked only on CI runners and failed most of the time on
real hardware. Then it stopped working on CI runners too.
The failure almost always included the first item in the list of
stackorders, which only reports the order of the windows after
the setup phase. Of course, if the initial order is wrong
that can cause later orders to be wrong as well.
The fact that the hack worked better on Windows and linux than on
macOS could be regarded as Aqua specific. But it is really just that
there are some differences between platforms with respect to the
exact timong of when the Tk event loop is entered before, during,
and after the creation of a toplevel. Anything that causes a call
to Tcl_DoOneEvent between the creation of two toplevels will fix
the reverse ordering problem.
This also explains why the tests started failing on macOS. As you
know and have pointed out there are several places in the macOS port
where loops like
while Tcl_DoOneEvent(TCL_IDLE_EVENTS)) {}
have been added to "fix" various bugs. There is a consensus that
such nested event loops should never be needed, especially in
platform specific code. (There is a loop like that in the generic
TkCreateFrame function which causes rhe reverse ordering. But I did
not get any response to my query about whether that should be there,
and I take that to mean that there is no consensus about this for
generic code.) Anyway, we have been working for a while to eliminate
those loops from the macOS code, and in retrospect it is clear that
one of the loops that we removed was responsible for making the
wm-stackorder tests pass with the raiseDelay hack.
That does not make adding the addition of update idletasks after a call
to toplevel into an Aqua specific hack. It is needed on all platforms
to guarantee that windows created in rapid succession will appear in
the expected order, and it should probably be documented as being
needed.
PS According to the comments, raiseDelay was intended to wait for
a raise operation to finish. But in fact it was also added in the
setup sections to try to "fix" out of order windows.
serhiy.storchaka added on 2026-11-13 10:05:17: I have no expertise in this area, but the use of idle tasks looks to me like an Aqua-specific hack. However, these tests are about raise, wm stackorder, etc. If the idle tasks are only processed in the setup, they do not affect the purpose of the tests. I think it is appropriate to use them in the setup. marc_culler (claiming to be Marc Culler) added on 2026-11-12 15:01:41: Thank you, Serhiy. Yes, that works fine on macOS. I made that change in 9.1 and 9.0. No one responded to my question about whether idle tasks which create nested event loops are appropriate. So I am changing the status to Pending and the resolution to Remind. Maybe someone will think about this someday. serhiy.storchaka added on 2026-11-06 23:09:54: I tested branch [f314ea003c|stackorder_tests] on Windows and under 9 window managers on X11. The only new failure is wm-stackorder-5.1 under Fluxbox: wm stackorder . returns . .t instead of .t .. It is caused by the update idletasks after toplevel .t: the menubar is now added to a toplevel that is already mapped. Under Fluxbox raise . then has no effect, and the same happens with update instead of update idletasks. It works if the menubar is added before the toplevel is mapped, so the test passes here with a single update idletasks after .t configure -menu .t.m. Does that still work on macOS? marc_culler (claiming to be Marc Culler) added on 2026-11-06 18:44:49: It turns out that this is not specific to macOS and that it happens by design. Here is the explanation, copied from my email to the core list which refers to the following script:
Before calling Tk_MapWindow, MapFrame runs a nested event loop to process idle tasks, purportedly to make sure that a window is configured before it is mapped. The nested event loop looks like this:
When the script is run, the for loop is executed before any idle tasks get processed. When the for loop ends, the idle task queue contains MapFrame tasks for {. .t1 .t2 .t3 .t4 .t5} in that order. When the first MapFrame runs for the root window, before it maps the root window, it runs a nested event loop which processes the MapFrame task for .t1. That runs its nested event loop which processes the MapFrame for .t2, and so on. Finally the MapFrame runs for .t5, which then maps .t5 and allows the nested event loops to unwind. So the toplevels get mapped in the order {.t5 .t4 .t3 .t2 .t1 .}. That is the exact reverse of what the wm-stackorder tests assume.
marc_culler (claiming to be Marc Culler) added on 2026-11-04 00:16:02: A follow-up comment: this behavior depends on the macOS release version. On macOS 26 I see sporadic failures of wm-stackorder tests. On macOS 27 I do not. But the window ordering on macOS 27 is more erratic than what I saw on macOS 26, shown in the ticket. On macOS 27 the two scripts produce:
That is the reverse order as before, but with tkwait visibility I see:
so the first two windows appear in what seems random.
| |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki