| Ticket UUID: | 8419c55e2cb159f977b76cadb3cebe0353389b22 | ||
| Title: | Tclsh read loop does not handle EINTR | ||
| Type: | Bug | Created on: | 2021-02-27 04:08:26 |
| Submitter: | anonymous | Assigned to: | jan.nijtmans |
| Subsystem: | 55. Other Tools | Severity: | Important |
| Priority: | 5 Medium | Last modified: | 2021-08-09 21:33:32 |
| Status: | Closed | Closed by: | chw |
| Resolution: | Fixed | Closed on: | 2021-08-09 21:33:32 |
| Version: | 8.6.* | ||
| Description: | ||||
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;
}
| ||||
| User Comments: | ||||
jan.nijtmans added on 2021-03-09 09:23:11:
Thanks for your report, your analysis and the fix. Fully 100% OK! Fixed now in all active branches. Thanks! chw added on 2021-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 | ||||
