Tk Source Code

View Ticket
Login
Ticket UUID: 9b6065d1fd0d1179ea4acf4458b2ec61644b65fe
Title: Tk doesn't restore overwritten Tcl command "update"
Type: Bug Version: 8.6.11
Submitter: erikleunissen Created on: 2021-03-08 19:17:10
Subsystem: 99. Other Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2022-09-10 20:15:30
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2022-09-10 20:15:30
Description:
Observed:

> tclsh8.6
% update
% package require Tk
8.6.11
% destroy .
% update
can't invoke "update" command: application has been destroyed
%

Explanation of the observed misbehaviour:
- the Tcl command "update" is silently overwritten by a Tk command "update" upon [package require Tk].
- when Tk deletes the entire Tk command set upon [destroy .], it fails to restore the original Tcl command.
User Comments: jan.nijtmans added on 2021-03-29 06:34:52:

Nice catch!

This failure is not so difficult to check for. Done now. It means that for Tcl 8.6, if an application renames "update", then Tk cannot restore the old one it just will remove its own "update" command after service. So we are back to where we were in this (unlikely) case. At least nothing can crash any more.

I think this is OK for 8.6. But - indeed - for Tcl 8.7 there are still possibilities to do something better. Not high prio, but a nice to have.


griffin added on 2021-03-28 23:46:29:
Thank you Jan for merging the changes.  
I have found that this fix fails spectacularly when a script renames or redefines the update command. In this scenario, Tk cannot properly restore the old command.  

rename update tcl::update

proc update {args} {
    puts "Tracing update $args: [info level]"
    tcl::update {*}$args
}

after 300 {
    puts Hi-from-Tcl
    incr ::cont
}
set ::cont 0
while { ! $::cont} {
    update idle
    vwait ::cont
}

package require TK

# etc...


I do not see a way for Tk to handle properly restoring Tcl's update command as there is insufficient information to do so.  I think the only elegant way to handle this is if Tcl is involved in Tk's extension of update rather than having Tk redefining the command.

jan.nijtmans added on 2021-03-19 20:24:10:

Brian's fix is now committed to core-8-6-branch and up, so this ticket is resolved. I simplified it a little: Since it's sufficient to store the objCmdProc only, not the complete CmdInfo, we can save a ckalloc/free this way.

> Alternatively, provide a Tcl mechanism for Tk to install/uninstall a callback when Tcl's update is invoked, eliminating the need for Tk to have it's own "update" command.

I'll leave this ticket "pending", for further investigation.


jan.nijtmans added on 2021-03-17 08:38:42:

Since all testcases pass now in the bugfix branch, I suggest to merge it to core-8-6-branch. OK?

Any further work (removing Tk_UpdateCmd, some callback API) can then be continued in Tcl 8.7.

If there are no objections, I will do the merge in a few days (unless Brian wants to do that, it's actually Brian's work ....)


erikleunissen added on 2021-03-16 20:44:34:
My previous remark should not be interpreted as advocating the status quo where two quite different commands exist. I'm all for de-duplication and unification as a matter of principle.

erikleunissen added on 2021-03-16 19:31:43:
There a few (remarkable) differences w.r.t. Tcl_UpdateObjCmd() and Tk_UpdateObjCmd() that need consideration:

A. Tk_UpdateObjCmd() has an extra while loop that wraps around the loop that Tcl_UpdateObjCmd() also has. I cannot judge the importance of that.

B. Tcl_UpdateObjCmd() passes the flag TCL_WINDOWS_EVENTS on to Tcl_DoOneEvent() and Tk_UpdateObjCmd() doesn't.

I believe that Tcl_UpdateObjCmd() does so erroneously, and reported it as a bug in:

    https://core.tcl-lang.org/tcl/tktview?name=0221b993a1

griffin added on 2021-03-15 20:28:08:
I've checked in a fix and test case for this ticket: [87c23bfe]  The fix restores Tcl's version of the update command.  
The test confirms that update still works after "destroy .", presuming that Tcl's update has been restored.

An alternative to this fix would be to just leave Tk's update in place. It should continue to function correctly after "destroy ."

Alternatively, provide a Tcl mechanism for Tk to install/uninstall a callback when Tcl's update is invoked, eliminating the need for Tk to have it's own "update" command.

erikleunissen added on 2021-03-13 15:19:50:
Not part of the bug, but related:

Now that it is evident that Tk changes the update command after [package require], shouldn't the Tk test suite include tests regarding the replacement command in order to determine correct functioning?

griffin added on 2021-03-09 01:27:30:
The cause of this issue is the same as the older ticket, 
but the point of this ticket is that there is a bug, 
namely that the update command is removed from a still 
otherwise functioning Tcl interpreter.  
Tk's update blindly overrides Tcl's version of "update".
It should restore it, not disable it, when shutting down.

I've implemented a hack fix on branch [bug-9b6065d1fd].
It resolves the issue, but I don't feel it is the most 
correct/best solution.  I've put it there as a point of 
discussion.  Please review and critique.  
Thanks.

fvogel added on 2021-03-08 19:43:19:

For reference, this was discussed in comp.lang.tcl. An answer there from dgp interestingly links to [456548fff], opened 20 years ago.