Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2011-08-11
04:40 • Ticket [3386417fff] errorstack leaked on syntax error status still Closed wi... artifact: de3602ee56 user: ferrieux
2011-08-10
00:03 • Closed ticket [3386417fff]. artifact: 0556bb83ab user: ferrieux
00:03 • Ticket [3386417fff]: 4 changes artifact: 03d613f799 user: ferrieux
2011-08-09
06:13 • Ticket [3386417fff]: 4 changes artifact: 872d87b783 user: ferrieux
06:13 • Add attachment errorstackloop.patch to ticket [3386417fffffffff|3386417... artifact: bc41c0ced7 user: ferrieux
05:44 • Ticket [3386417fff] errorstack leaked on syntax error status still Open with... artifact: a86ed1e5b0 user: ferrieux
2011-08-08
19:57 • Ticket [3386417fff]: 4 changes artifact: 20c7176632 user: dkf
01:59 • Ticket [3386417fff]: 4 changes artifact: b44dafb799 user: ferrieux
00:36 • Ticket [3386417fff]: 4 changes artifact: d7d01d004f user: ferrieux
2011-08-07
19:54 • Ticket [3386417fff]: 4 changes artifact: cf7cbd36c2 user: msofer
18:33 • Ticket [3386417fff]: 4 changes artifact: 64eb4dbdc9 user: ferrieux
18:09 • Ticket [3386417fff]: 4 changes artifact: 96087605cd user: ferrieux
04:17 • Ticket [3386417fff]: 4 changes artifact: ce7eae230e user: msofer
00:21 • Ticket [3386417fff]: 4 changes artifact: a7e9842cec user: ferrieux
00:06 • Ticket [3386417fff]: 4 changes artifact: 9e65f2384c user: ferrieux
2011-08-06
23:49 • Ticket [3386417fff]: 4 changes artifact: d0db4e5596 user: ferrieux
21:45 • Ticket [3386417fff]: 4 changes artifact: 278af2e4b0 user: msofer
20:07 • Ticket [3386417fff]: 4 changes artifact: 35b394048c user: ferrieux
18:45 • Ticket [3386417fff]: 4 changes artifact: 683e19203e user: ferrieux
17:50 • Ticket [3386417fff]: 4 changes artifact: 9a8dd6cfea user: msofer
17:46 • Ticket [3386417fff]: 5 changes artifact: e27f6d2e11 user: ferrieux
2011-08-04
23:03 • New ticket [3386417fff]. artifact: b613eaa37f user: msofer

errorstack leaked on syntax error

Closed / Fixed3386417fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-08-04 23:03:38
When the compiler bails out on a syntax error, the errorstack is leaked. For example, for the script

  namespace eval x {set syntax {}{}}

valgrind reports


  0:  malloc (vg_replace_malloc.c:236)
  1:  TclpAlloc (tclAlloc.c:705)
  2:  Tcl_Alloc (tclCkalloc.c:1046)
  3:  Tcl_NewListObj (tclListObj.c:225)
  4:  Tcl_CreateInterp (tclBasic.c:542)
  5:  Tcl_Main (tclMain.c:639)
  6:  main (tclAppInit.c:84)
-- foo: ==9070== 3,248 (48 direct, 3,200 indirect) bytes in 1 blocks are definitely lost in loss record 136 of 136
ferrieuxplain2011-08-06 17:46:32
Quick analysis shows that an extra ref (to errorstack, but also to other return options) is kept in an options-dict computed by CompileReturnInternal. That dict is soldered into a literal (TclAddLiteralObj), so it looks like the lifecycle of that literal is the culprit.
msoferplain2011-08-06 17:50:38
Unlikely: my valgrind script would show the literal or the options dict being leaked, but not the errorstack (as a ref to it would still be around, namely in the options dict). This is a 'definitely lost' struct ...
ferrieuxplain2011-08-06 18:45:07
Good point :) So maybe that literal thing is a red herring. However, what is exactly your way of exiting Tcl so that all finalizations execute ? Are you using the 2919042 trick ?
ferrieuxplain2011-08-06 20:07:32
Cannot seem to repro with HEAD, even though patch 2919042 applied and TCL_FINALIZE_ON_EXIT set to 1, and single-line script with the above code passed as arg to non-interactive tclsh
Note that valgrind --leak-check=full gives many other (larger) leaks, but not this one.

Please give details about experimental conditions to help repro ;-)
msoferplain2011-08-06 21:45:00
Positively strange ...

To repro
  CFLAGS=-DPURIFY ./configure --disable-shared --enable-symbols

Then

mig@ari:~/DEVEL/tcl-core/trunk/unix$ cat /tmp/foo
namespace eval x {set syntax {}{}}
proc exit args {}
mig@ari:~/DEVEL/tcl-core/trunk/unix$ cat /tmp/foo | /usr/bin/valgrind --leak-check=yes --num-callers=10 --show-reachable=yes ./tcltest &> /tmp/foo.res

Look for 'definitely' in /tmp/foo.res, you'll find it.

BUT: the leak does NOT appear if you do
   /usr/bin/valgrind --leak-check=yes --num-callers=10 --show-reachable=yes ./tcltest /tmp/foo &> /tmp/foo.res
So it may have something to do with io???
ferrieuxplain2011-08-06 23:49:14
Hmm, I'd guess "interactive" is key. Though [history clear] was not enough to purge the offending literal in my tests. Will dig further.
ferrieuxplain2011-08-07 00:06:31
By the way, though you're not telling, I guess you are using the patch from 2919042 (so that -DPURIFY forces finalization), right ? Since it seems useful for leak hunting, how about a commit ?
ferrieuxplain2011-08-07 00:21:05
Sorry to push ahead with even more tangents.. but in my case:
 - with 2919042 's val4.patch and CLFLAGS=-DPURIFY, I do get the errorStack leak regardless of the kind of script passing method (stdin via <, stdin via pipe, or tclsh single arg).

- with val4.patch and no PURIFY, but just TCL_FINALIZE_ON_EXIT=1, I get many "possibly lost" but no "definitely lost", regardless of the script disposition.
msoferplain2011-08-07 04:17:37
not using the patch here
ferrieuxplain2011-08-07 18:09:12
Ah, ok !
Also note that your "proc exit args {}" is ineffective when placed after the barfing [namespace eval] ;-)
So basically, two things are equivalent regarding this leak, once -DPURIFY is set:
  - apply the patch
  - redefine exit (and do it before erroring out ;-)
However, with either method I cannot reproduce the difference between stdin and arg script. In both cases I do get the "definitely lost".
ferrieuxplain2011-08-07 18:33:30
Anyhow, I'm still wondering about literals' end of life. A watchpoint on the options dict object's refcount shows it never goes below 1, nor is TclReleaseLiteral called on it. Could it be subject to a kind of "fast erasure" that would deplete its hashtable behind the scenes ?
msoferplain2011-08-07 19:54:22
Redefining exit before the error (good catch, thx!) now shows the leak here in all script-input modes. Ie, not the same that you are seeing ...

Another funny observation: replacing 'namespace eval x' with 'if 1' (both insuring bcc'ing) removes the leak too.
ferrieuxplain2011-08-08 00:36:05
Hum, it turns out that hashtable values are stored in the "clientData" field of Tcl_HashEntry, with a cast (to void* or int*)... I dunno how smart Valgrind is about casts, but could a pointer type mismatch block its path-walking ?
ferrieuxplain2011-08-08 01:59:58
To clarify: I'm aware that a type cast is a source-level abstraction, and that all pointers are machine words.. but not sure of the amount of type info that -g brings into the executable. Pulling at straws...
dkfplain2011-08-08 19:57:01
I'd guess that the problem is elsewhere; Valgrind's really operating on machine words (it's a machine-code-level annotation tool applied to memory analysis). I've seen times when I thought it was a problem in valgrind and where it turned out to be somewhere else _in my code_ than the place I expected. I'm resigned to such things these days, but it's one of these hard-to-believe got-to-see-it-for-yourself matters...
ferrieuxplain2011-08-09 05:44:58
OK, got it: what we are witnessing is a cycle in the reference graph. Indeed errorstack is special in that it records, as true references, all the command lines in the stack, including the [namespace eval x OFFENDINGSCRIPT]. As a consequence:
    - options(-errorstack) holds a reference to OFFENDINGSCRIPT
    - OFFENDINGSCRIPT's BC holds, as a literal, a reference to options()
Cycle.

So, the reason why TclCleanupByteCode is not called on the syntax error, is the above cycle.
So the whole cycle gets unreachable, and valgrinds just selects one as "definitely lost" and the rest as "indirectly lost".

The cure is simply to remove the (compile-time) errorstack from the options dict when processing a syntax error.
Patch to follow.
ferrieuxplain2011-08-09 06:13:21
File Added - 420603: errorstackloop.patch
ferrieuxplain2011-08-10 00:03:00
Committed to HEAD after a quick chat.
ferrieux Closed ticket [3386417fff]. [History diff]2011-08-10 00:03:01
ferrieuxplain2011-08-11 04:40:31
As a post-bug-mortem comment: I wrote a page on the wiki summarizing all I've learnt while solving this one:

     Leak Hunt (C level)
     http://wiki.tcl.tk/28634

it also gives the basic 'valgrind recipe' and mentions the --enable-symbols=all pitfall dgp observed
Log in (anonymous is fine) to add a comment.

Attachments: