Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: e4218c487aa1ab029ef29fbde7329adc623b37a3faa6f73f79b1d05793d0504d
Ticket: d48728e10b820296e45fa73202e135f90952cb46
Blocking [gets] on a [chan push] transformed channel waits for 4096 bytes or EOF
User & Date: jan.nijtmans on 2026-11-22 22:59:10
Changes

  1. assignee changed to: "jan.nijtmans"
  2. closedate changed to: "2461367.4577562385"
  3. closer changed to: "jan.nijtmans"
  4. comment changed to:
    A blocking <nowiki>[gets]</nowiki> (or <nowiki>[read $ch $n]</nowiki>) on a channel with a reflected transform (<nowiki>[chan push]</nowiki>) does not return while complete lines are available. It waits until the transform has filled the whole 4096-byte input buffer or the other side closes the channel. Without the transform the same <nowiki>[gets]</nowiki> returns at once.
    
    Repro with a pipe (a socket behaves the same):
    
    <verbatim>
    proc xform {cmd handle args} {
        switch -- $cmd {
            initialize {return {initialize finalize read write}}
            finalize {}
            read - write {return [lindex $args 0]}
        }
    }
    # child writes three lines and keeps its stdout open for 3 seconds
    set f [open [list | [info nameofexecutable] << {
        puts -nonewline "one\ntwo\nthree\n"; flush stdout; after 3000
    }] r]
    chan push $f xform
    after 500
    set t0 [clock milliseconds]
    set line [gets $f]
    puts "gets -> $line after [expr {[clock milliseconds]-$t0}] ms"
    </verbatim>
    
    Actual: <tt>gets -> one after 2500 ms</tt> (returns only when the child exits).
    
    Expected: <tt>gets -> one after 0 ms</tt>, as without the <nowiki>[chan push]</nowiki>.
    
    Cause: <tt>ReflectInput()</tt> in generic/tclIORTrans.c loops "until the request is satisfied" and calls <tt>Tcl_ReadRaw()</tt> on the parent channel again after it already has data. In blocking mode that second read blocks. Only the non-blocking case returns a short read (when <tt>Tcl_InputBlocked()</tt> reports it). A plain channel driver returns what one read gives, so <tt>ReflectInput()</tt> could stop reading from the parent once it has some data.
    
    The loop dates from the TIP #230 implementation [057ba9eab5]. Reproduced with 8.6.0 through 8.6.18, 9.0.5 and main (Linux, pipes and sockets).
    
  5. icomment:
    Merged to [0d03b1f576|trunk], core-9-0-branch and core-8-6-branch
    
    Many thanks!
    
  6. login: "jan.nijtmans"
  7. mimetype: "text/x-fossil-wiki"
  8. resolution changed to: "Fixed"
  9. status changed to: "Closed"