Tk Source Code

View Ticket
Login
Ticket UUID: e0c7091f2bcf7c9e2916ec8b7a81833e0da7d6e4
Title: Hang handling window resize in display update in callback from another thread
Type: Bug Version: 8.6.12 Aqua
Submitter: jal_frezie Created on: 2022-09-08 16:15:44
Subsystem: 66. Aqua Window Operations Assigned To: nobody
Priority: 5 Medium Severity: Severe
Status: Open Last Modified: 2022-09-25 04:32:17
Resolution: None Closed By: nobody
    Closed on:
Description:
My application uses a worker thread to get calculations done. This is called -async from the master thread which vwaits for the result, so user interaction can be handled in an 'update' in a master procedure that is called periodically from the worker thread.

The problem is, if a window resize is handled in such a callback, a hang occurs. The console and window become unresponsive, the process uses about 120% CPU and rapidly chews up all the system memory.

The two attached files allow the problem to be demonstrated. Put them in the working directory and go 'wish threadbare.tcl'. Resize the toplevel Tk window while the count is running. If a resize is handled in the callback update, it will hang.

The same code allows the toplevel to be resized during the count without hanging in Linux.
User Comments: chrstphrchvz added on 2022-09-25 04:32:17:

I'm not aware what is causing this issue. I had tried the reporter's example and found that program gets stuck in while (Tcl_ServiceEvent(TCL_WINDOW_EVENTS|TCL_DONT_WAIT)) {} during generateExposeEvents:, so the toplevel resize never finishes. Something I don't understand is why ThreadEventProc() ignores flags and so will do ThreadSendEval() and eval a script (such as one that does update at a seemingly inopportune time) even for TCL_WINDOW_EVENTS|TCL_DONT_WAIT.

According to the heap command the memory is getting used up by NSEvent and NSMutableArray objects, but I'm not aware what for.

I don't know very much about AppKit, but at the moment I'm not aware of an alternative to live resizing which would work for Tk, and so for cgimage-drawing I have only looked for what Tk might do differently during live resizing.


marc_culler (claiming to be Marc Culler) added on 2022-09-24 05:14:37:
I agree that this is a serious bug.  One resolution might be that if we
switch to using Christopher Chavez's cgimage drawing we will no longer
need to use Apple's LiveResize mode.

jal_frezie added on 2022-09-23 07:47:14:
Good point -- I wrote the code like that in order to handle the situation where the main thread is also busy all the time doing graphical operations in callbacks from the worker thread. I've now fixed it by having the worker thread pause after a series of continuous callbacks to allow the main thread to process the updates in its own event loop. But I think this is still a severe bug because it can hang your whole system.

marc_culler (claiming to be Marc Culler) added on 2022-09-22 18:57:46:
I said "I see" but I don't.  Your worker thread in the example appears to
be what one would use if one were not using threads at all.  If you were
doing a long computation in the main thread you might pause every so often
to call update so that window events get processed and the UI does not
freeze.  But if you are doing a long computation is a separate thread
then the main thread should continue to process window events while the
worker thread computed.  Perhaps the worker would change a variable or
update some data to show its progress, but I can't see why it would
ever need to pause its computation to tell the main thread to run the
update command.

marc_culler (claiming to be Marc Culler) added on 2022-09-21 18:33:10:
I see.

That looks pretty tricky to me.

For macOS, when a resize is in progress the Apple window manager enters
"liveResize" mode. Currently we do not remove any events from the Apple
event queue when in liveResize mode.  This means that the Tcl "event source"
goes dry.  I don't *know* why that would cause a hang with high CPU usage,
but I am guessing that the Threads package is causing Tcl to call select,
which in the mac port involves a spinlock, and somehow the process of
entering liveResize mode causes Tcl not to be able to obtain the lock.
I am guessing that the CPU cycles are being consumed by the spinlock.  I
would not be too surprised to find that the authors of the Threads package
made assumptions which are always valid on linux but perhaps not always
valid on macOS.  Also, the macOS Tcl event processing was changed, and
that change may have occurred after the Threads project moved on to version
3 for Tcl 9.  But this is all speculation.  I will look at the Threads
code and see if I can figure anything out.

jal_frezie added on 2022-09-21 17:35:43:
That is correct, but if 'update' is replaced with 'update idletasks', no user interaction is handled within the window (which is the documented behaviour)

marc_culler (claiming to be Marc Culler) added on 2022-09-21 17:11:35:
If you replace "update" by "{update idletasks}" in worker.tcl:

proc churn {count} {
    for {set j 0} {$j < $count} {incr j} {
        set p [expr {pow($j, 0.7)}]
        if {$j==$count/2} {
            thread::send $::masterId {update idletasks}
        }
    }
    return $p
}

then there is no hang.  The count is not updated during the resize but
otherwise it seems to work.  (I was testing with 8.7 but I don't think
that should matter.)

Attachments: