Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2014-10-09
14:13 • Closed ticket [3062331fff]: segfault in variable traces with ttk::* widgets ... artifact: d2abb1ae06 user: dgp
2011-10-24
23:44 • Ticket [3062331fff]: 5 changes artifact: 6f9fc8a6d2 user: jenglish
2011-06-18
01:44 • Ticket [3062331fff]: 3 changes artifact: 23df240ccc user: dgp
01:44 • Ticket [3062331fff]: 4 changes artifact: ad11403c7a user: dgp
2011-06-17
06:38 • Ticket [3062331fff]: 4 changes artifact: 7812581380 user: jenglish
03:28 • Ticket [3062331fff]: 4 changes artifact: fecc27a47f user: dgp
01:30 • Ticket [3062331fff]: 4 changes artifact: be27127841 user: dgp
2011-06-16
01:19 • Open ticket [3062331fff]. artifact: 2527fa544d user: dgp
2011-06-15
05:20 • Ticket [3062331fff]: 4 changes artifact: 60d0f24ee7 user: jenglish
05:20 • Ticket [3062331fff]: 4 changes artifact: 92b005ee0d user: jenglish
03:13 • Ticket [3062331fff]: 4 changes artifact: 6f57840f9e user: jenglish
01:38 • Closed ticket [3062331fff]. artifact: b612619dfb user: dgp
2010-10-13
21:46 • Ticket [3062331fff]: 4 changes artifact: da6ba2d24b user: dgp
02:08 • Ticket [3062331fff]: 1 change artifact: 4d11e1d8aa user: dgp
02:08 • Ticket [3062331fff]: 4 changes artifact: 876301455c user: dgp
01:26 • Ticket [3062331fff]: 4 changes artifact: 8d47bf9edf user: dgp
01:19 • Ticket [3062331fff]: 4 changes artifact: 2173951573 user: jenglish
01:08 • Ticket [3062331fff]: 4 changes artifact: dd9af63d93 user: dgp
00:00 • Ticket [3062331fff]: 5 changes artifact: 82d30b5dd8 user: dgp
2010-10-12
23:57 • Ticket [3062331fff]: 4 changes artifact: d6704b6b58 user: dgp
23:53 • Ticket [3062331fff]: 4 changes artifact: af4af01f5a user: dgp
2010-10-04
22:56 • Ticket [3062331fff]: 3 changes artifact: 0c45b2a7c5 user: dgp
2010-09-09
09:27 • Ticket [3062331fff]: 4 changes artifact: d83348b4b2 user: pcmacdon
02:22 • New ticket [3062331fff]. artifact: 63f2216db6 user: pcmacdon

segfault in variable traces with ttk::* widgets

Closed / Fixed3062331fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 12 years ago
Descriptionplain2010-09-09 02:22:01
The script below crashes wish 8.5.9 and 8.5.8 (on Linux).  

# Crash script
pack [ttk::button .b -textvariable Y]
trace variable Y u "destroy .b"
unset Y
pcmacdonplain2010-09-09 09:27:09
Possibly related is ID: 2629338

Note, this may be due to the fact the handler is trying to remove
the trace thats to be called next.
dgp Ticket [3062331fff]: 3 changes [History diff]2010-10-04 22:56:34
dgpplain2010-10-12 23:53:24
Program received signal SIGSEGV, Segmentation fault.
0x08223505 in Tcl_GetString (objPtr=0x83c8578)
    at /home/dgp/cvs/tcl-only/generic/tclObj.c:1625
1625        if (objPtr->typePtr->updateStringProc == NULL) {
(gdb) up
#1  0x080eeb9a in VarTraceProc (clientData=0x844eae8, interp=0x82e6768,
    name1=0x8454420 "Y", name2=0x0, flags=192)
    at /home/dgp/cvs/tk/unix/../generic/ttk/ttkTrace.c:42
42          name = Tcl_GetString(tracePtr->varnameObj);
dgpplain2010-10-12 23:57:00
suggests an invalid state in tracePtr->varNameObj
where both objPtr->bytes == NULL and objPtr->typePtr == NULL.

Something somewhere is not following the rules.
dgpplain2010-10-13 00:00:14
That points the finger of blame at
ttkTrace.c in Tk.  I'll leave this item open
for now in case we can close it quickly.
If not, we should re-open in the Tk tracker.
dgpplain2010-10-13 01:08:14
Possibly a flaw still in Tcl_UntraceVar2() as well.

The call to Tcl_UnsetVar2() is bailing out based on
the flags set in the variable before it gets to the
code that scans the activeVarPtr chain for presence
of the trace that's getting unset.
jenglishplain2010-10-13 01:19:34
Quick diagnosis indicates that this is the same problem as #2629338 -- the trace machinery is calling a VarTraceProc after it has been removed.

Not sure what to do about this.
dgpplain2010-10-13 01:26:20
I wanted to blame var reform for this, but
the same test case also crashes Tcl/Tk 8.5a6
in what appears to be the same way.
dgpplain2010-10-13 02:08:27
A lawyer could probably make a case
that things are behaving "as documented"
in the ACCESSING VARIABLE DURING TRACES
section.

During an unset operation, when unset traces
fire, they see the variable (and all traces attached
to it) as already gone.  This means that those
bits of code trying valiantly to tidy up by removing
those unset traces that are no longer valid get
turned into no-ops.  Then they cannot remove
these invalid trace routines before the unset
operations launches them anyway and they
crash.

"As documented", true, but that just means
we've documented a system that's completely
unworkable.

le sigh.
dgp Ticket [3062331fff]: 1 change [History diff]2010-10-13 02:08:28
dgpplain2010-10-13 21:46:07
so, two perspectives on what's wrong here.

1) Tcl_UntraceVar() can be a no-op.  Code that
     doesn't account for that can run into this trouble,
    but accounting for that is at best inconvenient, and
    possibly unworkably difficult.

2) Once an [unset] operations gets started, its set of
    unset traces is locked in place.  You can't get rid
    of any of them, even if you know they will crash
    when run.

Two ways of describing the same inherent brokenness
in the unset trace model.

:P
dgpplain2011-06-15 01:38:32
allow_comments - 1

Fixed for Ttk 8.5.10 +
jenglishplain2011-06-15 03:13:57
Note that the attempted fix (fossil commit ca3d9b7274) -- zero out tracePtr->varnameObj in Ttk_UntraceVariable() and test it in the trace callback -- is not completely correct, since tracePtr itself is freed (this will probably crash in a TCL_MEM_DEBUG build for example).
jenglishplain2011-06-15 05:20:35
I don't think the second fix (fossil e9e6f67c5b) fixes it either (check under valgrind) -- calling Tcl_Preserve()/Tcl_Release() in VarTraceProc() is too late, since tracePtr (VarTraceProc's clientData) has already been EventuallyFree()d by the time it gets there.

Timeline is:
script-level unset trace calls [destroy .b]
.b's cleanup code calls Ttk_UntraceVariable, which frees the trace handle and (attempts to) remove the trace hook;
trace hook gets called anyway, accessing freed memory.
jenglishplain2011-06-15 05:20:36
allow_comments - 0
dgpplain2011-06-16 01:19:23
Reverting failed patches.
dgpplain2011-06-17 01:30:48
Here's a related demo of the problem,
using only the (testing) commands of Tcl,
leaving out the Tk complication:

testlink create {*}[lrepeat 14 1]
trace add variable int unset {testlink delete; #}
unset int

This does not crash, but it does leave the
variable "int" linked to its C counterpart
while all the other special variables of
the [testlink] command properly have their
links broken by [testlink delete].
dgpplain2011-06-17 03:28:43
See new Tk branch bug-3062331 for a new
attempt to fix this in Ttk.
jenglishplain2011-06-17 06:38:16
Looks like [e1302383c5] ought to do the trick. 

(My only suggestion would be to add a note mentioning this bug #, to explain why the extra machinery bypassing Tcl_UntraceVar is necessary.)

(Also also: the comment in Tcl_UntraceVar2() claiming that "The code below makes it possible to delete traces while traces are active" perhaps ought to be amended, lest others fall into the same trap I did of believing what it says...)
dgpplain2011-06-18 01:44:27
data_type - 360894

Converting this to a lower prio feature request.
I'd like to think a saner trace design is possible
whenever the time comes we can more seriously
consider such a thing.  May be wishful thinking.
History shows that deletes, destructors and the
like are devilishly tricky.  May be no "right" design
to achieve.
dgp Ticket [3062331fff]: 3 changes [History diff]2011-06-18 01:44:28
jenglishplain2011-10-24 23:44:42
See also #3425285.  Initial fix (2011-06-17) introduced another bug, now fixed (2011-06-29).  Unfortunately the buggy version made it into Tk release 8.5.10.

See http://core.tcl.tk/tk/info/b938e2364a for the final fix.
dgpplain2014-10-09 14:13:53
Appears this was finished long ago.
Log in (anonymous is fine) to add a comment.