unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* A timely question on interrupted system calls
@ 2013-07-11  6:45 mark.d.witmer
  2013-07-11 12:07 ` Ludovic Courtès
  0 siblings, 1 reply; 3+ messages in thread
From: mark.d.witmer @ 2013-07-11  6:45 UTC (permalink / raw)
  To: guile-user


I followed the thread a few days ago on @guile-dev about SCM_SYSCALL and
was grateful I hadn't run into any problems with it. But now I have!

I'm working with an event loop for my X bindings that polls a socket for
availablity using `select'. Meanwhile, I have a repl server running in
another thread. When something connects to the server, the call to
`select' get interrupted and throws a system error. In the following
code the catch expression doesn't catch the system error:

(define (file-ready? fd)
    (memq fd
          (car 
           (catch 'system-error
             (lambda () (select (list fd) '() '() 0 16667))
             (lambda args
               (if (= (system-error-errno args) EINTR)
                   '(() () ()) ;; Assume it isn't available for now
                   (apply throw args)))))))

I gathered from what I read that the error is getting handled in a
system async, which could explain why my catch expression doesn't see
it. I also tried a couple versions of `sigaction' without any luck:

(sigaction SIGINT (lambda (signum) #t) SA_RESTART)

and

(sigaction SIGINT #f SA_RESTART)

It'd be fine if it automatically retried instead of returning the empty
lists. Any ideas?

-- 
Mark Witmer



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: A timely question on interrupted system calls
  2013-07-11  6:45 A timely question on interrupted system calls mark.d.witmer
@ 2013-07-11 12:07 ` Ludovic Courtès
  2013-07-12  4:26   ` mark.d.witmer
  0 siblings, 1 reply; 3+ messages in thread
From: Ludovic Courtès @ 2013-07-11 12:07 UTC (permalink / raw)
  To: guile-user

mark.d.witmer@gmail.com skribis:

> I followed the thread a few days ago on @guile-dev about SCM_SYSCALL and
> was grateful I hadn't run into any problems with it. But now I have!

Excellent.  :-)

> I'm working with an event loop for my X bindings that polls a socket for
> availablity using `select'. Meanwhile, I have a repl server running in
> another thread. When something connects to the server, the call to
> `select' get interrupted and throws a system error. In the following
> code the catch expression doesn't catch the system error:

Could it be that it’s actually the other thread that gets EINTR?

> (define (file-ready? fd)
>     (memq fd
>           (car 
>            (catch 'system-error
>              (lambda () (select (list fd) '() '() 0 16667))
>              (lambda args
>                (if (= (system-error-errno args) EINTR)
>                    '(() () ()) ;; Assume it isn't available for now
>                    (apply throw args)))))))
>
> I gathered from what I read that the error is getting handled in a
> system async, which could explain why my catch expression doesn't see
> it.

No: single handlers are run from an async, but the ‘system-error’ for
EINTR it thrown immediately.

> I also tried a couple versions of `sigaction' without any luck:
>
> (sigaction SIGINT (lambda (signum) #t) SA_RESTART)
>
> and
>
> (sigaction SIGINT #f SA_RESTART)

You mean you still get EINTR with that?

In my experience SA_RESTART works as advertised, with the caveat that
execution of system asyncs (such as signal handlers) are delayed until
the syscall completes (see <http://bugs.gnu.org/14640>.)

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: A timely question on interrupted system calls
  2013-07-11 12:07 ` Ludovic Courtès
@ 2013-07-12  4:26   ` mark.d.witmer
  0 siblings, 0 replies; 3+ messages in thread
From: mark.d.witmer @ 2013-07-12  4:26 UTC (permalink / raw)
  To: guile-user

ludo@gnu.org (Ludovic Courtès) writes:

> mark.d.witmer@gmail.com skribis:
>
>> I followed the thread a few days ago on @guile-dev about SCM_SYSCALL and
>> was grateful I hadn't run into any problems with it. But now I have!
>
> Excellent.  :-)
>
>> I'm working with an event loop for my X bindings that polls a socket for
>> availablity using `select'. Meanwhile, I have a repl server running in
>> another thread. When something connects to the server, the call to
>> `select' get interrupted and throws a system error. In the following
>> code the catch expression doesn't catch the system error:
>
> Could it be that it’s actually the other thread that gets EINTR?

Thanks for the insights. I figured out what I was doing wrong... it was
actually a result of my not understanding dynamic states well enough.

I had some parameters in use in the main thread that I wanted to share
with the repl thread, so I figured it wouldn't hurt to do something like
this:

(define dynamic-state (current-dynamic-state))
(with-dynamic-state dynamic-state
  (make-thread
     (lambda ()
       ... start repl ...)))

But of course that was the cause of all my problems. The tag that
`throw' uses when it aborts to the catch prompt is captured as an
argument to a function stored in a fluid, so it's sensitive to
manipulations of the dynamic state like this. Basically, the system
error was throwing to a different (make-prompt-tag "catch") than the
(make-prompt-tag "catch") my catch was set up to handle. D'oh! It didn't
really have much to do with system calls or errors at all.

I think the correct way to make parameters in different threads refer to
the same object is like this:

(define my-param-value (my-param))
(make-thread
  (lambda ()
    (parameterize ((my-param my-param-value))
     ...)))

It feels a bit hackish, but sometimes I like the nested dynamic scope
behavior of parameters even when I don't want the thread sandboxing.

-- 
Mark Witmer




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-07-12  4:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-11  6:45 A timely question on interrupted system calls mark.d.witmer
2013-07-11 12:07 ` Ludovic Courtès
2013-07-12  4:26   ` mark.d.witmer

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).