Skip to content
Tcl Source Code View Ticket
Login

View Ticket

leak due to non closing base of stacked channels

Closed / Fixed3394654fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 13 years ago
Descriptionplain2011-08-19 16:13:48
$ 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)
dgpplain2011-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.
dgpplain2011-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.
dgpplain2011-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
dgpplain2011-08-20 00:28:40
When the buffer fails to test as empty,
it claims to be holding -2 bytes.
dgpplain2011-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.
dgpplain2011-08-20 02:07:20
allow_comments - 1

fixed in trunk
dgp Ticket [3394654fff]: 1 change [History diff]2011-08-20 02:07:31
dgpplain2014-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.
Log in (anonymous is fine) to add a comment.