all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: help-gnu-emacs@gnu.org
Subject: Re: unwind-protect and inhibit-quit
Date: Fri, 16 Jul 2021 23:30:31 +0200	[thread overview]
Message-ID: <8735seosjs.fsf@sperrhaken.name> (raw)
In-Reply-To: <jwv4kcul35y.fsf-monnier+emacs@gnu.org> (Stefan Monnier via Users list for the's message of "Fri, 16 Jul 2021 11:00:14 -0400")

Stefan Monnier via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

>> Now I'm curious too... Would something like this work?
>>
>>  (let ((inhibit-quit t))
>>    (setq process
>>       (let ((inhibit-quit nil))
>>          (ftp-setup-buffer host file))))
>
> No.  The problem is not in the `setq` itself but in the fact that
> a non-local exit from `ftp-setup-buffer` (e.g. because of `C-g`) will
> cause `ftp-setup-buffer` not to return the process.

But at which point is the ‘quit-flag’ actually handled?  When unwinding
the function, just before returning a value?  The macro
‘with-local-quit’ states that the “quit-flag” “will not be handled until
the next function call”[1].  Could, therefore, a careful and cooperative
“ftp-setup-buffer” ensure that the process is either cleaned-up or
returned?  I am thinking of something like the following:


    (defun ftp-setup-buffer (host file)
      (let (process)
        (condition-case nil
            (progn
              (let ((inhibit-quit t))
                ;; I am assuming that ‘start-process’ does not block for a
                ;; long time.  Does it?  Maybe with a defective drive?
                ;; How about ‘start-file-process’?
                (setq process (start-process …)))
              ;; do more stuff
              process)
          (quit
           ;; I guess another quit could happen here; nest more
           ;; ‘condition-case’?  Itʼs ‘condition-case’ all the way down.
           (and (processp process)
                (kill-process process))
           (setq quit-flag t)
           ;; Propagate quit and make sure an outside handler receives it.
           ;; I do not have a good understanding of this.  I simply copied
           ;; it from the ‘with-local-quit’ macro.
           (eval '(ignore nil))))))


    (let (process)
      (unwind-protect
          (let ((inhibit-quit t))
            (setq process
                  (with-local-quit
                    (ftp-setup-buffer host file)))
            ;; do stuff
            )
        (and (processp process) (kill-process process))))


> We should devise a more reliable API, tho I'm not completely sure what
> it should look like.  Maybe
>
>     (let ((list-of-created-processes nil))
>       (unwind-protect ...
>         (mapc #'delete-process list-of-created-processes)))
>
> Where the low-level primitives which create processes add them to
> `list-of-created-processes`.

How do the low-level C functions to create processes handle quit?  If it
happens inside of them, they should be responsible for killing the
process.  But I am understanding that there is a step in between the
creation of the process object, the return from a process creation
primitive, and the assignment of the process object to a variable.

> But then this gets into trouble when some unrelated code is run during
> `...` (e.g. via timers or whatnot) which creates unrelated processes,
> so we'd need some extra care to make sure those processes don't get
> added to "the same" `list-of-created-processes`.

If I am allowed to spitball here: create another function call to
“claim” a process, and subject unclaimed processes to a similar clean-up
routine as buffers (with ‘clean-buffer-list’):


    (let (process)
      (unwind-protect
          (progn
            (setq process (ftp-setup-buffer host file))
            (claim-process process)
            ;; do stuff
            )
        (and (processp process)
             (kill-process process))))


Add an optional CLAIMED parameter to process creation primitives that
defaults to t in order to not disturb existing code.  Naturally, I have
no idea how to implement that nor the skill to figure this out.


Footnotes:
[1]  Although there is also this information in (info "(elisp)
     Quitting"):

     “Eventually, ‘inhibit-quit’ will become ‘nil’ again, such as when
     its binding is unwound at the end of a ‘let’ form.  At that time,
     if ‘quit-flag’ is still non-‘nil’, the requested quit happens
     immediately.”

-- 
Felix Dietrich



  parent reply	other threads:[~2021-07-16 21:30 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-15 15:14 unwind-protect and inhibit-quit Felix Dietrich
2021-07-16  7:18 ` Eli Zaretskii
2021-07-16  8:10   ` Thibaut Verron
2021-07-16 11:19     ` Eli Zaretskii
2021-07-16 14:46       ` Felix Dietrich
2021-07-16 14:56         ` Felix Dietrich
2021-07-16 15:00     ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-16 20:01       ` Thibaut Verron
2021-07-16 20:06         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-16 21:30       ` Felix Dietrich [this message]
2021-07-16 21:37         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-17 11:52           ` Felix Dietrich
2021-07-17  6:20         ` Eli Zaretskii
2021-07-17 15:46           ` Felix Dietrich
2021-07-17 16:34             ` Eli Zaretskii

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

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

  git send-email \
    --in-reply-to=8735seosjs.fsf@sperrhaken.name \
    --to=felix.dietrich@sperrhaken.name \
    --cc=help-gnu-emacs@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.