Tk Source Code

View Ticket
Login
Ticket UUID: 7b8d4e4b8e871d23005fa5b7bb6c45b537695935
Title: `tkwait visibility .` loop forever in macOS if `winfo visible .` == 1
Type: Patch Version: 8.6
Submitter: carenas Created on: 2021-08-04 10:52:05
Subsystem: 66. Aqua Window Operations Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2021-10-20 13:35:07
Resolution: None Closed By: nobody
    Closed on:
Description:
when a root window is created, starting with 8.6.11, it is marked as "visible" (unlike what was observed with 8.5 or older versions of 8.6), and so if the application waits for visibility after, will wait forever as shown in the following report from gitk :

https://lore.kernel.org/git/CAPx1Gvdp85H_EY9qLY3jkxktXCDAwJyznMWfDDGHy56db7sc6Q@mail.gmail.com/T/#md7cc1a563dbe9083cfa6896c92dc88100585798d

an event could be triggered by withdraw/deiconify or the code could be made to skip the wait if the window is already visible (as coded in the workaround and documented in wiki), but it would be less racy if done inside Tk as shown by the proposed fix.
User Comments: marc_culler (claiming to be Marc Culler) added on 2021-10-20 13:35:07:
A bit of poking around in xlib reveals that a window's visibility state (or
obscurity state, as it is called in the bind manual page) can take one of three
values: VisibilityUnobscured, VisibilityPartiallyObscured, or VisibilityFullyObscured.

The bind manual page says:
  "Visibility events are generated whenever a window's obscurity state changes;
  the state field (%s) specifies the new state."

The macOS and Windows ports do not come anywhere close to doing this.  In
win/tkWinWindow.c it says:
  "Note that we don't properly report the visibility state, since Windows does
  not provide that info."
The macOS port simply copied its code for generating VisibilityNotify events
from Windows.

So it was a conscious (but undocumented) decision not to simulate X11's visibility
notifications on these platforms.  There was a half-hearted attempt to support
the tkwait visibility command without supporting the notificatons.

I guess the question is whether simulating X11 visibility notifications would be
worth the effort.

marc_culler (claiming to be Marc Culler) added on 2021-10-19 21:25:58:
Well, I looked at the code.  In Win and macOS there are static functions
NotifyVisibility which generate the VisibilityNotify events.  These two
functions are identical.  They are passed a TkWindow pointer.  They
recursively send VisibilityNotify events to the TkWindow which is passed
as a parameter and to all of its children.  There is no attempt to determine
whether any one of these windows is covered by any other window, and a
sibling window will never be sent a VisibilityNotify event.  Also, all of
these events have:
  event.xvisibility.state = VisibilityUnobscured
The NotifyVisibility function is called by XMapWindow and is passed the
window which just got mapped.  It is not called by XUnmapWindow

I conclude that the author of this function did not interpret visibility to
have anything to do with whether a window is covered by another window.
Being visible is treated as equivalent to being mapped.  And the
notification is only send when a window becomes mapped, not when it becomes
unmapped.

The manual does not provide any definition of what "visible" means, and does
not mention anything about windows covering other windows.  I would point
out that it is not at all clear what "visible" should mean.  Suppose .f1
covers half of .f2.  Does that mean that .f2 is visible?  Does it mean that
it is not visible?  To me it is just as reasonable to say that .f2 is
visible as it is to say that .f2 is not visible.

At the very least, if a word like this is used in the manual then it should
also be defined.  But we should also think about what is useful.  Does window
.f1 need to be notified when window .f2 is placed so that it covers half, or
all of .f1?  And if so, for what purpose?

Of course it is implicit that an undefined term in the Tk manual means
whatever the X11 manual says that it means, in the case where the term
is defined for X11 or where the meaning can be deduced from the behavior
of X11.  I don't know what the defacto meaning of "visible" is for X11,
however.

For the use case described in the manual it would seem sufficient for the
notification to be sent when a window is mapped or unmapped.  But that
could not have been the intended purpose of the VisibilityNotify event
since there are also MapNotify and UnmapNotify events.

Maybe the best solution would be to eliminate the VisibilityNotify event
and to provide MapNotify and UnmapNotify events more consistently.

griffin added on 2021-08-26 19:19:56:
It has been my long-term experience that this command is inherently a race condition.  Consequently, I've avoided ever using it since it simply is not reliable.  As it turns out, in my experience, this operation is never really needed, there are better ways to build a GUI.

As pointed out, the proposed change, changes the (mis-guided) defined behavior of the original command.  I would much rather see new command(s) that define a more concrete, non-race, behavior.  For example [tkwait appear $w] and [tkwait withdrawn $w].  This avoids any compatibility issues.

fvogel added on 2021-08-26 17:30:11:
But the call to tkwait visibility DOES return on X11 (Linux Debian 10) when the blue square covers the red one.

Looks like we have found a new bug :-) !

marc_culler (claiming to be Marc Culler) added on 2021-08-26 15:13:07:
The call to tkwait visibility does not return on macOS when the blue square
covers the red one.

fvogel added on 2021-08-26 14:34:53:

> The manual indicates that the purpose of the command is to wait until a newly
> created window appears on the screen and specifically states that the command
> waits for a NotifyVisibility. So the manual would need to change if the
> proposed patch were used. To me this does not really seem like a change in the
> language, however. I wonder what others think. Francois? Jan?

My understanding is that the proposed change is to let 'tkwait visibility' return immediately if the window is already visible.

We all know how we love to be conservative and avoid changes that are not bug fixes in patch releases. In this specific case, my first opinion is we should make this change (along the lines of https://github.com/carenas/tk/compare/main...carenas:tkwait?diff=unified&expand=1) in 8.7 only, and here is why.

The current manual says:

If the first argument [to tkwait] is visibility (or any abbreviation of it) then the second argument is the name of a window and the tkwait command waits for a change in its visibility state (as indicated by the arrival of a VisibilityNotify event). This form is typically used to wait for a newly-created window to appear on the screen before taking some action.

So the manual explicitely says it waits for a *change* in the visibility state, and even it also explains the typical use case, one cannot exclude that some code somewhere waits for a window to become no longer visible. In such a use case the 'tkwait visibility' would no longer be useful. So yes, to me it is a change in the language, that cannot be made in a patch release in principle.

BUT: that said, the following script does NOT let 'tkwait visibility' return, contrary to what the manual stays (I have tried it on Windows only, confirmation still needed on Aqua and X11):

pack req Tk
frame .f1 -bg red -width 20 -height 20
place .f1 -relx 0.2 -rely 0.2
frame .f2 -bg blue -width 50 -height 50
bind .f1 <1> {place .f2 -relx 0.1 -rely 0.1}
tkwait visibility .f1
# and now click the red frame -> the blue one hides it -> visibility state of .f1 changed, but tkwait does not return!

As a conclusion, I think the patch should be applied to core-8-6-branch AND the manual be perhaps a bit more accurate.


marc_culler (claiming to be Marc Culler) added on 2021-08-05 11:52:08:

I see that the problem reported here was also reported in 2015 on the Tk wiki by Harald Oertmann. The suggestion on that page is to use

if {![window already visible]} { tkwait visibility $w }
which seems identical to what was done in gitk. (The wiki page also claims, inaccurately, that NotifyVisibility events are never generated in Aqua or Windows and that, consequently, the tkwait visibility command is only useful on linux.)

The manual indicates that the purpose of the command is to wait until a newly created window appears on the screen and specifically states that the command waits for a NotifyVisibility. So the manual would need to change if the proposed patch were used. To me this does not really seem like a change in the language, however. I wonder what others think. Francois? Jan?


carenas added on 2021-08-05 07:09:23:
the "race" was only theoretical, as it hasn't been observed and isn't likely with the current implementation based on your explanation, but the "visible" state could change in between the calls for `winfo visible` and `tkwait visibility` and it would seem safer and more correct to not even wait for notification events if the window is already visible anyway.

I know the original proposed code was ugly (even if it worked) but the following (with 2 commits) might be an alternative:

  https://github.com/carenas/tk/compare/main...carenas:tkwait?diff=unified&expand=1

marc_culler (claiming to be Marc Culler) added on 2021-08-04 15:53:30:
I think that what you are saying about "when a window is created" only
applies to the root window.  The XMapWindow function sends a
VisibilityNotification event when a window is mapped, with one exception.
The exception is the root window.  There is a reason for that exception.
It is there because the root window gets mapped by Tk very early in its
initialization process, before the Apple window manager is actually able
to instantiate an NSWindow.

The Tcl solution used in the gitk code seems extremely reasonable to me.
Can you provide any evidence for why it is "racy"?  What is the race
condition that you are worried about?

Attachments: