From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.bugs Subject: bug#26503: Local variables reclaimed early vs. finalizers Date: Wed, 19 Apr 2017 11:50:22 +0200 Message-ID: <87tw5krajl.fsf@gnu.org> References: <87a87islel.fsf@gnu.org> <87shl4esj8.fsf@igalia.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1492595875 4202 195.159.176.226 (19 Apr 2017 09:57:55 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 19 Apr 2017 09:57:55 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Cc: 26503@debbugs.gnu.org To: Andy Wingo Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Wed Apr 19 11:57:49 2017 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d0mMt-0000vN-Oe for guile-bugs@m.gmane.org; Wed, 19 Apr 2017 11:57:48 +0200 Original-Received: from localhost ([::1]:46780 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0mMz-0006T7-Fh for guile-bugs@m.gmane.org; Wed, 19 Apr 2017 05:57:53 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0mGP-0000xD-BJ for bug-guile@gnu.org; Wed, 19 Apr 2017 05:51:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d0mGM-0001Lm-8x for bug-guile@gnu.org; Wed, 19 Apr 2017 05:51:05 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:57365) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d0mGM-0001Ld-5e for bug-guile@gnu.org; Wed, 19 Apr 2017 05:51:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1d0mGL-0007dX-R0 for bug-guile@gnu.org; Wed, 19 Apr 2017 05:51:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Wed, 19 Apr 2017 09:51:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 26503 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 26503-submit@debbugs.gnu.org id=B26503.149259543529318 (code B ref 26503); Wed, 19 Apr 2017 09:51:01 +0000 Original-Received: (at 26503) by debbugs.gnu.org; 19 Apr 2017 09:50:35 +0000 Original-Received: from localhost ([127.0.0.1]:55564 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d0mFv-0007co-Km for submit@debbugs.gnu.org; Wed, 19 Apr 2017 05:50:35 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:35719) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d0mFu-0007cc-6t for 26503@debbugs.gnu.org; Wed, 19 Apr 2017 05:50:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d0mFm-00015M-0q for 26503@debbugs.gnu.org; Wed, 19 Apr 2017 05:50:29 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:42970) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0mFl-00015A-Te; Wed, 19 Apr 2017 05:50:25 -0400 Original-Received: from reverse-83.fdn.fr ([80.67.176.83]:37562 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1d0mFl-0006zN-83; Wed, 19 Apr 2017 05:50:25 -0400 X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 30 Germinal an 225 de la =?UTF-8?Q?R=C3=A9volution?= X-PGP-Key-ID: 0x090B11993D9AEBB5 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5 X-OS: x86_64-unknown-linux-gnu In-Reply-To: <87shl4esj8.fsf@igalia.com> (Andy Wingo's message of "Wed, 19 Apr 2017 10:00:11 +0200") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:8733 Archived-At: Andy Wingo skribis: > ludo@gnu.org (Ludovic Court=C3=A8s) writes: > >> Consider this code: >> >> (use-modules (system foreign)) >> >> (define %abort >> (dynamic-func "abort" (dynamic-link))) >> >> (let ((ptr (make-pointer 123 %abort))) >> (display "hello\n") >> (gc)) >> >> Guile is free to collect =E2=80=98ptr=E2=80=99 when =E2=80=98gc=E2=80=99= is called since it has become >> unreachable at that point; that=E2=80=99s what 2.2.0 does, as explained = in >> =E2=80=98NEWS=E2=80=99. >> >> However, there=E2=80=99s a finalizer here so collecting =E2=80=98ptr=E2= =80=99 has an observable >> side effect. This side effect makes the semantic change visible: the >> =E2=80=9Cexpected=E2=80=9D semantics would be that =E2=80=98ptr=E2=80=99= is not subject to GC while it=E2=80=99s >> in scope. > > This would indicate that the user has erroneous expectations ;-) > > Note that here since (gc) is in tail position, ptr is in fact not > protected in any way, even given this mental model, though with a single > thread it may be that the collection actually happens later in 2.0 given > that finalizers are run by asyncs. Also ptr is not protected during the > "display" either, in 2.0; in 2.0 this "let" reduces to "begin" under > peval since the ptr is not used. Indeed (in practice =E2=80=98ptr=E2=80=99 would happen to be finalized late= r, but that=E2=80=99s =E2=80=9Cout of luck=E2=80=9D.) >> (In 2.0 the finalizer is not called until =E2=80=98ptr=E2=80=99 is no lo= nger in scope.) >> >> I=E2=80=99m not sure this counts as a bug, but it=E2=80=99s certainly a = pitfall when >> working with finalizers and the FFI. >> >> Thoughts? > > For me, I don't think this is a bug. Rather the contrary, as it's more > in spirit with safe-for-space principle that a continuation should only > keep alive those values that it uses; any other data should be available > for the GC to reclaim. > > In any case, I think this manual section treats the problem adequately, > for me at least: > > https://www.gnu.org/software/guile/manual/html_node/Foreign-Object-Memo= ry-Management.html > > Would you like to add something there? Hmm, I don=E2=80=99t think so (great section, BTW). I need to chew a bit more on this, but the conclusion is probably that my expectations were incorrect, indeed. :-) Thanks, Ludo=E2=80=99.