Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 57da73926c11dfbdf205e394a976ad02690a66a451f8175ead7f0295e3a9e4d1
Ticket: a58737f57bf740979e0ed091118508188b8c28f5
The macOS implementation of Tcl_WaitForEvent is incorrect.
User & Date: marc_culler on 2020-07-10 22:08:56
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    The platform specific function Tcl_WaitForEvent (in tclMacOSXNotify.c for
    macosx) is called from Tcl_DoOneEvent, the core of the Tcl event loop.  It
    gets called in Tcl_DoOneEvent after first checking that there are no
    asynchronous events and no window events queued. Before calling Tcl_WaitForEvent
    the setup proc for each available event source is called. Then the check proc
    is called for each event source.  The main job of the setup proc is to set the
    timeout value which will be passed to Tcl_WaitForEvent.  On macOS if there
    are any events in the Apple window event queue then the timeout is set to 0,
    meaning that Tcl_WaitForEvent should check for file events and return
    immediately with a positive return value if any file events have been
    recorded by an auxiliary notification thread which runs select in the
    background and records any active files descriptors in a thread specific
    data structure.  If the Apple window event queue is empty then the timeout
    will be non-zero and Tcl_WaitForEvent will wait until the timeout expires.
    
    In actual fact, however, the original Cocoa implementation of Tcl_WaitForEvent
    did not return immediately when the timeout was set to 0.  Instead, before
    polling for file events, the following loop was run:
    
     while (Tcl_ServiceAll() && tsdPtr->waitTime == 0) {}
    
    This code predates the release of Cocoa and was probably following the
    instructions in the Tcl_ServiceAll man page regarding how to embed Tcl in
    an application which has its own private event loop. There are two issues
    here.  First, the man page does not recommend calling Tcl_ServiceAll in a loop.
    Doing that would appear to run the risk of a hang, in cases where servicing
    one event causes a new event to be queued. Indeed, there were many reports
    of macOS hanging and Kevin Walzer found that calling Tcl_ServiceAll once,
    not in a loop, reduced the incidence of hangs.  So, in commit [c699f98468]
    in 2015, Kevin removed the loop and replaced it by a single call with
    noticeable improvement in the behavior.
    
    The second issue is more subtle.  While it was probably true in the Carbon
    implementation of Tcl that Tcl actually was being embedded in an application
    with a private event loop, I think that ceased to be the case with the release
    of Cocoa.  It is true that the NSApplication object has a private event loop
    which can be started by calling the function run().  But Tcl never calls that
    function. The run() method handles dispatching of Apple NSEvents to AppKit
    objects such as NSView and NSWindow.  But that was not helpful for Tk since
    what was needed was to translate NSEvents into XEvents and add them to the
    Tcl queue so Tk could process them.  Instead of running Apple's private event
    loop, the Cocoa implementation of Tk emulates the Apple run() loop within the
    check proc for the Aqua event source.  The check proc removes NSEvents from
    the Aqua queue manually and translates them into XEvents before calling
    NSApplication sendEvent to allow the NSEvents to be processed by the window
    manager.  The adaptation of TclWaitForEvent to account for this change was
    never done.
    
    While Kevin's change in 2015 reduced the number of hangs observed in Tk
    considerably, they still persisted.  Moreover, the design of Tcl_WaitForEvent
    caused a lot of randomness in when the [NSView drawRect] method was being
    called.  After Christopher Chavez noticed that the drawRect method only gets
    run during calls to CFRunLoopinMode or NSNextEventMatchingMask (used in the
    setup proc and the check proc) I was eventually led to study what was actually
    happening in Tcl_WaitForEvent.  (With the release of OSX 10.14 (Mojave) it became
    critical to control when drawRect gets called because Apple made changes for
    that release which caused it to be the case that graphics calls which were not
    made from within drawRect were ignored; those calls would have no effect on the
    appearance of any application windows, causing Tk to do nothing visible other
    than display empty black windows.) 
    
    My conclusion was that TclServiceAll should never be called in TclWaitForEvent.
    This is consistent with the fact that Tcl is really not being embedded in an
    application with its own private event loop.
    
    I have made this change to tclMacOSXNotify.c in the branch named macOS_hangs.
    I have tested extensively both with regression tests and real applications.
    
    There are no noticeable bad effects in Tcl.  All regression tests pass.  The
    branch also makes some unrelated changes having to do with the fact that the
    NSSpinLock was deprecated by Apple with the release of OSX 10.15 (Catalina) and
    replaced by the os_unfair_lock.  The additional changes allow the use of
    os_unfair_lock in place of NSSpinLock on systems which support this more efficient
    new lock type.
    
    This change to Tcl, together with changes to how [NSView drawRect] is handled
    in Tk, led to significant improvements in Tk.  Some regression test failures
    disappeared including those reported in [06d8246baf] and [37a27ddb2] and
    [aedc9f6fa4] as did many graphical artifacts which were reported for example
    in tickets [2a61eca3a].  These changes are in the Tk branch idle_curiosity.
    
    I have extensively tested the combination of idle_curiosity and macOS_hangs
    for several months as has Nicolas Bats and, I believe, others.  I would like
    to see both of these merged into the core_8_6_branches before the release of
    8.6.11.  I would be very grateful for reviews, comments and testing.
    
  5. foundin changed to: "10.6.10"
  6. is_private changed to: "0"
  7. login: "marc_culler"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "4a267845b96d18ced148980dad8c23b791785018"
  10. resolution changed to: "None"
  11. severity changed to: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "marc_culler"
  14. subsystem changed to: "02. Event Loops"
  15. title changed to:
    The macOS implementation of Tcl_WaitForEvent is incorrect.
    
  16. type changed to: "Bug"