Artifacts Associated With Ticket 2407783fffffffff
Ticket change [d229dd74bf] (rid 113481) by sbron on 2008-12-08 22:44:14:
- assignee initialized to: "hobbs"
- cmimetype initialized to: "text/plain"
- comment initialized to:
I noticed that 'seek 0' on a file handle makes the handle accessible in all interps. I can't find any evidence in the docs that this is the intended behavior. Example: The following script will dump 2 x four bytes from /dev/random: interp create foo interp create bar set f [open /dev/random rb] interp eval foo { proc bytes {fh} { binary scan [read $fh 4] H* hex puts $hex close $fh } } interp eval bar { proc bytes {fh} { binary scan [read $fh 4] H* hex puts $hex close $fh } } seek $f 0 interp eval foo [list bytes $f] interp eval bar [list bytes $f] close $f Without the seek command it reports an error as expected: can not find channel named "file5" while executing "read $fh 4" (procedure "bytes" line 2) invoked from within "bytes file5" invoked from within "interp eval foo [list byte $f]" SuSE linux 11.0 Kernel 2.6.25.18-0.2-pae Tcl 8.5.5 - foundin initialized to: "obsolete: 8.5.5"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "sbron"
- subsystem initialized to: "20. [interp]"
- title initialized to:
Seek causes IO channel to be shared between all interps
- type initialized to: "Bug"
Ticket change [dc9e3883d7] (rid 114512) by dkf on 2008-12-09 18:47:32:
- assignee changed to: "andreas_kupries"
- icomment:
Diagnosis: leakage of file handle through Tcl_Obj internal rep. Problem is not [seek] itself, but rather Tcl_GetChannelFromObj.
- login: "dkf"
- mimetype: "text/plain"
- priority changed to: "9 Immediate"
- subsystem changed to: "25. Channel System"
- username: "dkf"
Ticket change [658b72b65c] (rid 102566) by andreas_kupries on 2008-12-10 03:47:33:
- icomment:
Ok, coming to this I see the problem in 8.5, but not 8.6/Head. As I also saw a commit by dkf to tclIO.c I am assuming right now that the 'Refactoring tclIO.c' commit was (intended as) a fix for this. Meaning that I 'just' have to back port it. Is that correct, Donal ?
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [59fd343d06] (rid 118065) by hobbs on 2008-12-10 05:54:02:
- icomment:
Donal's changes do not look like they intentionally changed any behavior. I'm skeptical that TclGetChannelFromObj is the source of the issue as it layers over Tcl_GetChannel, which maintains a table in the assocData of a single interp, and uses Tcl_Objs, which should not be shared. Am I missing something?
- login: "hobbs"
- mimetype: "text/plain"
- username: "hobbs"
- icomment:
Ticket change [3e975247cd] (rid 121297) by andreas_kupries on 2008-12-10 05:55:11:
- icomment:
Note 1: There is no Tcl_GetChannelFromObj(). Only a TclGetChannelFromObj(). I.e. internal, not public. Note 2: Trying to trace where the fix is in all the unrelated changes is ... not fun. There do not seem to be any changes to fix it. Tracing the TclGetChannelFromObj a bit it seems that in 8.6 something invalidates the intrep after the seek, before the 'puts' in interp foo is reached. So far no idea what.
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [025a9fea70] (rid 119631) by andreas_kupries on 2008-12-10 05:58:06:
- icomment:
@jeffh: The intrep of tclChannelType is the statePtr (ChannelState) of a channel, without an association to an interp. This means a Tcl_Obj with vlaid intrep can carry a channel into an interp which actually doesn't know, and it will be properly used by direct access to its state, without looking it up anywhere.
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [47edeb6eb3] (rid 99794) by andreas_kupries on 2008-12-10 06:06:03:
- icomment:
Ok, I was possibly not too clear. SetChannelFromAny (SCFA) uses Tcl_GetChannel. However if and only if the intrep is not already tclChannelType. If the intrep is properly tclChannelType it pulls the ChannelState* directly out of the intrep, and now we have direct access to a channel which may not be known to the interpreter we are in, because the intrep was set in a different interpreter. So, even with a properly typed intrep we have to recheck that the channel we got is actually known in the current interp, and the same. Best would be to not only store channelstate*, but also interp* of the interp which set the intrep. The moment we are in a different interp we invalidate and recompute the intrep. sort of like cmd resolution intreps.
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [0bfc2c0ac2] (rid 114095) by andreas_kupries on 2008-12-10 06:27:30:
- icomment:
File Added - 304737: channel-intrep.patch
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Add attachment "channel-intrep.patch" [d5213a08f1] (rid 123564) by andreas_kupries on 2008-12-10 06:27:30.
Ticket change [36774aacb1] (rid 107107) by andreas_kupries on 2008-12-10 06:27:58:
- icomment:
Added patch containing my proposed fix. dkf, schelte, please review.
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [e0eb25d16f] (rid 87357) by dkf on 2008-12-10 06:28:49:
- icomment:
sounds like a reasonable fix. You also probably need to check an epoch counter or something like that so that if the channel is deleted and reallocated, that can be handled too...
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [b4d1689327] (rid 125109) by andreas_kupries on 2008-12-10 06:48:35:
- icomment:
I do not believe that an epoch is required. SCFA checks the flags of the preserved channelState for CHANNEL_CLOSED. If that is true it already invalidated and refetched the intrep. This should handle the close/reopen case.
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [584c3e731e] (rid 117774) by dkf on 2008-12-10 17:07:58:
- icomment:
If interpreter boundaries are the only concern (the "preservation within an interp" issue is a great thing to have a test case on in any case) then the patch is good. Needs a test, but that can probably be adapted from the bug report.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [bdd28214dc] (rid 101972) by dgp on 2008-12-10 21:11:15:
- icomment:
comments on the chat indicated the solution here involved having the intrep of a Tcl_Obj set a Tcl_Preserve on a struct, with a matching Tcl_Release when the intrep is shimmered away. That's fine as a stopgap, but if at all possible, the need to do that means the preserved struct really needs its own refCount field.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [80b04221cc] (rid 116912) by andreas_kupries on 2008-12-11 00:09:44:
- icomment:
@dgp: A bit of clarification. The functions of tclChannelType do indeed Tcl_Preserve/Release ChannelState pointers. This is however not because of my fix. They did that even before I put my hands into this particular pie. As such it is an integral part of this TclObjTyp I was not really willing to change for my fix. What I did was to extend this scheme to the Interp pointer I added to the intrep. It might actually not be necessary, as the Interp pointer is only copied and compared, never de-referenced, contrary to the ChannelState. I should further note that ChannelState structures actually have a refCount. This however is used to count how many interpreters have the the channel Tcl_RegisterChannel()'ed in them, and a refCount > 0 prevents fully closing the channel. I do not believe that it is a good idea to use this refCount to count how many Tcl_Obj refer to this channel. As it means that a 'close $f' will never really close the channel, but only at some arbitrary time later when the last intrep goes away. Which actually may be never during the lifetime of an interp. I will see that I can get a test done for 8.5 and 8.6 in the next days, and then check if may Preseve/Release of the Interp* was overkill. Likely yes. If so I will remove that part of the patch.
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Add attachment "tcl85.diff-wu" [2ca61dcd08] (rid 118941) by andreas_kupries on 2008-12-11 07:37:08.
Ticket change [12b0d736a8] (rid 124086) by andreas_kupries on 2008-12-11 07:37:09:
- icomment:
File Added - 304858: tcl85.diff-wu
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Add attachment "tcl86.diff-wu" [35eb151d48] (rid 107024) by andreas_kupries on 2008-12-11 07:37:52.
Ticket change [5ab3936fd7] (rid 118188) by andreas_kupries on 2008-12-11 07:37:52:
- icomment:
File Added - 304859: tcl86.diff-wu
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [94b9aacbcf] (rid 106040) by sbron on 2008-12-11 15:44:12:
- icomment:
I don't know enough of the internals of Tcl to be able to give a theoretical opinion about the patch. I have confirmed that it works in practice with the application where I found the issue. One additional remark: I found that this bug was not present in Tcl 8.5.2. It must have been introduced in Tcl 8.5.3 or 8.5.4.
- login: "sbron"
- mimetype: "text/plain"
- username: "sbron"
- icomment:
Ticket change [aa31dee268] (rid 99284) by andreas_kupries on 2008-12-12 00:31:16:
- icomment:
Applied patches to both 8.5 and head.
- login: "andreas_kupries"
- mimetype: "text/plain"
- username: "andreas_kupries"
- icomment:
Ticket change [6b312ea02c] (rid 128276) by andreas_kupries on 2008-12-12 00:31:26:
- closedate changed to: "2008-12-11T17:31:26"
- closer changed to: "andreas_kupries"
- resolution changed to: "Fixed"
- status changed to: "Closed"