Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2001-04-05
22:51 • Ticket [218120ffff] exec command fails with EINTR status still Closed with 4... artifact: c5af7a98bb user: nobody
08:20 • Ticket [218120ffff]: 3 changes artifact: 104f61b308 user: dgp
2000-11-29
04:18 • Ticket [218120ffff]: 4 changes artifact: 5280d1b8b0 user: bluemoon
2000-10-26
08:14 • Closed ticket [218120ffff]. artifact: c249b7fffa user: welch
00:53 • New ticket [218120ffff]. artifact: 006ba06a89 user: nobody

exec command fails with EINTR

Closed / Fixed218120ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:53:21
OriginalBugID: 1941 Bug
Version: 8.0.4
SubmitDate: '1999-04-27'
LastModified: '2000-04-26'
Severity: SER
Status: Closed
Submitter: pat
ChangedBy: hobbs
OS: Solaris
OSVersion: 2.6
Machine: Other
FixedDate: '2000-10-25'
ClosedDate: '2000-04-26'


Name:

Tethys



Extensions:

BLT, expect (although sometimes none)



CustomShell:

Applied Jan Nijtman's plus/dash patches.



Comments:

I'm almost 100% sure my patch is NOT correct. However, it appears to be

    a usable workaround for the time being. I don't know enough about Tcl

    channel handling to fix it properly.



ReproducibleScript:

Most Tcl scripts containing an "exec" command that returns data.

    

    wish -f foo.tcl

    

    Note that *some* scripts work fine. Others (seemingly at random) fail.

    An earlier posting to comp.lang.tcl pointed me to GetInput(), but the

    recommended patch (treating EINTR the same as EAGAIN / EWOULDBLOCK)

    didn't work for me.



ObservedBehavior:

An error message:

    

    error reading output from command: interrupted system call



DesiredBehavior:

The "exec" command not causing an error. The same scripts work fine with

    tcl7.6



Patch:

*** tclIO.c     Mon Apr 26 13:46:35 1999

--- tclIO.c.orig        Thu Apr 22 18:15:52 1999

***************

*** 2717,2726 ****

            } else {

                panic("Blocking channel driver did not block on input");

            }

- #ifndef TREAT_EINTR_AS_ERROR

-       } else if (result == EINTR) {

-               result = 0;

- #endif /* TREAT_EINTR_AS_ERROR */

        } else {

            Tcl_SetErrno(result);

        }

--- 2717,2722 ----



PatchFiles:

tclIO.c





We need something reproducable to go any further with this report. 
-- 04/26/2000 hobbs
welch Closed ticket [218120ffff]. [History diff]2000-10-26 08:14:48
bluemoonplain2000-11-29 04:18:53
This is the same problem as bug 118076 and they're trying a similar fix.
Essentially, Tcl is ignoring EINTR from read(). The valid response to this
interrupt is to re-enter the read() and ignore the error. The program
cannot control this action within an interrupt handler.
dgp Ticket [218120ffff]: 3 changes [History diff]2001-04-05 08:20:14
nobodyplain2001-04-05 22:51:23
Logged In: NO 

Reproducing this is difficult because it is dependent on
what the kernel is doing and what is specifically running
on a specific machine at the time. I've noticed a much
higher occurrence on hpux 10.20 machines than any others.

With that said, I don't believe its necessary to reproduce
a problem that is caused by the improper coding of unix io.
The low level unix io functions (on some platforms) can be
interrupted at any time by the system for any reason. When
this occurs the function returns an "error", but this needs
to be interpreted. The interpretation is to check errno for
EINTR. EINTR implies that the io was simply stopped to allow
interrupt handling to occur. Handling the interrupt is
actually a non-issue. But if an interrupt handler is in place,
it will occur asynchronously to the process and control will
be returned to where the io call is. At this point the io
should be restarted. No data is ever moved when it is
interrupted due to EINTR, so nothing can be lost. If no
interrupt handler is in place, the io is still halted and still
needs to be restarted.

Some of the io functions that do this are read(), write(),
fgetc(), and fputc(). Interestingly, wait's also work this
way. Take a look at the Tcl 8.3, compat/waitpid.c, and
see how the waitpid() function handles return codes
from wait() or wait3().

The patch given above may work, but isn't really proper.
The real fix should be to root out all of the low level io calls
and loop them when the call fails due to EINTR. read() (and
probably write()) seems to be the most used offender.

read() is simpler but here's an example of fgetc() which
shows the "quirkiness" of unix io.

      cp = buffer;
      for(i = 0; i<(sizeof(buffer)-1); i++) {
         /* Read chars until a true EOF/io error or a good char is read */
         while ((ch=fgetc(rfp)) == EOF) {
            if (feof(rfp)) goto eof;
            if (errno != EAGAIN && errno != EINTR) goto badio;
         }
         *cp++ = ch;
         if(ch == '\n') break;
      }
      *cp = 0;

Note the checks for fgetc(). An EOF value is returned "if an error
occurs", as described in the man page. The "error" now needs
to be interpreted. A call to feof() needs to be made on the stream
to determine if the EOF returned was indeed an actual EOF. If so,
its not an error, just time to process EOF. Now look at the next
tests for errno = EAGAIN or EINTR. If the errno is set to these,
it still is not an error and the code simply continues.

This is really how the io code in tcl should be handling these
interrupts rather than trying to propogate EINTR up to a higher
level. (Note also that EAGAIN is a possibility that is not handled
by the tcl code at all). Higher level code should never have to
be concerned with these interrupts. They can install interrupt
handlers if they need to see them.

Finally, I you don't want to take my word for it, take a look
at a unix programming book. Something like "Advanced
Programming in the UNIX Environment", by W. Richard Stevens,
Addison-Wesley Publishing Company, Copyright 1992, section
10.5, pages 275-277. Page 276 has the following text:

"...The typical code sequence (assuming a read operation and
assuming we want to restart the read even if it's interrupted)
would be

again:
   if ( (n = read(fd, buff, BUFFSIZE) ) < 0) {
      if (errno == EINTR)
         goto again;         /* just an interrupted system call */
   }

To prevent the applications from having to handle interrupted system
calls 4.2BSD introduced the automatic restarting of certain interrupted
system calls..."

"...POSIX.1 allows an implementation to restart system calls, but it is
not required.

System V has never restarted system calls by default...

... With 4.3+BSD the automatic restarting of system calls depends on
which function is called to set the signal's disposition..."

Its a little weak in the "assuming we want to restart the read" bit.
There really is no reason to not restart the read. Especially since
some vendors do and some don't. The only way to get consistency
across vendors is to restart them.
Log in (anonymous is fine) to add a comment.