Skip to content
Tcl Source Code History Of Ticket ea69b0258a9833cb
Login

History Of Ticket ea69b0258a9833cb

Artifacts Associated With Ticket ea69b0258a9833cb

  1. Ticket change [5b629d1669] (rid 208140) by petrokaz on 2023-01-12 15:04:29:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-markdown"
    4. comment initialized 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 sleep {delay} {
          after $delay {set ::sleepVar 1}
          vwait ::sleepVar
      }
      
      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"
      # sleep 0 ;#- avoids crash
      chan pop $sock
      
      sleep 100 ;# crashes here
      
      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 sleep 0 ;#- 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.
      
    5. foundin initialized to: "8.6.13"
    6. is_private initialized to: "0"
    7. login: "petrokaz"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "e603fff52985a053bcf4cafd5d99e14a243356ca"
    10. resolution initialized to: "None"
    11. severity initialized to: "Critical"
    12. status initialized to: "Open"
    13. submitter initialized to: "petrokaz"
    14. subsystem initialized to: "26. Channel Transforms"
    15. title initialized to:
      Crash when using a channel transformation on TCP client socket
      
    16. type initialized to: "Bug"
  2. Ticket change [af54bc6f6e] (rid 208142) by petrokaz on 2023-01-12 16:33:47:

    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"
  3. Ticket change [1377f7d150] (rid 209800) by apnadkarni on 2023-03-07 06:53:28:

    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"
  4. Add attachment "crash3.tcl" [d3b6cc9933] (rid 209812) by petrokaz on 2023-03-07 10:31:17.

  5. Ticket change [c8920c9d34] (rid 209813) by petrokaz on 2023-03-07 10:32:52:

    1. icomment:
      Hello Ashok,
      sorry for the poor bug report... I made some mistakes while trying to extract a minimal reproducible example from my original code that is much bigger.
      
      I've taken your script and changed it so that it crashes again - attached crash3.tcl
      
      So, AFAICS [chan pop] crashes if there is pending input.
      
    2. login: "petrokaz"
    3. mimetype: "text/plain"
  6. Ticket change [c7d5b89f63] (rid 209818) by oehhar on 2023-03-07 12:42:53:

    1. icomment:
      Thanks !
      What I have done:
      
         *    start wish 8.6.13 32 bit on windows
         *    take tcllib 1.21
      
      <verbatim>
      lappend auto_path {C:\test\tcllib1.21}
      </verbatim>
      
         *   Paste your script
         *   it waits some time in the line
      
      <verbatim>
      vwait ::msg
      </verbatim>
      
         *   It shows the background error
      
      <verbatim>
      error reading "sock03F5A828": socket is not connected
          while executing
      "chan gets $sock line"
          (procedure "readable" line 2)
          invoked from within
      "readable sock03F5A828"
      </verbatim>
      
      Would you have additional instructions to reach the line where it should crash ?
      
      Thank you and take care,
      Harald
      
    2. login: "oehhar"
    3. mimetype: "text/x-fossil-wiki"
    4. username: "oehhar"
  7. Ticket change [95aadbf939] (rid 209819) by petrokaz on 2023-03-07 12:48:35:

    1. icomment:
      Hello Harald,
      you need some 'echo' TCP server running on the port 4400.
      On Linux the easiest way is to run netcat as I show in the bug report. I'm not sure if netcat with the same features is available on Windows. 
      You could write it in Tcl as well.
      
    2. login: "petrokaz"
    3. mimetype: "text/x-markdown"
  8. Ticket change [87df5baed2] (rid 209826) by oehhar on 2023-03-07 13:10:45:

    1. icomment:
      Yes, I fear the reproduction recipe is "Linux only". stdout is used, what is not available with Windows wish.
      
      Maybe, some Linux foks will jump in...
      
      Take care,
      Harald
      
    2. login: "oehhar"
    3. mimetype: "text/x-fossil-wiki"
    4. username: "oehhar"
  9. Ticket change [ea95732851] (rid 209827) by apnadkarni on 2023-03-07 15:00:43:

    1. icomment:
      I've tried it on Windows using magicsplat, as well as the latest repository under debug and release modes, in a batch loop. Have not been able to get it to crash. (Using the Windows ncat from the nmap site).
      
      Output is always
      
      ```
      Status=connected
      Hello1
      Hello2
      Connection aborted
      Got closed
      pending input before pop: 0
      pending input after pop: 0
      DONE
      ```
      
      Note pending input is always 0. I just cannot get it to be non-0 which may be why my test does not crash. When I get time, I'll experiment different configs to try and get non-0 input.
      
      BTW what version of tcllib are you using? I presume same as what shipped with the 8.6.13 magicsplat distribution?
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  10. Ticket change [308c74d199] (rid 209828) by apnadkarni on 2023-03-07 15:23:37:

    1. icomment:
      Well, the good news is that I tried it on my Ubuntu WSL and it crashed right away. The bad news is I have no clue about Linux crash dumps or debuggers. So this will take some time. But at least it is reproducible on Linux with 8.6.13.
      
      /Ashok
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  11. Ticket change [006f692866] (rid 209878) by pooryorick on 2023-03-07 21:35:51:

    1. assignee changed to: "pooryorick"
    2. login: "pooryorick"
    3. mimetype: "text/plain"
  12. Ticket change [80a3895bd9] (rid 210094) by oehhar on 2023-03-13 12:45:17:

    1. icomment:
      Pooryorik,
      thanks for the fix in [052f54ddfb].
      Can you imagine, why this only fails on Linux ? It is in the gereric code...
      
      Pietro,
      can you check, if the fix is ok for you ?
      
      Take care,
      Harald
      
    2. login: "oehhar"
    3. mimetype: "text/x-fossil-wiki"
    4. username: "oehhar"
  13. Ticket change [54043e6e59] (rid 210109) by apnadkarni on 2023-03-13 16:09:05:

    1. icomment:
      Harald,
      
      Petro could reproduce on Windows as well though I could not despite many different attempts with configurations.
      
      Given it looks like an early release of memory, it could all be timing dependent for the bug to manifest itself depending on when the memory gets reused for example.
      
      Just guessing...
      
      /Ashok
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  14. Ticket change [629cb07cfe] (rid 210116) by chw on 2023-03-13 16:55:52:

    1. icomment:
      Nathan's fix in [052f54ddfb] looks plausible but IMO isn't complete yet.
      When the creation of the ChannelTimerProc timer increments the refCount
      per TclChannelPreserve() and it's expiry does the opposite with
      TclChannelRelease(), all locations where the (unexpired) timer is deleted
      must perform the TclChannelRelease(), too.
      
    2. login: "chw"
    3. mimetype: "text/plain"
  15. Ticket change [851b534171] (rid 210128) by pooryorick on 2023-03-13 21:02:19:

    1. icomment:
      Fixed for 8.6 in [a8b8ecbc039fb4e0], for 8.7 in [6d017aacac9dd19d], and for 9.0 in [62058cafe065cb35].
      
    2. login: "pooryorick"
    3. mimetype: "text/x-fossil-wiki"
    4. resolution changed to: "Fixed"
    5. status changed to: "Pending"
  16. Ticket change [7e9ffdac51] (rid 210129) by chw on 2023-03-13 21:09:16:

    1. icomment:
      Nathan, at least in core-8-6-branch there are two locations
      where Tcl_DeleteTimerHandler() is called for the timer.
      Which has incremented the refCount when created. Which does
      not get decremented when the timer is deleted. Which might
      result in the refCount never reaching zero. Which might be
      kind of a leak. Do you agree?
      
    2. login: "chw"
    3. mimetype: "text/plain"
  17. Ticket change [b8ce31539e] (rid 210130) by pooryorick on 2023-03-13 21:28:23:

    1. icomment:
      chw, I missed your previous remark today.  I see what you mean.  Looking into it now.
      
    2. login: "pooryorick"
    3. mimetype: "text/x-fossil-wiki"
  18. Ticket change [5e9fa93a3d] (rid 210190) by pooryorick on 2023-03-15 11:43:05:

    1. icomment:
      Proper cleanup added for 8.6 in [a719a1392ca50359], for 8.7 in [294e0130dcff79a3], and for 9.0 in [a76dee9eb447f3f9].
      
    2. login: "pooryorick"
    3. mimetype: "text/x-fossil-wiki"
  19. Ticket change [e4e8d32166] (rid 210191) by pooryorick on 2023-03-15 11:48:22:

    1. icomment:
      The fix includes a test that, prior to the fix, reproduced the problem on a Linux system.  I would expect the following script to reproduce the issue on Windows as well.  Could someone try it out and report the results?
      
      <code><verbatim>
      #! /usr/bin/env tclsh
      
      namespace eval rchan {
      
      	proc initialize {chan mode} {
      		namespace eval $chan {
      			variable source "line one\nline two"
      		}
      		after 0 [list ::chan postevent $chan read]
      		return {initialize finalize watch read}
      	}
      
      
      	proc finalize chan {
      		namespace delete $chan
      	}
      
      
      	proc read {chan count} {
      		namespace upvar $chan counter counter source source
      		set res [string range $source 0 $count-1]
      		set source [string range $source $count end]
      		return $res
      	}
      
      
      	proc watch {chan events} {
      		after 0 [list chan postevent $chan read]
      		return read
      	}
      
      
      	namespace ensemble create
      	namespace export *
      }
      
      
      namespace eval filter {
      	proc initialize {chan mode} {
      		return {initialize finalize read}
      	}
      	
      	proc read {chan buffer} {
      		return $buffer
      	}
      
      	namespace ensemble create
      	namespace export *
      }
      
      
      proc read1 {chan args} {
      	variable read
      	catch {
      		set res [gets $chan]
      	}
      	incr  read
      }
      
      set chan [chan create read rchan]
      chan configure $chan -blocking 0
      chan push $chan filter
      chan event $chan read [list read1 $chan]
      vwait [namespace current]::read
      chan pop $chan
      vwait [namespace current]::read
      return
      </verbatim></code>
      
    2. login: "pooryorick"
    3. mimetype: "text/x-fossil-wiki"
  20. Ticket change [2e85ab4d60] (rid 210849) by pooryorick on 2023-03-24 21:39:56:

    1. closedate changed to: "2460028.40273947"
    2. closer changed to: "pooryorick"
    3. login: "pooryorick"
    4. mimetype: "text/plain"
    5. status changed to: "Closed"