Skip to content
Tcl Source Code View Ticket
Login

View Ticket

unset traces on local vars write in caller context

Open7f02ff1efa308825b21e58ec04cee898fe9c44adCreated 10 years agoUpdated 9 years ago
Descriptionplain2016-09-07 17:06:01
proc constant {name value} {
    upvar 1 $name c  
    set c $value
    trace variable c wu [list reset $value]
}
proc reset {v a i o} {
    uplevel 1 [list constant $a $v]
 }
proc demo {} {
  puts [info exists pi]
  constant pi 3.14
  puts [info exists pi]
}
puts [info exists pi]
demo
puts [info exists pi]

Output is:

0
0
1
1

Output should be:

0
0
1
0

The unset trace that re-creates the variable
is doing so in the caller context of [demo].
This is wrong.
Referenced by2
  • b98ee56376[7f02ff1efa] Fix scope for var unset traces when procs return. *** POTENTIAL INCOMPATIBILITY *** check-in2016-09-08 02:38:39
  • b25357cf3aAttempt to fix [7f02ff1efa]. Make trace-18.1 fail. Suspect test is an experiment that preserves the bug. check-in2016-09-07 18:31:58
dgpplain2016-09-07 17:17:34
The same flaw is present on core-8-0-3-pr
which suggests it is in all of recorded Tcl
history.
dgpplain2016-09-07 17:22:14
I have a binary of tclsh 7.5p1 which exhibits
the same flaw.

This raises the concern that this has been wrong
for so long, there may well be scripts relying on
the bug. Very old, unmaintained scripts.

Suggest we fix only in 8.7+, at least at first.
dgpplain2016-09-07 18:32:53
See branch bug-7f02ff1efa.

Test trace-18.1 fails.  Suspect the
test is wrong.
dgpplain2016-09-08 02:39:23
Fixed and tested in 8.7.
apnadkarniplain2016-09-09 06:16:17
Note that the documentation currently states:

"If an unset occurs because of a procedure return, then the trace will be invoked in the variable context of the procedure being returned to:"

So although the exhibited behaviour may not be desirable, it is what is documented.
dgpplain2016-09-09 12:22:59
So it is.

That documentation is also as old as recorded
Tcl history.

The design seems unusably broken if you ask me.
It forces you to write a handler script that
functions correctly in 2 completely different contexts.

I'll revert the change from the trunk (8.7).

For now, I plan to leave it in novem.  At least
this wart should be removed in Tcl 9, IMHO,
if not a more complete reworking of traces
altogether.  Experience shows me that a
programmatic unset of a variable is meaningfully
different from the destruction of a variable
(say, because context or interp are going away),
and the trace mechanism ought to be able to react
to that difference.
apnadkarniplain2016-09-09 12:57:20
Personally, I would not have any heartburn if it was fixed for 8.7. Difficult for me to imagine any code that relied on this behaviour. But then again my imagination doesn't go too far.
dkfwiki2016-10-09 16:35:59

I'd like to totally remove traces on local variables from novem. The semantics are really expensive to implement and deeply confusing. However, I'd need to replace with an ability to get callbacks when a frame terminates (that is useful) and having the ability to designate variables as being unable to change after the initial setup is entirely reasonable too.

FWIW, tclquadcode is never going to be exactly reproducing of the trace-level view. The most we're likely to promise is that if a global variable would have been read from or written to at least once on some path between two suspension points (e.g., entries and calls to return or yield), a trace will be fired. That's really quite different to getting the semantics right, and gives us a lot more freedom to optimise.

gustafn2plain2016-11-16 08:59:32
unset traces on local variables are a nice means for firing handlers when local scopes are left. we are using this in some places for the automatic cleanup of local objects, when a proc scope is left. 

Maybe this behavior could be moved with some tweaking into command leave traces. However,  the trace handler has no access to the local vars of the left proc, so one has to record the information about the objects which should be deleted somewhere else. E.g. defining on-shot handlers for every proc invocation seems expensive, global vars are not pretty, ...
dgpplain2018-06-01 17:55:46
test trace-18.5 is failing again on novem.  Time to consider
how we handle this issue again.
Log in (anonymous is fine) to add a comment.