Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 3daba3b2797c3a64cbb010dc8422e83a469c16e3
Ticket: 780533ffffffffffffffffffffffffffffffffff
[fcopy] callback sometimes fails to trigger
User & Date: ferrieux on 2008-04-01 00:14:32
Changes

  1. icomment:
    Logged In: YES 
    user_id=496139
    Originator: NO
    
    Found and fixed. This was a day-1 bug...
    
    Bug summary: doing an async [fcopy -size $size], if the input stalls (not EOF) after exactly $size bytes, the callback is not called.
    
    The problem is in the CopyData function, near the end of the
    while(toRead) loop, in the if (cmdPtr) block. Apply the following
    patch:
    
    -       if (cmdPtr) {
    +       if (cmdPtr &&(csPtr->toRead != 0)) {
               /*
                * The first time we enter this code, there won't be a channel
                * handler established yet, so do it here.
                */
    
    Explanation: this test is here to "break out" of the entire routine
    (not just the while loop) with a return TCL_OK, in async mode, after
    making sure we have a fileevent to wake us up later. The problem is
    that at this spot the remaining number of bytes to read due to [-size]
    has already been updated, and when it reaches zero, we should only
    break of the while loop in order to terminate properly (and call the
    callback).
    
    Hence the fix: just add "&&(csPtr->toRead != 0)", so that it will fall
    thru to the beginning of the loop, which does test toRead.
    
    We can also understand why it was "rare" and "random": this is because
    people often use "-size" only as a progress indicator, so the pump is
    often "re-primed" by additional data on the channel coming after the
    given -size bytes. But what about the last chunk ? I believe people
    often use a channel with a proper EOF (not a never-ending pipe like in
    Miguel's ad hoc example), which completely hides the issue since it
    fires the readable fileevent !
    
    Below is a small script showing the bug with probability one: the script blocks forever.
    Patched version completes normally after reading 3 lines from the child process.
    Test suite OK.
    
    -Alex
    
    set pi [open "|[info nameofexecutable] 2>@ stderr" r+]
    fconfigure $pi -translation binary -buffering line
    fconfigure stdout -translation binary -buffering line
    
    puts $pi {
        fconfigure stdout -translation binary -buffering line
        puts stderr Waiting...
        after 1000
        foreach x {a b c} {
    puts stderr Looping...
    puts $x
    after 500
        }
        puts stderr Now-sleeping-forever
        while {[gets stdin line]>=0} {}
        exit
    }
    
    proc done args {puts stderr DONE;exit}
    
    fcopy $pi stdout -size 6 -command done
    
    vwait forever
    
    
    
  2. login: "ferrieux"
  3. mimetype: "text/plain"
  4. username: "ferrieux"