From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Olivier Dion Newsgroups: gmane.lisp.guile.user Subject: Re: Question about handling SIGINT properly in Guile Date: Sat, 13 Apr 2024 12:46:04 -0400 Message-ID: <874jc51a9f.fsf@laura> References: <87wmp1fhhj.fsf@vijaymarupudi.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17164"; mail-complaints-to="usenet@ciao.gmane.io" To: Vijay Marupudi , guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Apr 13 18:46:59 2024 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rvgWp-0004Ee-Fi for guile-user@m.gmane-mx.org; Sat, 13 Apr 2024 18:46:59 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rvgWG-0001p3-Tk; Sat, 13 Apr 2024 12:46:24 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rvgWF-0001os-BU for guile-user@gnu.org; Sat, 13 Apr 2024 12:46:23 -0400 Original-Received: from smtp.polymtl.ca ([132.207.4.11]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rvgW9-0002Bd-W1 for guile-user@gnu.org; Sat, 13 Apr 2024 12:46:22 -0400 Original-Received: from localhost ([209.209.33.179]) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 43DGk5Jd088673; Sat, 13 Apr 2024 12:46:09 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 43DGk5Jd088673 In-Reply-To: <87wmp1fhhj.fsf@vijaymarupudi.com> X-Poly-FromMTA: ([209.209.33.179]) at Sat, 13 Apr 2024 16:46:05 +0000 Received-SPF: pass client-ip=132.207.4.11; envelope-from=olivier.dion@polymtl.ca; helo=smtp.polymtl.ca X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19515 Archived-At: On Sat, 13 Apr 2024, Vijay Marupudi 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