Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 9a1925bb2a2b1519161aefcfab88a60d26786471
Ticket: 943274ffffffffffffffffffffffffffffffffff
Tcl 8.4.5 with Expect 5.39 hangs waiting for data matching p
User & Date: mellenbe on 2004-04-27 19:48:55
Changes

  1. assignee changed to: "andreas_kupries"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    See the thread by the same name at comp.lang.tcl for 
    more information.
    
    Running this on solaris.  We have a large testing 
    framework written in tcl/expect.  The framework 
    executes on one platform (solaris) and uses spawn to 
    start rsh on another platform.  The shell is left running 
    for the life of a test run which can last several days.
    
    Commands are sent to the rsh shell and expect is used 
    to parse the input as it is returned.  We recently 
    upgraded to more current versions of tcl 8.4.5 and 
    expect 5.39 (from tcl 7.5 and expect 5.21).
    
    What I have found is when the rsh shell returns large 
    amounts of data (approx 150,000 bytes) it is very likely 
    to produce a hang.
    
    Back trace copied from comp.lang.tcl,
    
    #0  0xff21da28 in _read () from /usr/lib/libc.so.1
    #1  0x000284c8 in ExpInputProc 
    (instanceData=0x176078,
        buf=0x1784f0 "rver\r\n137.69.53.234\tpv2-
    mv\r\n137.69.51.200\tpacket\r\n137.69.3.186
    \tmarvel\r\n137.69.12.162\tcal162\r\n137.69.9.235
    \tbutiki\r\n137.69.5.45\tableix\r\n137.69.121.160
    \tbelem\r\n137.69.101.155\tubik\r\n137.69.103.118
    \trunt\r\n13"...,
    toRead=4096, errorCodePtr=0xffbfe260) at 
    exp_chan.c:122
    #2  0x000b1148 in GetInput (chanPtr=0x173a48) at
    ./../generic/tclIO.c:5303
    #3  0x000afb98 in DoReadChars (chanPtr=0x173a48, 
    objPtr=0x164b88,
    toRead=2092,
        appendFlag=1) at ./../generic/tclIO.c:4447
    #4  0x000af92c in Tcl_ReadChars (chan=0x173a48, 
    objPtr=0x164b88,
    toRead=4000,
        appendFlag=1) at ./../generic/tclIO.c:4339
    #5  0x000363d8 in expIRead (interp=0x15ba80, 
    esPtr=0x176078,
    timeout=10,
        save_flags=0) at expect.c:1652
    #6  0x000364b0 in expRead (interp=0x15ba80, 
    esPtrs=0x172f80,
    esPtrsMax=1,
        esPtrOut=0xffbfe580, timeout=10, key=12317) at 
    expect.c:1723
    #7  0x000383d4 in Exp_ExpectObjCmd (clientData=0x0, 
    interp=0x15ba80,
    objc=13,
        objv=0xffbfe9a8) at expect.c:2526
    #8  0x00046d18 in TclEvalObjvInternal 
    (interp=0x15ba80, objc=13,
        objv=0xffbfe9a8, command=0x12d3a8 "", length=0, 
    flags=0)
        at ./../generic/tclBasic.c:3087
    #9  0x00047058 in Tcl_EvalObjv (interp=0x15ba80, 
    objc=13,
    objv=0xffbfe9a8,
        flags=0) at ./../generic/tclBasic.c:3203
    #10 0x000332a4 in exp_eval_with_one_arg 
    (clientData=0x0,
    interp=0x15ba80,
    
    (gdb) p *esPtr
    $7 = {channel = 0x173a48, name = "exp4", '\0' <repeats 
    36 times>, fdin
    = 4,
      fdout = 4, channel_orig = 0x0, fd_slave = -1, 
    validMask = 14, pid =
    29846,
      buffer = 0x164b88, msize = 4001, umsize = 2000, 
    printed = 4, echoed
    = 0,
      rm_nulls = 1, open = 1, user_waited = 0, sys_waited = 
    0, registered
    = 1,
      wait = 0, parity = 1, key = 12317, force_read = 1, 
    notified = 0,
      notifiedMask = 2, fg_armed = 0, leaveopen = 0, 
    bg_interp = 0x0,
      bg_ecount = 0, bg_status = unarmed, 
    freeWhenBgHandlerUnblocked = 0,
      fdBusy = 0, keepForever = 0, valid = 1, nextPtr = 
    0x170b50}
    (gdb) p toRead
    $8 = 4096
    (gdb) p bytesRead
    $9 = 0
    
    Our script looks like this, I added eof and full_buffer to 
    see if either would get executed.  They don't.
    
     while {1}  {
        expect {
            -i $ID -re "(\[^\r]*)(\r\r\n|\r\n)" {
            append lines "$expect_out(1,string)\n"
            continue
         }
         "$user_prompt" {
             puts "prompt"
             break
         }
         eof {
            puts "\n >>> hit eof"
         }
    
         full_buffer {
            puts "\n full buffer hit"
            exit -1
         }
      }
    
    The problem is a mix of buffering and blocking IO.  Since 
    the rsh is a long running server, we never see an EOF 
    from it.  The user_prompt is basically a logical eof we 
    use as "end of data".  
    
    What I have seen is all data, including the logical eof 
    has been buffered.  There is no more to expect from the 
    rsh.  During our last trip through expect/tcl a portion of 
    the data was returned but some data would not fit in 
    our buffer (expect_out, I think) so the remainder is left 
    in the tcl buffers.  
    
    The next time we go through expect/tcl the remainder 
    of the data is returned (including our logical eof) but 
    then the logic in DoReadChars does another read.  The 
    read is blocking and we sit there forever.
    
    There is a tricky exchange that needs to happen here.  
    I don't know if it is a problem with expect or tcl or both.
    
    In our case, when expect is reading, blocking reads 
    shouldn't be issued unless the pattern matching logic 
    has had a chance to look at the data and decided to 
    drive another read.  In this case,  the logic in 
    DoReadChars copies data to the expect buffer and then, 
    without letting expect see it, does another read. 
    
    I circumvented the hang with the following update to 
    DoReadChars.  It works well with a long running server 
    but causes some of the blocking/nonblocking tests to 
    fail.
    
    Original code from DoReadChars around line 4416,
    
          for (copied = 0; (unsigned) toRead > 0; ) {
              copiedNow = -1;
    
              if (statePtr->inQueueHead != NULL) {
                  if (encoding == NULL) {
                      copiedNow = ReadBytes(statePtr, objPtr, 
    toRead,
    &offset)
                  } else {
                      copiedNow = ReadChars(statePtr, objPtr, 
    toRead,
    &offset,
                              &factor);
                  }
              }
    
    Fixed code,
    
          for (copied = 0; (unsigned) toRead > 0; ) {
              copiedNow = -1;
              if (statePtr->inQueueHead != NULL) {
                  if (encoding == NULL) {
                      copiedNow = ReadBytes(statePtr, objPtr, 
    toRead,
    &offset);
                  } else {
                      copiedNow = ReadChars(statePtr, objPtr, 
    toRead,
    &offset,
                              &factor);
    >>> New           if (copiedNow > 0) {
    >>>                   statePtr->flags |= CHANNEL_BLOCKED;
    >>>               }
                  }
              }
    
    This basically prevents another read and allows a return 
    to user code.
     The user will know if another read is necessary.
    
    Contact me if you need more information.
    
    Mike
    
    
    
    
  4. foundin changed to: "obsolete: 8.4.5"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "mellenbe"
  9. subsystem changed to: "25. Channel System"
  10. title changed to:
    Tcl 8.4.5 with Expect 5.39 hangs waiting for data matching p
    
  11. type changed to: "Bug"