Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-21
10:03 • Closed ticket [1845108fff]: FilterInputBytes/GetInput do not handle EINTR pl... artifact: 59ea27fcad user: serhiy.storchaka
2021-08-09
21:33 • Ticket [8419c55e2c] Tclsh read loop does not handle EINTR status still Close... artifact: 98db484630 user: chw
2021-03-09
09:23 • Closed ticket [8419c55e2c]. artifact: a1a2f536cf user: jan.nijtmans
09:14
Fix [8419c55e2c]: Tclsh read loop does not handle EINTR check-in: 346094351f user: jan.nijtmans tags: core-8-6-branch
08:59 • Ticket [8419c55e2c] Tclsh read loop does not handle EINTR status still Open ... artifact: 73c9a8e3b7 user: jan.nijtmans
2021-02-27
04:08 • New ticket [8419c55e2c]. artifact: c8899efe6d user: anonymous

Tclsh read loop does not handle EINTR

Closed / Fixed8419c55e2cb159f977b76cadb3cebe0353389b22Created 6 years agoUpdated 5 years ago
Descriptionplain2021-02-27 04:08:26
I'm building an extension to a large and rather opaque Linux package
(About which I won't say more).  One of the things I knew from the
documentation, but which I had not thought about is that the package
uses timers and SIGALRM handling internally to do its house keeping
and maintain its state.  I saw the warning to not use SIGALRM in
any calling program, but since I wasn't doing that, and was pretty
sure Tcl wasn't doing that, I wasn't worried.

And in fact, things work pretty well as long as I am running my
tests from scripts.  I noticed however, that if I had the package
loaded in an interactive Tclsh so I could type things at it, Tclsh
would often unexpectedly & silently exit. I finally figured out
this must be whenever a SIGLARM fires and the package calls its
handler function.

Looking at the code in tclUnixChan.c where the Tclsh terminal read
loop reads are done, I see that EINTR is not handled, even though
it is handled for pipes and sockets elsewhere.

If I make the following change, based on the code used for pipes
and sockets,  and compile a new Tclsh, it works fine.  This seems
like a bug to me -- is there some reason for not handling EINTR
here?


% diff -c tclUnixChan.c.bak tclUnixChan.c
*** tclUnixChan.c.bak   Fri Feb 12 13:59:13 2021
--- tclUnixChan.c       Fri Feb 12 13:59:13 2021
***************
*** 253,259 ****
       * nonblocking, the read will never block.
       */
  
!     bytesRead = read(fsPtr->fd, buf, (size_t) toRead);
      if (bytesRead > -1) {
        return bytesRead;
      }
--- 253,262 ----
       * nonblocking, the read will never block.
       */
  
!     do {
!       bytesRead = read(fsPtr->fd, buf, (size_t) toRead);
!     } while ((bytesRead < 0) && (errno == EINTR));
! 
      if (bytesRead > -1) {
        return bytesRead;
      }
Referenced by2
  • 59ea27fcadClosed ticket [1845108fff]: FilterInputBytes/GetInput do not handle EINTR plus 4 other changes ticket2026-10-21 10:03:37
  • 346094351fFix [8419c55e2c]: Tclsh read loop does not handle EINTR check-in2021-03-09 09:14:04
jan.nijtmans Ticket [8419c55e2c]: 3 changes [History diff]2021-03-09 08:59:44
jan.nijtmanswiki2021-03-09 09:23:11

Thanks for your report, your analysis and the fix. Fully 100% OK!

Fixed now in all active branches. Thanks!

chwplain2021-08-09 21:33:32
IMO the fix in tclUnixChan.c due to the bug report is too invasive, since it
changes the entire channel behaviour on EINTR fundamentally w.r.t. earlier
Tcl versions. The bug report mentions only the REPL of tclMain.c having a
problem, thus a less invasive measure against signals would be to only change
the REPL regarding EINTR, as can be seen in this check-in on www.androwish.org:

  https://www.androwish.org/home/info/0c961bcefd7ca47c
Log in (anonymous is fine) to add a comment.