unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: 30368@debbugs.gnu.org
Subject: bug#30368: ‘select’ returns non-empty sets upon EINTR or EAGAIN
Date: Tue, 06 Feb 2018 11:39:30 +0100	[thread overview]
Message-ID: <87y3k67671.fsf@gnu.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 1310 bytes --]

Hello,

As of Guile 2.2.3, the Scheme-level ‘select’ can return non-empty sets
when the C-level ‘select’ returns EINTR or EAGAIN.  The program below
illustrates this:

--8<---------------cut here---------------start------------->8---
(use-modules (ice-9 match))

(sigaction SIGINT (lambda args
                    (pk 'signal! args)))

(let ((target (getpid)))
  (match (primitive-fork)
    ((? zero?)
     (sleep 4)
     (kill target SIGINT)
     (primitive-exit 0))
    (_ #t)))

(match (select (list (current-input-port)) '() '())
  (((port) () ())
   (pk 'reading-from port)
   (read-char port))
  (lst
   (pk 'done lst)))
--8<---------------cut here---------------end--------------->8---

On 2.2.3, it prints:

--8<---------------cut here---------------start------------->8---
$ guile select.scm


;;; (signal! (2))
;;; (reading-from #<input: file /dev/pts/9>)
--8<---------------cut here---------------end--------------->8---

From there on it’s stuck in a read(0, …) call.

The attached patch fixes it by clearing the returned FD sets on
EINTR/EAGAIN.

(Besides it seems that select(2) never returns EAGAIN.)

I’m not sure how to write a test for this; the one above is
timing-sensitive, which wouldn’t be great.

Thoughts?

Ludo’.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 1467 bytes --]

diff --git a/libguile/filesys.c b/libguile/filesys.c
index e39dc4a0d..05dd2bd16 100644
--- a/libguile/filesys.c
+++ b/libguile/filesys.c
@@ -1,5 +1,5 @@
 /* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2006,
- *   2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Free Software Foundation, Inc.
+ *   2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017, 2018 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -900,10 +900,20 @@ SCM_DEFINE (scm_select, "select", 3, 2, 0,
     int rv = scm_std_select (max_fd + 1,
                              &read_set, &write_set, &except_set,
                              time_ptr);
-    /* Let EINTR / EAGAIN cause a return to the user and let them loop
-       to run any asyncs that might be pending.  */
-    if (rv < 0 && errno != EINTR && errno != EAGAIN)
-      SCM_SYSERROR;
+    if (rv < 0)
+      {
+        /* Let EINTR / EAGAIN cause a return to the user and let them
+           loop to run any asyncs that might be pending.  */
+        if (errno != EINTR && errno != EAGAIN)
+          SCM_SYSERROR;
+        else
+          {
+            /* Return empty sets.  */
+            FD_ZERO (&read_set);
+            FD_ZERO (&write_set);
+            FD_ZERO (&except_set);
+          }
+      }
   }
 
   return scm_list_3 (retrieve_select_type (&read_set, read_ports_ready, reads),

             reply	other threads:[~2018-02-06 10:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-06 10:39 Ludovic Courtès [this message]
2018-02-07 13:33 ` bug#30368: ‘select’ returns non-empty sets upon EINTR or EAGAIN Ludovic Courtès
2018-02-16 14:33 ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y3k67671.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=30368@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).