Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 6019c3828c7535900c63047903f6c166dbe474a8ce5b637d02315cc3d54866d6
Ticket: b3977d199b08e3979a8da970553d5209b3042e9c
Channel gets inserts spurious empty lines on receiving torn CR-LF
User & Date: sebres on 2022-07-21 16:47:31
Changes

  1. icomment:
    Test illustrating potentially undefined behavior (delay dependent):
    
    ```
    proc accept {so args} {
      fconfigure $so -translation binary
      puts -nonewline $so "1 line\r"
      puts -nonewline $so "\n2 li"
      flush $so
      #Now force separate packets
      puts -nonewline $so "ne\r"
      flush $so
      if {[incr ::cnt] & 1} {
        after 20 {set ::cnt $::cnt}; vwait ::cnt; # simulate short delay (and client can process events)
      } else {
        after 20; # small delay without processing events (just to avoid large output), client would receive the buffer in single chunk
      }
      puts -nonewline $so "\n3 line"
      flush $so
      close $so
    }
    set so [socket -server accept 10000]
    proc test {} {
      if {1} {
        set chan [socket 127.0.0.1 10000]
      } else {
        set chan stdin
      }
      fileevent $chan readable [list apply {chan {
        if {[gets $chan line] >= 0} {
          puts <$line>
        } elseif {[eof $chan]} {
          close $chan
          set ::done 1
        }
      }} $chan]
      fconfigure $chan -blocking 0 -buffering line
      vwait ::done
      puts "----$::cnt---"
      update
    }
    timerate {test} 100
    ```
    The result will show an extra NL by each odd attempt (due to immediate processing of every buffer) and no extra NL by each even attempt (buffers coming as 1 piece):
    ```
    <1 line>
    <2 line>
    <>
    <3 line>
    ----1---
    <1 line>
    <2 line>
    <3 line>
    ----2---
    <1 line>
    <2 line>
    <>
    <3 line>
    ----3---
    <1 line>
    <2 line>
    <3 line>
    ----4---
    <1 line>
    <2 line>
    <>
    <3 line>
    ----5---
    ```
    
  2. login: "sebres"
  3. mimetype: "text/x-markdown"