leak in tcltest
Closed / Fixed3386721fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-08-05 11:34:52
This script
package require tcltest 2
namespace import -force ::tcltest::*
testintobj set 0 1
interp create slave
load {} Tcltest slave
causes the following leak:
0: malloc (vg_replace_malloc.c:236)
1: TclpAlloc (tclAlloc.c:705)
2: Tcl_Alloc (tclCkalloc.c:1046)
3: Tcl_NewIntObj (tclObj.c:2410)
4: TestintobjCmd (tclTestObj.c:640)
5: TclNREvalObjv (tclBasic.c:4272)
6: TEBCresume (tclExecute.c:2789)
7: TclNRRunCallbacks (tclBasic.c:4314)
8: TclEvalObjEx (tclBasic.c:5885)
9: Tcl_EvalObjEx (tclBasic.c:5866)
The script is the minimal thing I found while investigating a leak in execute.test, every line seems significant.
Referenced by1
983839ac0dClosed ticket [6525391072]: TclObjTest_Init leaks when Tcltest is initialized twice in an interpreter plus 8 other changes ticket2026-10-13 12:07:32
dgpplain2011-08-10 01:48:54
No longer leaking in the trunk?
dgp Pending ticket [3386721fff].
[History diff]2011-08-10 01:48:55
msoferplain2011-08-10 04:58:03
Still there: mig@ari:~/DEVEL/tcl-core/trunk/unix$ /usr/bin/valgrind --leak-check=yes --num-callers=10 ./tcltest < /tmp/foo ==9650== Memcheck, a memory error detector ==9650== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==9650== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info ==9650== Command: ./tcltest ==9650== ==9650== ==9650== HEAP SUMMARY: ==9650== in use at exit: 1,074 bytes in 26 blocks ==9650== total heap usage: 40,907 allocs, 40,881 frees, 3,898,487 bytes allocated ==9650== ==9650== 48 bytes in 1 blocks are definitely lost in loss record 23 of 26 ==9650== at 0x4C2815C: malloc (vg_replace_malloc.c:236) ==9650== by 0x4BCFA1: TclpAlloc (tclAlloc.c:705) ==9650== by 0x4CED10: Tcl_Alloc (tclCkalloc.c:1046) ==9650== by 0x4546B6: Tcl_NewIntObj (tclObj.c:2410) ==9650== by 0x422B18: TestintobjCmd (tclTestObj.c:640) ==9650== by 0x4C2C00: TclNREvalObjv (tclBasic.c:4269) ==9650== by 0x54B39C: TEBCresume (tclExecute.c:2793) ==9650== by 0x4C2D31: TclNRRunCallbacks (tclBasic.c:4311) ==9650== by 0x4C54E9: TclEvalObjEx (tclBasic.c:5882) ==9650== by 0x4C5479: Tcl_EvalObjEx (tclBasic.c:5863)
ferrieuxplain2011-08-10 07:03:53
OK, got it. In this test, there is a static array varPtr[] that holds the working values. When [load .. slave] is called, somehow the TclObjTest_Init() function is called a 2nd time, and that array is reset, overwriting the obj references. Now to understand how the LoadObjCmd code is supposed to prevent that double Init.
ferrieuxplain2011-08-10 17:57:44
Ah, I realize now that the Init func of a package is supposed to be called once for each interp it is loaded in ... so clearly the fault here is the init proc's, which is not designed for this multiple-interp setup. Fix: move the varPtr array from static to per-interp storage. Never done this myself, appreciate a hand. Also, verify similar behavior in tons of other Tcltest subpackages ?
msoferplain2011-08-10 18:03:06
assigning to the package expert so that he chimes in
ferrieuxplain2011-08-10 18:06:39
Note that tclTest.c also has a few r/w statics. Also tclThreadTest.c has a few ones, though I cannot say they really are a threat (mainThreadID etc), by ignorance of realistic cases of concurrent/multiinterp loads of the Thread package.
nijtmansplain2011-08-10 21:30:47
>Fix: move the varPtr array from static to per-interp storage. Never done >this myself, appreciate a hand. The 'standard' way to deal with this is using TSD (Thread Specific Cata)
nijtmansplain2011-08-10 21:33:53
Committed to trunk. Michael, does this help?
Regards,
Jan Nijtmans
msoferplain2011-08-10 21:37:34
nope, leak still present
nijtmansplain2011-08-10 22:29:27
Actually, that's not strange: The testintobj set 0 1 allocates a Tcl object in the TSD, but no-one throws it away!! Maybe we could create a "testclearall" command which clears all this storage as cleanup command after the test. Is there a way to define a cleanup callback when the thread ends?
nijtmansplain2011-08-10 22:33:14
How about creating an exit handler using Tcl_CreateExitHandler(), which clears the storage? That should do the trick.
ferrieuxplain2011-08-10 23:16:59
Jan, you're up the wrong tree :} There are two orthogonal things (1) the fact that the varPtr are never freed actively (2) the fact that two calls to TclTestObj_Init by two interps wreak havoc by overwriting the static array As it turns out, (1) is no problem because valgrind marks such blocks as "still reachable" (and there are a bunch of other such ones : a low priority endeavour), while here we're after the more ominous "definitely lost". And (2) is the real problem: an overwritten last reference to an object makes it definitely lost indeed. Note that using the TSD is not enough. As I said earlier, those data shoule be per-interp, not per-thread: there may be several interps in a single thread, as is notably the case here (a slave).
dgpplain2011-08-10 23:43:18
I still cannot reproduce the claimed leak. Will someone please reveal the complete recipe?
ferrieuxplain2011-08-10 23:53:03
env CFLAGS=-DPURIFY ./configure && make clean && make tcltest valgrind ./tcltest foo.tcl
dgpplain2011-08-10 23:53:52
Ok, that's what I'm doing. The script in this Tracker item does not leak.
dgpplain2011-08-11 00:08:32
--enable-symbols=all should not be used.
ferrieuxplain2011-08-11 00:11:41
OK, so to get back to the original problem, emiliano suggested RBC as an example of per-interp package init. It uses the assocdata hashtable to store its stuff.
dgpplain2011-08-11 00:30:05
Reverted the nijtmans commit. Comments here are correct. Moving to per-thread storage isn't the right fix. If getting the right fix is easy, by all means go ahead, and if someone's ambitious and lacks other tasks, go ahead and audit Tcltest for all the similar outdated coding that must lurk within (A very very large part of Tcltest has roots back to the days when "multiple interps" just could not happen, and were coded to the truths of the day). That said, I imagine most folks reading this comment have other tasks they could be doing that would be more important in getting 8.6.0 out the door. I will not block 8.6.0 because our testing tools are not as polished as we would like them to be.
ferrieuxplain2011-08-11 01:25:18
Fixed in HEAD, with assocdata.
ferrieux Closed ticket [3386721fff].
[History diff]2011-08-11 01:25:19
nijtmansplain2011-08-11 02:57:45
Great work, Alex! Yes, that's much better!
Log in (anonymous is fine) to add a comment.