Tcl Source Code

View Ticket
Login
Ticket UUID: 3394654
Title: leak due to non closing base of stacked channels
Type: Bug Created on: 2011-08-19 16:13:48
Submitter: dgp Assigned to: dgp
Subsystem: 26. Channel Transforms Severity: Minor
Priority: 5 Medium Last modified: 2014-05-08 15:59:59
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2014-05-08 15:59:59
Version: obsolete: 8.6b2
Description:
$ cat leak.tcl
    set nested 0
    proc foo {fd args} {
        switch -exact [lindex $args 0] {
            initialize {return {initialize finalize read write}}
            finalize {return {}}
        }
        global nested
        if {$nested} {
            return
        }
        set nested 1
        chan pop $fd
    }
    set b [open /dev/null w]
    set c [chan push $b [list foo $b]]
    puts $c a
    flush $c
    close $c

$ valgrind --leak-check=yes ./tclsh leak.tcl
...
==27262== 8,577 (16 direct, 8,561 indirect) bytes in 1 blocks are definitely lost in loss record 20 of 20
==27262==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==27262==    by 0x482844: Tcl_Alloc (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x453534: TclpOpenFileChannel (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x42066F: Tcl_FSOpenFileChannel (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x4E0073: Tcl_OpenObjCmd (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x47CF39: TclNREvalObjv (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x47D583: Tcl_EvalObjv (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x47DFF3: TclEvalEx (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x42CE58: TclSubstTokens (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x47D8BF: TclEvalEx (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x47E325: Tcl_EvalEx (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
==27262==    by 0x421169: Tcl_FSEvalFileEx (in /local/tmp/dgp/fossil/tcl-only/valgrind/tclsh)
User Comments:
dgp added on 2011-08-19 23:17:40:
The ClientData of channel $c is leaking
because [close $c] is not actually closing
the channel (not calling CloseChannel(),
thus not calling the closeProc of the driver).

This is not happening because FlushChannel
sees the channel as not having an empty
buffer at "curOutPtr" and for reasons I don't
understand, takes that as an indication not
to make the close.

If I disable that test in FlushChannel(), the
leak goes away.

dgp added on 2011-08-19 23:29:13:
The need for this "only empty buffers
can permit a close" test is not demonstrated
by the test suite, FWIW.

dgp added on 2011-08-19 23:30:49:
That said, I suspect the real bug here is
that the buffers in fact should be empty
at this point, and whatever bug is causing
it not to be empty is likely a common cause
of Bug 3393276

dgp added on 2011-08-20 00:28:40:
When the buffer fails to test as empty,
it claims to be holding -2 bytes.

dgp added on 2011-08-20 01:43:20:
FlushChannel needs to account for the
possibility that the ChanWrite() call might
recycle the buffer our from under us.

Adding a test IsBufferEmpty(.) at the right
place is effective.

dgp added on 2011-08-20 02:07:20:

allow_comments - 1

fixed in trunk

dgp added on 2014-05-08 15:59:59:
Turns out this was a band-aid solution, and the
design troubles with sharing of ChannelBuffers
ran much deeper.  Now the band-aid is corrupting
the actual fix.  Removing it allows the real fix
(refcounting ChannelBuffers) to function properly.