Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 3317aff8e47aff5b5e57994a5f3594edf5fb07e9
Ticket: 2762041fffffffffffffffffffffffffffffffff
Fix (too) early recognition of EOF in a channel stack
User & Date: andreas_kupries on 2010-02-23 05:59:40
Changes

  1. icomment:
    Now I understand. Here is a trace I made with fprintf at strategic places in the Zlib and general IO code.
    
        Compressed size is 41
        IO ChanRead (4096)
        IO got 48
        IO OK
        HEADER:hahaha
        Z INFLATE 0x804cdc0
        Z PUSH  0x80681a8
        Now reading through gunzip
        IO DoReadChars c0 -1 /loop 4294967295
        IO DOReadChars Copied -1
        IO ChanRead (4096)
        Z INPUT 0x80681a8 4096
        Z GET RAW 0x80738e0 4096
        IO ChanRead (4055)
        Z         41
        Z processing 41, space for 4096
        Z 0 left 10, remaining 0 /1/0
        Z returning 4096
        IO got 4096
        IO OK
        IO DoReadChars c0 -1 /loop 4294967295
        IO DoReadChars Queued Data
        IO DOReadChars Copied 4096
        IO DoReadChars c4096 -4097 /loop 4294963199
        IO DOReadChars Copied -1
    *   IO DOReadChars EOF
        Received 4096 out of 5000
        Z CLOSE 0x80681a8
    
    The problem is shown by the *'d line. 
    
    (1) We have a base channel, and
    (2) a transformation on top
    (3) By reading from the transformation we cause it to
    (4) read from the base channel.
    (5) The base channel hits EOF in that read.
    (6) The loop in DoReadChars() sees that EOF flag and aborts,
    (7) never mind that data is in the transform which we could read.
    
    The generic IO code thinks that the EOF flag applies to the whole stack, while it actually applies only to the base channel. The flags, including EOF are managed in the ChannelState, which is shared between all channels in the stack.
    
    To truly fix this we have to manage some EOF flag on a per-channel/transform base, and look only at the flag of the topmost channel instead of the shared one.
    
    This is an issue any and all transforms can run into, causing loss of the last part of their info.
    
    A related problem which can occur in the middle of a channel is if you have, for example multiple deflated streams in the same channel, then reading data for the first stream may read ahead into data of the next, and you cannot unread this part, it is in the buffers high up and cannot be pushed back for the next stream to be processed.
    
    I believe that is the reason for TIP 230 having the 'limit' method. It can be used to limit the amount of read-ahead. In the case of limit=1 nothing is read beyond the end of a stream, so the next stream is ok, nothing to unread.
    
    This helps in our case actually as well. Because reading the Z_STREAM_END marker byte(s) does not yet trigger eof in the base channel, allowing the outer DoReadChars() loop to run fine.
    
    Thus, changing Tcl_ReadRaw(..., cd->inAllocated); in tclZLib.c to Tcl_ReadRaw(..., 1); is a quick fix for the issue. Correct behaviour at the expense of more calls to the base channels.
    
    
    
  2. login: "andreas_kupries"
  3. mimetype: "text/plain"
  4. username: "andreas_kupries"