Skip to content
Tcl Source Code View Ticket
Login

View Ticket

While in a trace-proc, traces are not disabled for TCL_TRACE

Closed / Fixed219113ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:10:59
OriginalBugID: 6049 Bug
Version: 8.3
SubmitDate: '2000-07-25'
LastModified: '2000-07-25'
Severity: MED
Status: Released
Submitter: techsupp
ChangedBy: ericm
OS: All
FixedDate: '2000-07-25'
FixedInVersion: 8.4a2
ClosedDate: '2000-10-25'


Name:
Stefan Vogel

Extensions:
None

CustomShell:
None

ReproducibleScript:
I've created a trace-callback-function "VariableProc" and registered a trace
on an array with Tcl_TraceVar2.
Than the callback is invoked with a call to e.g. "array names ...".
In the callback-function the array-variable is assigned a new value.
This causes another Write-Trace.

Here is some code (for Windows?):

#include <tcl.h>

static char*
VariableProc(clientData, interp, part1, part2, flags)
 ClientData clientData;
 Tcl_Interp* interp;
 char *part1, *part2;
 int flags;
{
  if (flags & TCL_TRACE_WRITES) {
printf("--Traced write on %s(%s)\n", part1, part2);
  } else if (flags & TCL_TRACE_ARRAY) {
printf("--Traced array on array %s(%s)\n Now setting tstkey = tstvalue.\n This will incorrectly call VariableProc with TCL_TRACE_WRITES\n", part1, part2);
/* Tcl_GetVar2 behaves correctly */
Tcl_SetVar2(interp, part1, "tstkey", "tstvalue", 0);
printf("tstkey is set to tstvalue\n");
  }
  return NULL;
}

__declspec(dllexport) int Array_test_Init(interp)
 Tcl_Interp* interp;
{
  if (Tcl_InitStubs(interp, "8.3", 0) == NULL) {
return TCL_ERROR;
  }
  Tcl_PkgProvide(interp, "Array_test", "0.1");

  /* Set an initial value to array 'foobar' */
  Tcl_SetVar2(interp, "foobar", "hello", "one", TCL_LEAVE_ERR_MSG);
  /* Put the trace on 'foobar' */
  Tcl_TraceVar2(interp, "foobar", (char*)NULL, 
(TCL_TRACE_WRITES | TCL_TRACE_READS | TCL_TRACE_UNSETS | TCL_TRACE_ARRAY),
VariableProc, NULL);
  /* Now executing a TCL_TRACE_ARRAY-trace */
  Tcl_Eval(interp, "array names foobar");

  return TCL_OK;
}

Build a DLL and call your tcl-shell, then call:
% load array_test.dll

ObservedBehavior:
The output of the above command "load array_test.dll" is:

Executing: 'array names foobar'
--Traced array on array foobar((null))
Now setting tstkey = tstvalue.
This will incorrectly call VariableProc with TCL_TRACE_WRITES
--Traced write on foobar(tstkey)
tstkey is set to tstvalue
tstkey hello
%

The Write-trace should not be executed according to the man-page

DesiredBehavior:
Expected this (from the man-page for Tcl_TraceVar2):

During read, write, and array traces, the trace procedure can read, write, or unset the traced variable using Tcl_GetVar2, Tcl_SetVar2, and other procedures. While proc is executing, traces are temporarily disabled for the variable, so that calls to Tcl_GetVar2 and Tcl_SetVar2 will not cause proc or other trace procedures to be invoked again. 

I expected the trace on "foobar" includes a trace on "foobar(tstkey)" and so 
the trace is not invoked again. Maybe I'm wrong here?



Verified.  The problem is that VAR_TRACE_ACTIVE is set for the array, but when setting an element of the array, presence of that flag is only tested for the array element, and traces on the array are blindly executed.  The fix is trivial:  in CallTraces, add a check for VAR_TRACE_ACTIVE on the array before executing traces on the array.  The test suite passes successfully with this modification, but there are no explicit tests of the TCL_TRACE_ARRAY stuff (that feature is currently not exported to Tcl).

I committed this change to CVS; the fix will go out with 8.4a2.  Thanks for finding this bug!

   Eric Melski                            The Other Tcl Guy
   ericm at ajubasolutions.com            Ajuba Solutions

-- 07/25/2000 ericm
welch Closed ticket [219113ffff]. [History diff]2000-10-26 08:34:57
dgp Ticket [219113ffff]: 3 changes [History diff]2001-04-20 02:11:54
Log in (anonymous is fine) to add a comment.