Artifacts Associated With Ticket 32ae34e63a
Ticket change [7f8ce56d41] (rid 142773) by dgp on 2015-08-11 20:46:57:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- 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 - foundin initialized to: "all"
- is_private initialized to: "0"
- login: "dgp"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "dgp"
- subsystem initialized to: "25. Channel System"
- title initialized to: "[chan copy] reflected channel segfault"
- type initialized to: "Bug"
Ticket change [4a53ecc6d9] (rid 142774) by dgp on 2015-08-11 20:55:30:
- 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.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [cd8de14e93] (rid 142775) by aku on 2015-08-11 21:12:52:
- 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.
- login: "aku"
- mimetype: "text/plain"
- icomment:
Ticket change [058252b27c] (rid 142793) by dgp on 2015-08-12 15:16:12:
- 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.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [ae253117b3] (rid 142800) by dgp on 2015-08-12 16:38:22:
- icomment:
New branch: http://core.tcl.tk/tcl/info/fa8fb6f35fa354e2
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [0ddd114166] (rid 142809) by dgp on 2015-08-13 16:52:35:
- 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.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [ffa3db8dd1] (rid 143141) by dgp on 2015-09-22 19:06:40:
- 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.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [a2887c20fe] (rid 143190) by dgp on 2015-09-23 17:01:48:
- assignee changed to: "dgp"
- closedate changed to: "2457289.20958266"
- closer changed to: "dgp"
- icomment:
Fix committed.
- login: "dgp"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"
Ticket change [1527f81dd6] (rid 143255) by dgp on 2015-09-25 16:32:56:
- closer changed to: "nobody"
- 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.
- login: "dgp"
- mimetype: "text/plain"
- resolution changed to: "None"
- status changed to: "Open"
Ticket change [4d0603f507] (rid 257786) by serhiy.storchaka on 2026-10-12 19:22:40:
- 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.
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- icomment: