unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Olivier Dion <olivier.dion@polymtl.ca>
To: Vijay Marupudi <vijaymarupudi@gatech.edu>, guile-user@gnu.org
Subject: Re: Question about handling SIGINT properly in Guile
Date: Sat, 13 Apr 2024 12:46:04 -0400	[thread overview]
Message-ID: <874jc51a9f.fsf@laura> (raw)
In-Reply-To: <87wmp1fhhj.fsf@vijaymarupudi.com>

On Sat, 13 Apr 2024, Vijay Marupudi <vijaymarupudi@gatech.edu> wrote:
> Hello folk of guile-user,
>
> I am trying to create a simple Guile program that continues to perform a
> task until an interrupt signal is sent. However, I am unable to
> deterministically end the program when SIGINT is sent, as the
> interrupt handler does not run soon after the call to (sleep ...) ends.
>
> Try running the simple reproduction included below. Try typing ^C
> (Ctrl+C) soon after the program begins. Sometimes "WORK" is run
> once, and sometimes "WORK" is run twice. What do I need to do to make
> "WORK" run only once in this instance?

Depending where the signal is sent, you will have two WORKs.  For
example:

--8<---------------cut here---------------start------------->8---
(define quit? #f)

(sigaction SIGINT (lambda (arg)
                    (pk "SIG" arg)
                    (set! quit? #t)))

(let loop ()
  (pk "QUIT-CHECK")
  (if quit?
      (begin (format #t "Quitting!~%") (exit 0))
      (begin (pk "WORK" (do ((i 0 (+ i 1))
                             (sum 0 (+ sum i)))
                            ((= i 1000) sum)))
             (pk "SLEEP" (sleep 10))
             (kill (getpid) SIGINT)    ;; Ctrl-C emulation.
             (loop))))
--8<---------------cut here---------------end--------------->8---


> I understand that signal handlers are asynchronous (Asyncs) and aren't
> guaranteed to run immediately. However, I expect them to be run before
> the next primitive call. Am I doing something wrong?

So there is two things with signals.  First, when a process get a signal
queued, the OS only deliver the signal -- at least on linux -- when
going back to user-space.  Typically, before the process get
re-scheduled or after a system call.  So sending a signal is not
immediate.  Furthermore, Guile also queues the signals and deliver them
to threads with async marks I think.

-- 
Olivier Dion
oldiob.ca



  reply	other threads:[~2024-04-13 16:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-13 14:46 Question about handling SIGINT properly in Guile Vijay Marupudi
2024-04-13 16:46 ` Olivier Dion [this message]
2024-04-13 20:22   ` Vijay Marupudi
2024-04-14 22:50     ` Maxime Devos
2024-04-14 23:12 ` Maxime Devos
2024-04-16  0:43   ` Vijay Marupudi
2024-04-17 14:28     ` Vijay Marupudi

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=874jc51a9f.fsf@laura \
    --to=olivier.dion@polymtl.ca \
    --cc=guile-user@gnu.org \
    --cc=vijaymarupudi@gatech.edu \
    /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).