Skip to content
Tcl Source Code History Of Ticket 32ae34e63a
Login

History Of Ticket 32ae34e63a

Artifacts Associated With Ticket 32ae34e63a

  1. Ticket change [7f8ce56d41] (rid 142773) by dgp on 2015-08-11 20:46:57:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      This script segfaults:
      
          proc driver {cmd args} {
              variable buffer
              variable index
              set chan [lindex $args 0]
              switch -- $cmd {
                  initialize {
                      set index($chan) 0
                      set buffer($chan) [encoding convertto utf-8 \
                              [string repeat a 100]]
                      return {initialize finalize watch read}
                  }
                  finalize {
                      unset index($chan) buffer($chan)
                      return
                  }
                  watch {
      		if {"read" in [lindex $args 1]} {
      		    chan postevent $chan read
      		}
      		return
                  }
                  read {
                      set n [lindex $args 1]
                      set new [expr {$index($chan) + $n}]
                      set result [string range $buffer($chan) $index($chan) $new-1]
                      set index($chan) $new
                      return $result
                  }
              }
          }
          proc noop args {}
          proc more {c outChan bytes args} {
              if {[eof $c]} {
                  set ::done eof
                  catch {close $c}
                  return
              }
              if {[llength $args]} {
                  set ::done error
              } else  {
                  chan copy $c $outChan -command noop
              }
          }
          set c [chan create read [namespace which driver]]
          chan configure $c -encoding utf-8
          set outChan [open out w]
          chan configure $outChan -encoding iso8859-1
          after 100 {set ::done timeout}
          chan copy $c $outChan -size 99 -command [list [namespace which more] $c $outChan]
          vwait ::done
          puts $::done
          close $outChan
          file delete out
          rename driver {}
          rename more {}
          unset ::done
      
    5. foundin initialized to: "all"
    6. is_private initialized to: "0"
    7. login: "dgp"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "dgp"
    13. subsystem initialized to: "25. Channel System"
    14. title initialized to: "[chan copy] reflected channel segfault"
    15. type initialized to: "Bug"
  2. Ticket change [4a53ecc6d9] (rid 142774) by dgp on 2015-08-11 20:55:30:

    1. icomment:
      
      The problem here is that [driver watch] makes a direct call to
      [chan postevent] which directly calls Tcl_NotifyChannel.  This
      chain of events makes it possible for the internal routine
      CopyData() to recursively call itself.
      
      Problem is that CopyData() has not been written to support
      a recursive call to itself.  It's written to support a strict
      assumption that all calls to it come from an event handler
      that has been scheduled by other instances of CopyData().
      Specifically each call to CopyData passes in a pointer to
      a CopyState struct and management of the lifetime of
      that struct doesn't account for any recursive operations,
      only event chained operations.
      If a recursive call does get made -- as in this demo script --
      the inner CopyData() can free the CopyState struct and then
      leave the outer CopyData pointing to and using freed memory,
      which is the case here.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  3. Ticket change [cd8de14e93] (rid 142775) by aku on 2015-08-11 21:12:52:

    1. icomment:
      Q&A from the chat
      
      [13:52]	aku	Q's: Would this be fixable by preventing calls to 'NotifyChannel' from a 'WatchProc()' ? Is that check even do-able ? Is that the right fix ? What other fixes are possible ?
      [13:55]	dgp	I think the possible fix to to have [chan postevent] just do exactly that.  That's mostly a guess at this point.
      [14:02]	dgp	the other fix would be to either refcount or Preserve the CopyState struct.
      
    2. login: "aku"
    3. mimetype: "text/plain"
  4. Ticket change [058252b27c] (rid 142793) by dgp on 2015-08-12 15:16:12:

    1. icomment:
      
      It appears the problem may be even worse.  Without any encounter
      with CopyData() at all, if a [driver watch] directly calls a
      [chan postevent] we can have recursive (effectively tail) calls
      to DoReadChars().  It appears that this means the inner read
      starts after, but ends before the outer read, which appears to
      mean that the order of bytes read from a channel can get shuffled.
      That is, the later bytes in the channel come back from a read 
      operation and can get processed prior to the completion of the
      outer read which brings back earlier bytes in the channel.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  5. Ticket change [ae253117b3] (rid 142800) by dgp on 2015-08-12 16:38:22:

    1. icomment:
      
      New branch:
      
      http://core.tcl.tk/tcl/info/fa8fb6f35fa354e2
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  6. Ticket change [0ddd114166] (rid 142809) by dgp on 2015-08-13 16:52:35:

    1. icomment:
      
      Latest commit to the branch has a new test demonstrating
      another variation of trouble with the [driver watch]
      interface.
      
      The ReflectWatch routine checks whether there's any
      change in the interest mask from what's already in place,
      and if not, it bails without calling [driver watch].
      
      This demands an interface, then, where a single call to
      [driver watch] sets up some continuing sequence of
      [chan postevent] calls whenever conditions on the reflected
      channel warrant.  Since [driver watch] has to return too,
      this pretty much insists on some recurring event handler
      scheme, and that leaves no(?) choice but timer events via
      [after].
      
      Seems like it would be simpler to have all ReflectWatch
      calls get reflected in a [driver watch] evaluation for
      a file-events-only driven mechanism of continued production
      of events.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  7. Ticket change [ffa3db8dd1] (rid 143141) by dgp on 2015-09-22 19:06:40:

    1. icomment:
      
      I'm going to restart on this one.  Changing the fundamental
      behavior of [chan postevent] is potentially disruptive, no
      matter how much I would like it to be the other way.
      
      Also, Tcl_NotifyChannel() is public so even changing
      [chan postevent] won't stop some extension from 
      bringing about the same problem.
      
      The alternative fix I'll now be pursuing is making
      CopyData() robust against the possibility that it
      might get called recursively.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  8. Ticket change [a2887c20fe] (rid 143190) by dgp on 2015-09-23 17:01:48:

    1. assignee changed to: "dgp"
    2. closedate changed to: "2457289.20958266"
    3. closer changed to: "dgp"
    4. icomment:
      
      Fix committed.
      
    5. login: "dgp"
    6. mimetype: "text/plain"
    7. resolution changed to: "Fixed"
    8. status changed to: "Closed"
  9. Ticket change [1527f81dd6] (rid 143255) by dgp on 2015-09-25 16:32:56:

    1. closer changed to: "nobody"
    2. icomment:
      
      I'm backing out the fix.  Putting the bug back in place.
      
      The attempt to fix has given rise to many more failures.
      Attempts to change CopyData() to behave robustly against
      recursive calls are making it much more fragile in
      normal operations.  If this bug is to be fixed, it's
      going to have to be done with much more care, and within
      an arena much better guarded by tests to expose missteps.
      
    3. login: "dgp"
    4. mimetype: "text/plain"
    5. resolution changed to: "None"
    6. status changed to: "Open"
  10. Ticket change [4d0603f507] (rid 257786) by serhiy.storchaka on 2026-10-12 19:22:40:

    1. icomment:
      The segfault was fixed by the fix for [79474c5880] (8.6.15): the CopyState is
      now reference counted. The script crashes with 8.6.14 and works with 8.6.15,
      9.0 and 9.1. It now ends with "timeout", because the driver posts only one event
      from <code>watch</code>, and <code>watch</code> is only called again when the
      interest changes.
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
    4. resolution changed to: "Fixed"
    5. status changed to: "Closed"