Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 1377f7d150a7fe2396ed4fc50fdc7220b12555d0a1f100afe1163951d00d2022
Ticket: ea69b0258a9833cb61ada42d1fc742d90aec04d0
Crash when using a channel transformation on TCP client socket
User & Date: apnadkarni on 2023-03-07 06:53:28
Changes

  1. icomment:
    Although Tcl should never crash from anything you do at the scripting level, there are several problems with your code which I presume you have fixed in your script but updated the ticket.
    
    In the `readcount` proc,
    
    - there is a `break` outside of a loop which causes the event handler to abort. I presume that should be a return.
    - the `$readcount <= 0` check should be `$readcount < 0` since getting back an empty line is perfectly kosher. In particular note your `puts Hello\r\n` is sending **two** newlines, one explicit and one because of the `\r\n`. So you will get back an empty line from the server.
    - The `set :status "closed"` line is missing a `:` - should be `::status`
    - Moreover, strictly speaking you should also set ::msg in there in case the
    server closes without sending anything back (the vwait is waiting on ::msg)
    - In case of EOF without receiving any lines, the handler closes the socket and the chan pop is attempted on a closed socket. Again this is only in the case the server closes without sending anything.
    
    In any case, I could not reproduce the crash. With your script, I simply get Tcl exceptions, as expected. With my changes (see script below), I get a print out
    
    ```
    Status connected
    Hello
    Connection aborted
    Got closed
    DONE
    ```
    
    This is irrespective of whether the update is commented or not. 
    
    My script is below, please try and see if the crash is reproduced on your system with my script:
    
    ```
    lappend auto_path d:/tcl/lib
    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
                chan event $sock readable {}
                set ::msg "closed"
                return
            }
            # 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"
    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
    
    ```
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"