| Ticket UUID: | a34733451b2cf189528061944466cf7ce7015008 | ||
| Title: | Regression with TIP #636 in foreach | ||
| Type: | Bug | Created on: | 2023-07-09 15:39:52 |
| Submitter: | fvogel | Assigned to: | griffin |
| Subsystem: | 16. Commands A-H | Severity: | Critical |
| Priority: | 5 Medium | Last modified: | 2024-08-20 14:48:55 |
| Status: | Closed | Closed by: | pooryorick |
| Resolution: | Fixed | Closed on: | 2024-08-20 14:48:55 |
| Version: | main | ||
| Description: | ||||
|
Github Actions testing Tk trunk on Linux with the main Tcl (9.0) branch results in three errors that were not present before.
==== imgPhoto-4.79 ImgPhotoCmd, transparency get: no option FAILED
==== Contents of test case:
set pixelCoords {{156 239} {76 207} {153 213} {139 43} {75 112}}
foreach coord $pixelCoords {
lappend result [photo1 transparency get {*}$coord]
}
set result
---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: can't read "coord": no such variable
while executing
"photo1 transparency get {*}$coord"
("foreach" body line 2)
invoked from within
"foreach coord $pixelCoords {
lappend result [photo1 transparency get {*}$coord]
}"
("uplevel" body line 3)
invoked from within
"uplevel 1 $script"
---- errorCode: TCL LOOKUP VARNAME coord
==== imgPhoto-4.79 FAILED
==== imgPhoto-4.81 ImgPhotoCmd, transparency get: -alpha FAILED
==== Contents of test case:
set pixelCoords {{156 239} {76 207} {153 213} {139 43} {75 112}}
foreach coord $pixelCoords {
lappend result [photo1 transparency get {*}$coord -alpha]
}
set result
---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: can't read "coord": no such variable
while executing
"photo1 transparency get {*}$coord -alpha"
("foreach" body line 2)
invoked from within
"foreach coord $pixelCoords {
lappend result [photo1 transparency get {*}$coord -alpha]
}"
("uplevel" body line 3)
invoked from within
"uplevel 1 $script"
---- errorCode: TCL LOOKUP VARNAME coord
==== imgPhoto-4.81 FAILED
==== panedwindow-17.1 MoveSash, move right FAILED
==== Contents of test case:
panedwindow .p -showhandle false -borderwidth 0 -sashpad 0 -sashwidth 2
foreach w {.f1 .f2} c {red blue} {
.p add [frame $w -height 20 -width 20 -bg $c] -sticky nsew
}
# Get the requested width of the paned window
lappend result [winfo reqwidth .p]
.p sash place 0 30 0
# Get the reqwidth again, to make sure it hasn't changed
lappend result [winfo reqwidth .p]
# Check that the sash moved
lappend result [.p sash coord 0]
---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: unknown color name "3"
(processing "-bg" option)
invoked from within
"frame $w -height 20 -width 20 -bg $c"
("foreach" body line 2)
invoked from within
"foreach w {.f1 .f2} c {red blue} {
.p add [frame $w -height 20 -width 20 -bg $c] -sticky nsew
}"
("uplevel" body line 3)
invoked from within
"uplevel 1 $script"
---- errorCode: TK LOOKUP COLOR 3
==== panedwindow-17.1 FAILED
plus an additional error message: [...] unixWm.test Test file error: can't read "geom": no such variable util.test [...] Bisection shows that the first commit triggering this regression is [91c2f411e7], that is the merge of TIP #636 implementation into trunk. In all the four cases, the problem is in a foreach loop apparently not assigning a value to the loop variable. Warning: the problem cannot be reproduced when running only the imgPhoto.test or panedwindow.test file from Tk. Running the entire test suite of Tk allows however to reproduce (on all three platforms). | ||||
| User Comments: | ||||
griffin added on 2023-07-09 19:37:12:
Hard to debug. Requires some prior test(s) to effect state. imgPhoto test seems to run in a subprocess, direct gdb doesn't work. It also seems that child interps are involved. One area of change that is a concern is in the bytecode engine. There are spots where care must be taken when making calls out of the bce when the call has the potential of then calling eval. This creates a recursive call to the bce. The caution is to make sure stack ptr save/restore calls are made, otherwise the stack can be corrupted. Abstract Lists increased the chances of this in places that were not protecting the stack pointer. The debugging continues... chrstphrchvz added on 2023-07-11 12:26:36:
This is a duplicate of [6022b9f946]. The bug got merged from main into tip-636-tcl9-644 by [3e44e4bdd3]. The bug was then fixed on main, but the fix somehow did not get merged into tip-636-tcl9-644 during [2f493bd21cb9]. The bug presumably then remained on tip-636-tcl9-644 and was merged back into main by [91c2f411e7]. jan.nijtmans added on 2023-07-11 13:05:31:
@nathan, can you help here? Since you introduced the bug, you should be able to fix it (again). I am aware that the fix is [c405bdd1f0|this commit], but this commit has so many changes that I cannot extract the relevant change from it. It's pure magic to me (which is not meant as a compliment). griffin added on 2023-07-11 13:47:44:
The problem is with TclDuplicatePureObj(). This function forces a type upon a value which sometimes causes a shimmer. This is bad for abstract lists when called prematurely, i.e. before the value is actually consumed. I had modified the function to prefer src Obj type over the requested type, except when the src was just a string. Any shimmer would already occur later at the point of consumption. The point of failure in this ticket is specifically relying on Dup for type conversion. (A hidden aspect of Dup) The specific value in this case is a variable name previously used, and still exits, as an "index" type. This interfered with the variable lookup because the string was stripped from the obj. It also explains why the test passes when run alone, the variable was not used prior to the test, so did not exist. apnadkarni added on 2023-07-11 15:23:04:
I don't think preference to the src Obj type over requested type would work in all cases as my reading of some of the calls indicates caller assumes the internal rep is the requested type and directly accesses it accordingly (in particular does not check for errors when further calls fail). griffin added on 2023-07-11 15:44:18:
Converting the type doesn't work in all cases either. That's why it is a bad idea to have 1 function do 2 orthogonal things, make a pure copy, convert the type. griffin added on 2023-07-12 03:34:16:
What happened?
Take away:
This issue has been fixed by replacing TclDuplicatePureObj with Tcl_DuplicateObj(). In this case, the values are var names. It's all about the string here. apnadkarni added on 2023-07-12 05:46:40:
Adding my two cents, more to clarify my own thinking:
apnadkarni added on 2023-07-12 06:06:52:
On further thought, I'll modify my third point as * `Tcl_DuplicateObj` was used in at least two ways. One was to make an unshared copy for further modification. The other was to ensure the internal rep remained valid (e.g. iterating a list) even though no modification was planned. `TclDuplicatePureObj` can replace `Tcl_DuplicateObj` in the first instance since anyways the `bytes` will be invalidated. On the other hand, it can only be applied in the second case in limited cases where the dup'ed object is not preserved. For example, immediately discarded after iteration on a list. In any case, all uses need to be examined for which of the two cases applies. apnadkarni added on 2023-07-12 07:35:58:
One more note - the semantics of TclDuplicatePureObj differ between 8.7 and 9.0. Not sure if this is intentional but it adds to the confusion about usage. 8.7 coerces to the passed in typePtr but 9.0 prefers the typePtr of the source Tcl_Obj. jan.nijtmans added on 2023-07-12 07:55:31:
Whatever recent changes were done to TclDuplicatePureObj() should be backported to 8.7 as well (we don't want the recently discovered bugs discovered in 9.0 to re-appear in 8.7). I never liked the introduction of TclDuplicatePureObj(), but I didn't have much arguments to complain. apnadkarni added on 2023-07-12 13:02:36:
My analysis might be misplaced so would be useful to have @pooryorick comment but ... Jan, I noticed you added int, double, index as additional "protected" types to TclDuplicatePureObj. Since lists (abstract and traditional) can also have the same issue - multiple string representations for the same internal rep - should those also be protected. In which case one wonders what's left and whether the whole exercise is worthwhile. Originally, the list code used TclListObjCopy as a specialized internal function. It was important from a performance perspective within its limited context. TclDuplicatePureObj generalizes it to do more but given it does not preserve value semantics its applicability is limited to those same use cases. That hints to me that the protections be removed, and the semantics of the function changed to reflect values are not preserved, only intreps are, and all call sites changed to revert to Tcl_DuplicateObj when they fall outside those limited use cases. Alternately, each type could revert back to its own specialized "internal copy" as the list implementation used to. This also avoid all the checks present in the "generalized" TclDuplicatePureObj. pooryorick added on 2023-07-12 13:37:54:
The TIP #636 code should be reverted from trunk until it can be successfully merged without causing this regression. I've warned that TIP 636 is not ready for prime time, and this issue report is evidence of that. TclDuplicatePureObj() should not be compromised in order to accommodate TIP 636. Rather, the TIP 636 should be modified to not interact with TclDuplicatePureObj() at all. If the TIP 636 work does interact with TclDuplicatePureObj() it is a bug in the implementation of TIP 636. TclDuplicatePuerObj() should not be modified to be sensitive to internal representations that seem to implement a Tcl_Obj interface. Such a modification would be a hack in lieu of a correct implementation of TIP 636. jan.nijtmans added on 2023-07-12 13:49:19:
> In which case one wonders what's left and whether the whole exercise is worthwhile I'm having the same doubt! The only type I see which doesn't have this problem is the ArithSeries objType. I would like to throw away the TclDuplicatePureObj() function, but first we need to discuss what to do instead .... griffin added on 2023-07-12 13:53:15:
pooryorick: #636 is not the problem. TclDuplicatePureObj has been used incorrectly or indiscriminately, that is the problem. jan.nijtmans added on 2023-07-12 13:54:25:
@Nathan. Your TclDuplicatePureObj() changes, which broke he build in your first commit are the cause of the trouble with TIP #636. So I suggest to revert the changes you introduced, until they work well with TIP #636 :-) jan.nijtmans added on 2023-07-12 13:57:29:
Ashok wrote: > My analysis might be misplaced so would be useful to have @pooryorick comment but ... I think your analysis is well-done. It's very clear to me. pooryorick added on 2023-07-12 14:04:31:
The TIP #636 code should be reverted from trunk until it can be successfully merged without causing this regression. I've warned that TIP 636 is not ready for prime time, and this issue report is evidence of that. TclDuplicatePureObj() should not be compromised in order to accommodate TIP 636. Rather, the TIP 636 should be modified to not interact with TclDuplicatePureObj() at all. If the TIP 636 work does interact with TclDuplicatePureObj() it is a bug in the implementation of TIP 636. TclDuplicatePureObj() should not be modified to be sensitive to internal representations that seem to implement a Tcl_Obj interface. Such a modification would be a hack in lieu of a correct implementation of TIP 636. pooryorick added on 2023-07-12 14:06:32:
Oops, please disregard the previous repeated message. Here is the new message: The last trouble reported with TclDuplicatePureObj() was fixed almost two months ago, in [c405bdd1f0]. Since then there have been no test failures until the merge of TIP 636. That clearly indicates that the trouble is with the TIP 636 merge, and that's what should be reverted here. jan.nijtmans added on 2023-07-12 14:51:08:
> The TIP #636 code should be reverted from trunk until it can be successfully merged without causing this regression Build is passing now, so no worries. I'm not closing this ticket yet, until we decided what to with TclDuplicatePureObj() fvogel added on 2023-07-13 06:18:50:
> Build is passing now The Github job failed yesterday morning. I see you simply re-ran the job and this time it passed. How come...?? There was no fixes between the first and the second attempt, isn't it? Besides: my thanks to everyone involved in this ticket! jan.nijtmans added on 2023-07-13 06:52:07:
> How come...?? In GitHub, the Tk builds are done _before_ the Tcl builds. That means that when Tk builds, Tcl doesn't have the latest commits from fossil yet. So, I re-ran the Tk build after Tcl synced. :-) fvogel added on 2023-07-13 07:48:36:
This explanation indeed makes a lot of sense! Do we control when the jobs are launched in each particular project? pooryorick added on 2024-08-20 14:48:55:
Doing some post-mortem analysis on this, Brian said below, "The Dup-Pure
removes the string rep, leaving the "index" ObjType internal rep.".
| ||||
