#1 Expect can use the wrong spawn_id

open
nobody
None
5
2002-05-11
2001-07-31
No

Under certain circumstances Expect can use the
wrong spawn_id. The circumstances are complicated,
but I think this may be masking a more general
problem. Here is an example script:

package require expect

#*******************
# Turn on the debugger
exp_internal -f debug.txt 0
#*******************

set last_device ""

#######################################################
###################
#
# This procedure writes a string to the log file, if
$LOGGER is True.
# It is safe to call this function even if the log
file is not open - so
# you don't have to enclose log statements in an 'If
$LOGGER' construct.
#
#######################################################
###################
proc log {log_str} {
}

#######################################################
###################
#
# This procedure writes variable traces to the log file
#
#######################################################
###################
proc traceproc {varname index op} {
upvar $varname value

if {$op == "r"} {
log "Reading $varname; value is '$value'"
}
}

#######################################################
###################
#
# This procedure logs into the device on the COM port.
#
#######################################################
###################
proc login {} {
# global spawn_id

#
# Flush the input buffer
#
expect *
send_slowly "\r"
expect {
timeout {error "\nDevice isn't responding\n"}
"TAU>" {}
}
}

#######################################################
###################
#
# This procedure is a wrapper for Expect's 'send'
command. 'Send' doesn't
# have any mechanism for sending slowly; this
procedure breaks the string
# up into smaller packets which it sends with a pause
after each one.
#
#######################################################
###################
proc send_slowly {send_string} {
global last_device spawn_id

# If we are sending to the same device we sent to
last time, then all is
# fine. If we have changed then we must make sure
we are logged in.
#
if {$spawn_id != $last_device} {
set last_device $spawn_id
login
}
send -- $send_string
}

set chan [open "COM1" w+]
spawn -open $chan

trace variable spawn_id rwu {traceproc}

login

I apologize for the complexity of the example,
but every line seems to be required. If you run this,
it will attempt to log in to the device on COM1 (and
of course it will fail.) If you then look at the
debug file (debug.txt) you will see that Expect is
writing to COM1 but reading from the console
(exp_user)! That won't _ever_ work correctly, and
should not occur without explicit changes of spawn_id.

The problem can be fixed several ways:

1) Turn off the "trace variable" - and that
should have _no_ affect!
2) Un-comment the "global spawn_id" statement
inside the "login" function. "Exploring Expect", page
241, says that this should not change the behaviour;
spawn_id should be found in the global scope without
the declaration.
3) In 'traceproc', don't trace reads. (It is OK
to trace writes and unsets.)
4) In 'login', comment out the "expect *"
5) In 'login', change "send_slowly \r" to "send
\r". This eliminates a recursive loop into
the 'login' proc. 'send_slowly' calls login if the
last device we sent something to was not this device.

I suspect that it is (4) that really causes the
problem - that something in Expect is not safe for
recursion. However, that is only intuition, based on
20 years of testing software. I have been seeing a
strange and intermittent bug in exactly this area of
code, where the 'write' routine is called
from 'send_slowly' (via 'send -- "\r"') to write
a '\r' to COM1, and 'write' says 'Invalid Argument'.
The bug occurs in exactly the same line every time,
always just after the program switches to another
device - but only when it feels like showing up. It
can run for 2 minutes or 20 minutes before the bug
bites. I put in the 'trace variable' code in order to
try to track the bug, and found this instead. Of
course, I could be completely wrong. It wouldn't be
the first time! :-)

I have verified this behaviour under both WinNT 4
and Win98.

After you fix this _easy_ bug drop me an email
and I will try to find you a _hard one_!

Leslie

Discussion

  • Leslie Brooks

    Leslie Brooks - 2001-07-31

    Logged In: YES
    user_id=271417

    I am running the Chaffee snap-29 version of Expect.

     
  • Andreas Kupries

    Andreas Kupries - 2001-08-24
    • assigned_to: nobody --> libes
     
  • Don Libes

    Don Libes - 2002-05-10
    • assigned_to: libes --> davygrvy
     
  • David Gravereaux

    • assigned_to: davygrvy --> nobody
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks