Tcl Source Code

View Ticket
Login
Ticket UUID: 3400944
Title: [exit] creates leaks
Type: Bug Created on: 2011-08-30 18:25:04
Submitter: dgp Assigned to: ferrieux
Subsystem: 53. Configuration and Build Tools Severity:
Priority: 5 Medium Last modified: 2026-10-12 17:08:05
Status: Open Closed by:
Resolution: None Closed on:
Version: obsolete: 8.6b2
Description:
Even using the special support for valgrind
(see 2919042) testing any script with [exit]
in it is going to show leaks of things that would
be freed by the natural unwinding that the [exit]
prevents.  For example,

$ cat leak.tcl
namespace eval foo exit

$ valgrind --leak-check=yes ./tclsh leak.tcl
...
==13495== 36 bytes in 3 blocks are definitely lost in loss record 40 of 50
==13495==    at 0x4005903: malloc (vg_replace_malloc.c:195)
==13495==    by 0x80DF060: TclpAlloc (tclAlloc.c:705)
==13495==    by 0x80EF8C0: Tcl_Alloc (tclCkalloc.c:1046)
==13495==    by 0x80E630A: TclArgumentEnter (tclBasic.c:5473)
==13495==    by 0x80E5E98: TclEvalEx (tclBasic.c:5213)
==13495==    by 0x80E54BE: Tcl_EvalEx (tclBasic.c:4872)
==13495==    by 0x806D584: Tcl_FSEvalFileEx (tclIOUtil.c:1751)
==13495==    by 0x8072F0F: Tcl_MainEx (tclMain.c:407)
==13495==    by 0x8073573: Tcl_Main (tclMain.c:634)
==13495==    by 0x805465B: main (tclAppInit.c:84)
==13495==
==13495== 201 (48 direct, 153 indirect) bytes in 2 blocks are definitely lost in loss record 47 of 50
==13495==    at 0x4005903: malloc (vg_replace_malloc.c:195)
==13495==    by 0x80DF060: TclpAlloc (tclAlloc.c:705)
==13495==    by 0x80EF8C0: Tcl_Alloc (tclCkalloc.c:1046)
==13495==    by 0x808ED07: Tcl_NewStringObj (tclStringObj.c:340)
==13495==    by 0x80817A3: TclSubstTokens (tclParse.c:2337)
==13495==    by 0x80E5A63: TclEvalEx (tclBasic.c:5101)
==13495==    by 0x80E54BE: Tcl_EvalEx (tclBasic.c:4872)
==13495==    by 0x806D584: Tcl_FSEvalFileEx (tclIOUtil.c:1751)
==13495==    by 0x8072F0F: Tcl_MainEx (tclMain.c:407)
==13495==    by 0x8073573: Tcl_Main (tclMain.c:634)
==13495==    by 0x805465B: main (tclAppInit.c:84)

The same issue is present using [testthread exit].
User Comments:
dgp added on 2011-08-31 01:30:05:
This is relevant to the `make valgrind` target.
It takes the test suite to be a suitable body of
code to cover seeking leaks.  So long as the
test suite includes use of [testthread exit] and
similar things, though, leaks will be reported,
likely false alarms.

I suppose a constraint could be used to work
around the matter.  Hoping for better ideas.

dgp added on 2011-08-31 02:33:46:
I see that the Thread package docs explicitly
warn against the [thread::exit] command in favor
of a preserve/release system.  Lessons the
testthread command could adopt, or the test suite
could migrat from [testthread] to [package require Thread].

ferrieux added on 2011-08-31 05:23:05:
This is similar to 3392735. Another facet :/

foxcruiser added on 2011-08-31 20:20:06:
We got the same issue with [exit] when memory debugging XOTcl/NSF based on our test suite  (with an internal refcounter and valgrind). We ended up with falling back to a graceful unwind instead of Tcl_Exit(), for the scope of test scripts:

 proc graceFullExit {{statuscode "1"}} {
      array set map {1 ok -1 error}
      set errorcode $map($statuscode)
      interp alias {} ::exit {}
      interp expose {} exit; 
      return -code $errorcode -level [expr {[info level]-1}] "Test was exited with code $statuscode"
    }

and then, before evaluating a test body:

 interp hide {} exit; 
 interp alias {} ::exit {} ::graceFullExit

works fine for us ...

serhiy.storchaka added on 2026-10-12 17:08:05:

Still true in 9.1: [exit] does not unwind the stack, so whatever the unwinding would free is reported by valgrind.

A possible solution is to make [exit] unwind the stack, like interp cancel -unwind, and exit the process when the unwinding is complete. catch could not stop it, but finally clauses would run, so the script could clean up other resources, as with Python's SystemExit exception. This is a visible change in semantics and needs a TIP.

The test suite problem was addressed as suggested here: tests which exit a thread in mid-operation have the notValgrind constraint, and [testthread exit] is no longer used in tests.