GNU bug report logs - #10581
exit-hook is undocumented and is not called from a non-interactive environment

Previous Next

Package: guile;

Reported by: Andrew Psaltis <ampsaltis <at> gmail.com>

Date: Sun, 22 Jan 2012 20:33:01 UTC

Severity: normal

Done: Andy Wingo <wingo <at> pobox.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 10581 in the body.
You can then email your comments to 10581 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-guile <at> gnu.org:
bug#10581; Package guile. (Sun, 22 Jan 2012 20:33:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Andrew Psaltis <ampsaltis <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-guile <at> gnu.org. (Sun, 22 Jan 2012 20:33:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Andrew Psaltis <ampsaltis <at> gmail.com>
To: bug-guile <at> gnu.org
Subject: exit-hook is undocumented and is not called from a non-interactive
	environment
Date: Sun, 22 Jan 2012 14:58:51 -0500
Currently using guile version 1.8.8.  I have been told that this also
affects the current upstream version as well.

My current project involves using a C library that has some
initialization and cleanup functions.

First:

The library initialization part is well-handled, but I could not find
a way to do the library cleanup part.  After doing some web searches,
I found an answer in "exit-hook", except that it was not clear from
the Guile reference manual that it existed.  It should probably be
documented a little more clearly.

Second:
When I write a scheme script that adds some function to exit-hook, it
is not invoked from a non-interactive call to guile.  I have a file
test.scm that looks like so:

(add-hook! exit-hook (lambda () (display "bye\n")))

In an interactive environment:

$ guile -l test.scm
guile> (exit)
bye
$

In a non-interactive environment:
$ guile -s test.scm
$

Nothing is printed.  As far as I can tell, exit-hook should be made
available in a non-interactive environment so that modules loading
libraries can cleanup easily.




Information forwarded to bug-guile <at> gnu.org:
bug#10581; Package guile. (Mon, 23 Jan 2012 02:16:02 GMT) Full text and rfc822 format available.

Message #8 received at 10581 <at> debbugs.gnu.org (full text, mbox):

From: Ian Price <ianprice90 <at> googlemail.com>
To: Andrew Psaltis <ampsaltis <at> gmail.com>
Cc: 10581 <at> debbugs.gnu.org
Subject: Re: bug#10581: exit-hook is undocumented and is not called from a
	non-interactive environment
Date: Mon, 23 Jan 2012 02:08:14 +0000
[Message part 1 (text/plain, inline)]
Andrew Psaltis <ampsaltis <at> gmail.com> writes:

> Second:
> When I write a scheme script that adds some function to exit-hook, it
> is not invoked from a non-interactive call to guile.  I have a file
> test.scm that looks like so:
>
> (add-hook! exit-hook (lambda () (display "bye\n")))
>
> In an interactive environment:
>
> $ guile -l test.scm
> guile> (exit)
> bye
> $
>
> In a non-interactive environment:
> $ guile -s test.scm
> $
>
> Nothing is printed.  As far as I can tell, exit-hook should be made
> available in a non-interactive environment so that modules loading
> libraries can cleanup easily.

Assuming exit-hook is supposed to do this, and I would presume it is
meant to. I have a patch which will implement this for
stable-2.0. Basically, I wrap everything in a large catch, and when
guile gets a 'quit exception it runs the exit hooks before throwing the
exception again.

As far as I can see, this covers the case where guile exits normally
either by use of quit/exit, or just by finishing the script. It does
_not_ perform the cleanup if guile quits because of an uncaught
exception, but if someone thinks this is desirable, it should be a
matter of changing the catch.

I'm not quite sure how to add a test case for this though.

-- 
Ian Price

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"

[0001-Run-exit-hook-when-scripts-finish-normally.patch (text/x-patch, inline)]
From baaf226cb91a41de66b7dc663c433c67f1c0f0e5 Mon Sep 17 00:00:00 2001
From: Ian Price <ianprice90 <at> googlemail.com>
Date: Sun, 22 Jan 2012 19:47:16 +0000
Subject: [PATCH] Run exit-hook when scripts finish normally.

* module/ice-9/command-line.scm (compile-shell-switches): Wrap the
  body of the output expression in a catch for `quit' exceptions, so
  that we can run the `exit-hook'.
---
 module/ice-9/command-line.scm |   68 +++++++++++++++++++++-------------------
 1 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/module/ice-9/command-line.scm b/module/ice-9/command-line.scm
index 8aed74e..e33fac8 100644
--- a/module/ice-9/command-line.scm
+++ b/module/ice-9/command-line.scm
@@ -385,38 +385,42 @@ If FILE begins with `-' the -s switch is mandatory.
       `(;; It would be nice not to load up (ice-9 control), but the
         ;; default-prompt-handler is nontrivial.
         (@ (ice-9 control) %)
-        (begin
-          ;; If we didn't end with a -c or a -s and didn't supply a -q, load
-          ;; the user's customization file.
-          ,@(if (and interactive? (not inhibit-user-init?))
-                '((load-user-init))
-                '())
-
-          ;; Use-specified extensions.
-          ,@(map (lambda (ext)
-                   `(set! %load-extensions (cons ,ext %load-extensions)))
-                 user-extensions)
-
-          ;; Add the user-specified load path here, so it won't be in
-          ;; effect during the loading of the user's customization file.
-          ,@(map (lambda (path)
-                   `(set! %load-path (cons ,path %load-path)))
-                 user-load-path)
-
-          ;; Put accumulated actions in their correct order.
-          ,@(reverse! out)
-
-          ;; Handle the `-e' switch, if it was specified.
-          ,@(if entry-point
-                `((,entry-point (command-line)))
-                '())
-          ,(if interactive?
-               ;; If we didn't end with a -c or a -s, start the
-               ;; repl.
-               '((@ (ice-9 top-repl) top-repl))
-               ;; Otherwise, after doing all the other actions
-               ;; prescribed by the command line, quit.
-               '(quit)))))
+        (catch 'quit
+          (lambda ()
+            ;; If we didn't end with a -c or a -s and didn't supply a -q, load
+            ;; the user's customization file.
+            ,@(if (and interactive? (not inhibit-user-init?))
+                  '((load-user-init))
+                  '())
+
+            ;; Use-specified extensions.
+            ,@(map (lambda (ext)
+                     `(set! %load-extensions (cons ,ext %load-extensions)))
+                   user-extensions)
+
+            ;; Add the user-specified load path here, so it won't be in
+            ;; effect during the loading of the user's customization file.
+            ,@(map (lambda (path)
+                     `(set! %load-path (cons ,path %load-path)))
+                   user-load-path)
+
+            ;; Put accumulated actions in their correct order.
+            ,@(reverse! out)
+
+            ;; Handle the `-e' switch, if it was specified.
+            ,@(if entry-point
+                  `((,entry-point (command-line)))
+                  '())
+            ,(if interactive?
+                 ;; If we didn't end with a -c or a -s, start the
+                 ;; repl.
+                 '((@ (ice-9 top-repl) top-repl))
+                 ;; Otherwise, after doing all the other actions
+                 ;; prescribed by the command line, quit.
+                 '(quit)))
+          (lambda args
+            (run-hook exit-hook)
+            (apply throw args)))))
 
       (if (pair? args)
           (begin
-- 
1.7.7.5


Information forwarded to bug-guile <at> gnu.org:
bug#10581; Package guile. (Sun, 05 Feb 2012 11:52:02 GMT) Full text and rfc822 format available.

Message #11 received at 10581 <at> debbugs.gnu.org (full text, mbox):

From: Andy Wingo <wingo <at> pobox.com>
To: Andrew Psaltis <ampsaltis <at> gmail.com>
Cc: 10581 <at> debbugs.gnu.org
Subject: Re: bug#10581: exit-hook is undocumented and is not called from a
	non-interactive environment
Date: Sun, 05 Feb 2012 12:50:39 +0100
Hi Andrew,

On Sun 22 Jan 2012 20:58, Andrew Psaltis <ampsaltis <at> gmail.com> writes:

> After doing some web searches, I found an answer in "exit-hook",
> except that it was not clear from the Guile reference manual that it
> existed.  It should probably be documented a little more clearly.

It's not documented at all, actually.  It was added in 1998 to
boot-9.scm with the following comment:

  ;;; This hook is run at the very end of an interactive session.
  ;;;
  (define exit-hook (make-hook))

It has not been changed since then.

> When I write a scheme script that adds some function to exit-hook, it
> is not invoked from a non-interactive call to guile.

Indeed, as the comment notes.

I think it's fair to say that this is a historical interface, and that
it probably shouldn't be changed.

Why not use atexit(), if you need to clean up the C library?  Just
wondering.

Andy
-- 
http://wingolog.org/




Information forwarded to bug-guile <at> gnu.org:
bug#10581; Package guile. (Sun, 05 Feb 2012 19:22:01 GMT) Full text and rfc822 format available.

Message #14 received at 10581 <at> debbugs.gnu.org (full text, mbox):

From: Andrew Psaltis <ampsaltis <at> gmail.com>
To: Andy Wingo <wingo <at> pobox.com>
Cc: 10581 <at> debbugs.gnu.org
Subject: Re: bug#10581: exit-hook is undocumented and is not called from a
	non-interactive environment
Date: Sun, 5 Feb 2012 14:20:13 -0500
On Sun, Feb 5, 2012 at 06:50, Andy Wingo <wingo <at> pobox.com> wrote:
> Hi Andrew,
>
> On Sun 22 Jan 2012 20:58, Andrew Psaltis <ampsaltis <at> gmail.com> writes:
>
>> After doing some web searches, I found an answer in "exit-hook",
>> except that it was not clear from the Guile reference manual that it
>> existed.  It should probably be documented a little more clearly.
>
> It's not documented at all, actually.  It was added in 1998 to
> boot-9.scm with the following comment:
>
>  ;;; This hook is run at the very end of an interactive session.
>  ;;;
>  (define exit-hook (make-hook))
>
> It has not been changed since then.
>

Ah, yes.

>> When I write a scheme script that adds some function to exit-hook, it
>> is not invoked from a non-interactive call to guile.
>
> Indeed, as the comment notes.
>
> I think it's fair to say that this is a historical interface, and that
> it probably shouldn't be changed.
>
> Why not use atexit(), if you need to clean up the C library?  Just
> wondering.
>

That would be because I forgot that it existed.  I probably should
just use that instead.

Thanks.

~Andrew




Reply sent to Andy Wingo <wingo <at> pobox.com>:
You have taken responsibility. (Fri, 06 Jul 2012 12:17:05 GMT) Full text and rfc822 format available.

Notification sent to Andrew Psaltis <ampsaltis <at> gmail.com>:
bug acknowledged by developer. (Fri, 06 Jul 2012 12:17:05 GMT) Full text and rfc822 format available.

Message #19 received at 10581-done <at> debbugs.gnu.org (full text, mbox):

From: Andy Wingo <wingo <at> pobox.com>
To: 10581-done <at> debbugs.gnu.org
Subject: Re: bug#10581: exit-hook is undocumented and is not called from a
	non-interactive environment
Date: Fri, 06 Jul 2012 13:21:41 +0200
Hi,

Closing out this bug, as it seems that the current situation shouldn't
be changed, and also that atexit sounds sufficient.  Please open a new
report if you still have issues :)

Regards,

Andy
-- 
http://wingolog.org/




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 04 Aug 2012 11:24:09 GMT) Full text and rfc822 format available.

This bug report was last modified 12 years and 323 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.