Tcl Source Code

View Ticket
Login
Ticket UUID: 3f90d66af9c432479bcdbae5fd57c075dd0a468e
Title: Inconsistent behaviour of trace add execute leave on a command already on the stack
Type: Bug Created on: 2023-05-04 12:29:21
Submitter: cyan Assigned to: nobody
Subsystem: 46. Traces Severity: Minor
Priority: 5 Medium Last modified: 2023-05-04 12:29:21
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.7a6
Description:
If a proc that is currently executing adds an execution leave trace on itself,
the trace only fires when that call returns if the proc already had an (unrelated)
execution trace registered for it when it started executing.  Otherwise the trace fires
only on the next call to the command:

proc _finish {target cmd code result op} {
    trace remove execution $target leave [list ::_finish $target]
    # fire finish breakpoint
    puts "would fire fini break"
}

proc foo {} {
    puts "in foo"
    set target [namespace which -command [lindex [info level 0] 0]]
    trace add execution $target leave [list ::_finish $target]
}

# Works if this line is uncommented
#trace add execution foo enter {apply {args {}}}

puts "first foo: [foo]"
puts "second foo: [foo]"


@aspect on the Tcl chat found this: "the block in EvalObjvCore that sets up
TEOV_RunLeaveTraces is under an if whose condition includes (cmdPtr->flags & CMD_HAS_EXEC_TRACES)"
I understand that the performance overhead may be prohibitive for rechecking each time a
proc finishes, but I'm raising this ticket in case it can be made feasible, or at least
be documented that registering a leave trace on a proc on the call stack is undefined
(depends on other trace state)