From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#67142: 29.1; with-sqlite-transaction commits on exception rather than rolling back Date: Sat, 18 Nov 2023 12:37:41 +0200 Message-ID: <83leavmjnu.fsf@gnu.org> References: <83pm09pz2n.fsf@gnu.org> <831qcoq0py.fsf@gnu.org> <87a5rc4vwy.fsf@gmail.com> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="674"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 67142@debbugs.gnu.org, visuweshm@gmail.com To: Vasilij Schneidermann Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 18 11:38:24 2023 Return-path: Envelope-to: geb-bug-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 1r4IiU-000AV2-QF for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Nov 2023 11:38:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r4IiA-000841-FF; Sat, 18 Nov 2023 05:38:02 -0500 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 1r4Ii9-00083r-Ba for bug-gnu-emacs@gnu.org; Sat, 18 Nov 2023 05:38:01 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r4Ii9-0007gR-3m for bug-gnu-emacs@gnu.org; Sat, 18 Nov 2023 05:38:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r4Ii9-00011W-Na for bug-gnu-emacs@gnu.org; Sat, 18 Nov 2023 05:38:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 18 Nov 2023 10:38:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67142 X-GNU-PR-Package: emacs Original-Received: via spool by 67142-submit@debbugs.gnu.org id=B67142.17003038753922 (code B ref 67142); Sat, 18 Nov 2023 10:38:01 +0000 Original-Received: (at 67142) by debbugs.gnu.org; 18 Nov 2023 10:37:55 +0000 Original-Received: from localhost ([127.0.0.1]:47875 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r4Ii2-00011B-Oi for submit@debbugs.gnu.org; Sat, 18 Nov 2023 05:37:55 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41608) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r4Ihz-00010y-Sl for 67142@debbugs.gnu.org; Sat, 18 Nov 2023 05:37:53 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r4Iht-0007eP-4I; Sat, 18 Nov 2023 05:37:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=Ca03YZmrn9LZqS2lkSwu4ZQSzah3zs9jT51shF03Nbc=; b=Nz12ZZV0tGKz eU4Fq3+7C6f8lJMobQsv83t0soOTp9ha0Bge2rkHkM59XEXxOQsLQvsr3+1o3AQxiIj5xVwgISho2 k79cK9LM35Ovr80gfwf4KUT6M0BmdJWxB+VtRh/UqT1Dal55Y1okTazPL/VwOo1ppoWzmApHw1TpZ jcaZGsRwATZWIbrPiDJw1ZOabg+XA2UGnh6YXIApKJIZjsUtahIv4O7fU6iCkh5dwmNg+jorgNexm 2Ah2eycDJwZoyC6HU45XG8o2gjPR/Lqj/oM7qRyEDbUqjMKNXprRUq9IsbSQeu6vHtXofdV3JG1jp 6EldDjlW/x84Q//CHPEABw==; In-Reply-To: (message from Vasilij Schneidermann on Fri, 17 Nov 2023 10:08:36 +0100) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:274550 Archived-At: > Date: Fri, 17 Nov 2023 10:08:36 +0100 > From: Vasilij Schneidermann > Cc: Eli Zaretskii , 67142@debbugs.gnu.org > > > db-var and func-var are uninterned symbols in the macro expansion but > > the variables 'result' and 'commit' marked below aren't. If the intent > > is not to expose these variables to the macro's BODY, the inner let > > should also uninterned symbols right? > > Correct, this is what I was hinting at. As annoying as it is, when > writing unhygienic macros one should use uninterned symbols to avoid > exposing additional variables to the BODY argument. > > Alternatively, the pattern of `(let ((return (...))) ... return)` can be > replaced with `(prog1 (...) ...)`. > > @Eli: The new patch looks better and closer to how the issue is solved > in the Ruby sqlite3 gem. I'm still not sure about this use of > `unwind-protect` being correct, but it does preserve the backtrace in > case of an error better than when I used `(condition-case e (...) (error > (apply #'signal e)))`. OK, how about the below? diff --git a/lisp/sqlite.el b/lisp/sqlite.el index aad0aa4..8a52573 100644 --- a/lisp/sqlite.el +++ b/lisp/sqlite.el @@ -24,19 +24,28 @@ ;;; Code: (defmacro with-sqlite-transaction (db &rest body) - "Execute BODY while holding a transaction for DB." + "Execute BODY while holding a transaction for DB. +If BODY completes normally, commit the changes and return +the value of BODY. +If BODY signals an error, or transaction commit fails, roll +back the transaction changes." (declare (indent 1) (debug (form body))) (let ((db-var (gensym)) - (func-var (gensym))) + (func-var (gensym)) + (res-var (gensym)) + (commit-var (gensym))) `(let ((,db-var ,db) - (,func-var (lambda () ,@body))) + (,func-var (lambda () ,@body)) + ,res-var ,commit-var) (if (sqlite-available-p) (unwind-protect (progn (sqlite-transaction ,db-var) - (funcall ,func-var)) - (sqlite-commit ,db-var)) - (funcall ,func-var))))) + (setq ,res-var (funcall ,func-var)) + (setq ,commit-var (sqlite-commit ,db-var)) + ,res-var) + (or ,commit-var (sqlite-rollback ,db-var)))) + (funcall ,func-var)))) (provide 'sqlite)