Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: af54bc6f6ee32c0540040a5b72e5661e15f20f5ad7a9df6fe41501ae28bf95d4
Ticket: ea69b0258a9833cb61ada42d1fc742d90aec04d0
Crash when using a channel transformation on TCP client socket
User & Date: petrokaz on 2023-01-12 16:33:47
Changes

  1. comment changed to:
    Tcl crashes when I setup a channel transformation on a non-blocking TCP client socket, and then remove it.
    
    Here is the minimal reproducible example.
    A dummy echo server:
    ```
    ncat -l -k -e /bin/cat --crlf 0.0.0.0 4400
    ```
    
    And my Tcl script:
    
    ```
    package require tcl::transform::observe
    
    proc writable {sock} {
        chan event $sock writable {}
        chan configure $sock -translation {crlf binary} -blocking 0 -buffering full -encoding binary
        chan event $sock readable [list readable $sock]
        set ::status "connected"
    }
    
    proc readable {sock} {
        set readCount [chan gets $sock line]
        if {$readCount <= 0} {
            if {[eof $sock]} { 
                puts "Connection aborted"
                close $sock
                set :status "closed"
                break
            }
            # we don't have a full line yet - wait for next chan event
            return
        }
        set ::msg $line
    }
    
    set sock [socket -async localhost 4400]
    chan event $sock writable [list writable $sock]
    vwait ::status
    puts "Status $status"
    tcl::transform::observe $sock stdout ""
    
    puts $sock "Hello\r\n"
    flush $sock
    
    vwait ::msg
    puts "Got $msg"
    # update ;#- avoids crash
    chan pop $sock
    
    after 100 {set ::forever 1}
    vwait forever ;# crashes here
    
    close $sock
    puts DONE
    ```
    
    This produces the following output:
    
    ```
    Status connected
    Hello
    
    Got Hello
    Segmentation fault (core dumped)
    ```
    
    The lines "hello\\r\\n" are printed by the channel observer. Then Tcl crashes after I remove the channel transformation and enter the event loop.
    
    The crash does NOT occur if I uncomment the line update ;#- avoids crash
    
    The crash does NOT occur if I don't use the channel transformation.
    
    I can reproduce the crash with 100% chance on OpenSUSE 15.4/Tcl 8.6.12 (from repos) and Windows/Tcl 8.6.13 (from Magicsplat).
    
    Thanks in advance.
    
  2. icomment:
    replace "sleep" with a plain "update" for even smaller example
    
  3. login: "petrokaz"
  4. mimetype: "text/plain"
  5. username: "Petro Kazmirchuk"