From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Felix Dietrich Newsgroups: gmane.emacs.help Subject: unwind-protect and inhibit-quit Date: Thu, 15 Jul 2021 17:14:56 +0200 Message-ID: <875yxbr4lr.fsf@sperrhaken.name> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5218"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Jul 15 19:32:37 2021 Return-path: Envelope-to: geh-help-gnu-emacs@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 1m45Dw-00019U-9x for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 15 Jul 2021 19:32:36 +0200 Original-Received: from localhost ([::1]:49444 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m45Dv-0002sM-99 for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 15 Jul 2021 13:32:35 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35388) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m434t-00072Y-RH for help-gnu-emacs@gnu.org; Thu, 15 Jul 2021 11:15:07 -0400 Original-Received: from mout.kundenserver.de ([212.227.17.10]:59065) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m434r-0002QQ-PI for help-gnu-emacs@gnu.org; Thu, 15 Jul 2021 11:15:07 -0400 Original-Received: from localhost ([77.191.6.118]) by mrelayeu.kundenserver.de (mreue108 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MYeZB-1la1ig3pa7-00Vjw7 for ; Thu, 15 Jul 2021 17:15:01 +0200 X-Provags-ID: V03:K1:HudHVYmhwaqNaTkAB5ryiPzOjBkdW90fOLIF1KPdCmXRGxzqYm1 zCiEJpbrExF77rqUXcnSsqBYNqWg7yszUsJATgzu5odobAD2kqS6bNZhGMqGpt4cgAmrwPY L492GEjsUymdad3FpvqHD96VeU++KrSRkfe/I0a61cW0+RRcOI3116Z7LeyqhqSCjfieUzJ aOJR1SUuYXdyk9UPR/Isw== X-UI-Out-Filterresults: notjunk:1;V03:K0:pLNJV4MJ0SQ=:A/CCAIwFdxV3MNzAsUucZw zF1GPADihNztdNnTu+PI51h+gZ9tC4c8bUPccTig5Of6TNSHYUrOPxV8mNH9OFjyHR+4F4jnl eXGtIbpDA5VmY8URyEQWcB2VtOUqXOww7DEFJxUb9+OcqvvOTpg/xTof2cq4bEZoIStu/opjy l5wTFI116FEqpK+Q8fD/I5zim+2TgIY9maK9r+IBBrHTGxIAFjXLTLO2Y2OB4vUt7kS1e86x+ Copicql4PHWj1idtYLNUObbCSno23JP+8icUVIdMRWldkrLCRvGDW7rmn2gU4k7zr4YpQZ0lr 7mbBvRlptLRDoW8kKyEPuY7+8wFhCN4ZjE2nQ7ZNKrqoCBdTdEHbaTvXlU+VfioXKc2fxxuTM /qIaCulHRKG6SBewZA1/T4VhgMQTDDqWv6HZsmkzZf7a5+HGjv7uEIuhT3lKr2FUMKNVKGxDS XqYOYEqtdBQq1AvjwI2TEWhHM8amv3ygkSz3qbdyF+BXBH2V4MWbhahbXhQFBROg/QSu3T0DO eLj9rFiw9dhijXtEaj9nYBNoi8dwXfBHds1W3jO2niO8XGzhRxIvb1mApIv4t7/3fOngurE1V ym9VrA96u4CP6OcZ6YSxOKuFZgwfy6mgNqo/H3oJNS5ZI5asAuXF0GBaCa83k12km/u/bdlFi ZC0cW3ZxSQC+LjyxmcD06VeaUX6JZZMm2S46qEr1r3sRAO2++wk2ZcCXwks8QA4+UBieyCJub 1E99nDlJieQjmgTL1mOMyIRSExbvrjvagR5j3TyI8svA5gDC+dOJ7dx5kQ6kqMIU+C07S8ui Received-SPF: none client-ip=212.227.17.10; envelope-from=felix.dietrich@sperrhaken.name; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 15 Jul 2021 13:32:03 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:131730 Archived-At: Hello, I have questions regarding =E2=80=9Cunwind-protect=E2=80=9D and the two exa= mples provided in the elisp reference manual [1]. ;; 1. Example (let ((buffer (get-buffer-create " *temp*"))) (with-current-buffer buffer (unwind-protect body-form (kill-buffer buffer)))) ;; 2. Example (let ((win nil)) (unwind-protect (progn (setq process (ftp-setup-buffer host file)) (if (setq win (ftp-login process host user password)) (message "Logged in") (error "Ftp login failed"))) (or win (and process (delete-process process))))) In the last paragraph [2], the description of the second example mentions a race condition between the assignment of the =E2=80=9Cftp-setup-buffer=E2= =80=9D function=CA=BCs return value to the variable =E2=80=9Cprocess=E2=80=9D and = the user sending a quit signal (C-g). Does the first example suffer from the same issue in it=CA=BCs assignment to the =E2=80=9Cbuffer=E2=80=9D variable? That paragraph also mentions that =E2=80=9Cthere is no easy way to fix this bug=E2=80=9D. Is that statement still true or can this issue be resolved by setting =E2=80=9Cinhibit-quit=E2=80=9D in current Emacs versions: ;; 2. Example (added inhibit-quit) (let ((win nil)) (unwind-protect (progn (let ((inhibit-quit t)) (setq process (ftp-setup-buffer host file))) (if (setq win (ftp-login process host user password)) (message "Logged in") (error "Ftp login failed"))) (or win (and process (delete-process process))))) The first example has the =E2=80=9Cunwind-protect=E2=80=9D form nested insi= de =E2=80=9Cwith-current-buffer=E2=80=9D; this leaves a window where the buffe= r is both created and assigned to the variable =E2=80=9Cbuffer=E2=80=9D but not prote= cted from being left alive in case of a quit (or, in more elaborated uses, an error). Shouldn=E2=80=99t it, therefore, be the other way around: =E2=80=9Cunwind-protect=E2=80=9D wrapping =E2=80=9Cwith-current-buffer=E2= =80=9D? In the following snippet I also have added =E2=80=9Cinhibit-quit=E2=80=9D, assuming that it = works like I expect. ;; 1. Example (moved unwind-protect up; added inhibit-quit) (let (buffer) (unwind-protect (progn (let ((inhibit-quit t)) (setq buffer (get-buffer-create " *temp*"))) (with-current-buffer buffer body-form)) (and buffer (kill-buffer buffer)))) There also seems to be an issue in the first example with modifying and killing a potentially already existing buffer " *temp*", which could be avoided, I believe, by using =E2=80=9Cgenerate-new-buffer=E2=80=9D: ;; 1. Example (moved unwind-protect up; added inhibit-quit; ;; use generate-new-buffer) (let (buffer) (unwind-protect (progn (let ((inhibit-quit t)) (setq buffer (generate-new-buffer " *temp*"))) (with-current-buffer buffer body-form)) (and buffer (kill-buffer buffer)))) Does =E2=80=9Cgenerate-new-buffer=E2=80=9D make the same guarantee as =E2=80=9Cget-buffer-create=E2=80=9D that it will never return nil? At least =E2=80=9Cgenerate-new-buffer=E2=80=9D is implemented with =E2=80=9Cget-buff= er-create=E2=80=9D. Footnotes: [1] =20=20=20=20=20 (info "(elisp) Cleanups") [2] =E2=80=9CThis example has a small bug: if the user types C-g to quit, = and the quit happens immediately after the function ftp-setup-buffer returns but before the variable process is set, the process will not be killed. There is no easy way to fix this bug, but at least it is very unlikely.=E2=80=9D --=20 Felix Dietrich