GNU bug report logs - #26503
Local variables reclaimed early vs. finalizers

Previous Next

Package: guile;

Reported by: ludo <at> gnu.org (Ludovic Courtès)

Date: Fri, 14 Apr 2017 21:58:02 UTC

Severity: normal

Done: Andy Wingo <wingo <at> igalia.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 26503 in the body.
You can then email your comments to 26503 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#26503; Package guile. (Fri, 14 Apr 2017 21:58:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to ludo <at> gnu.org (Ludovic Courtès):
New bug report received and forwarded. Copy sent to bug-guile <at> gnu.org. (Fri, 14 Apr 2017 21:58:02 GMT) Full text and rfc822 format available.

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

From: ludo <at> gnu.org (Ludovic Courtès)
To: bug-guile <at> gnu.org
Subject: Local variables reclaimed early vs. finalizers
Date: Fri, 14 Apr 2017 23:56:50 +0200
Hello!

Consider this code:

--8<---------------cut here---------------start------------->8---
(use-modules (system foreign))

(define %table
  (make-weak-value-hash-table))

(define %abort
  (dynamic-func "abort" (dynamic-link)))

(let ((ptr (make-pointer 123 %abort)))
  (display "hello\n")
  (gc))
--8<---------------cut here---------------end--------------->8---

Guile is free to collect ‘ptr’ when ‘gc’ is called since it has become
unreachable at that point; that’s what 2.2.0 does, as explained in
‘NEWS’.

However, there’s a finalizer here so collecting ‘ptr’ has an observable
side effect.  This side effect makes the semantic change visible: the
“expected” semantics would be that ‘ptr’ is not subject to GC while it’s
in scope.

(In 2.0 the finalizer is not called until ‘ptr’ is no longer in scope.)

I’m not sure this counts as a bug, but it’s certainly a pitfall when
working with finalizers and the FFI.

Thoughts?

Ludo’.




Information forwarded to bug-guile <at> gnu.org:
bug#26503; Package guile. (Wed, 19 Apr 2017 08:01:01 GMT) Full text and rfc822 format available.

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

From: Andy Wingo <wingo <at> pobox.com>
To: ludo <at> gnu.org (Ludovic Courtès)
Cc: 26503 <at> debbugs.gnu.org
Subject: Re: bug#26503: Local variables reclaimed early vs. finalizers
Date: Wed, 19 Apr 2017 10:00:11 +0200
ludo <at> gnu.org (Ludovic Courtès) 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 ‘ptr’ when ‘gc’ is called since it has become
> unreachable at that point; that’s what 2.2.0 does, as explained in
> ‘NEWS’.
>
> However, there’s a finalizer here so collecting ‘ptr’ has an observable
> side effect.  This side effect makes the semantic change visible: the
> “expected” semantics would be that ‘ptr’ is not subject to GC while it’s
> 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.

> (In 2.0 the finalizer is not called until ‘ptr’ is no longer in scope.)
>
> I’m not sure this counts as a bug, but it’s 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-Memory-Management.html

Would you like to add something there?

Andy




Information forwarded to bug-guile <at> gnu.org:
bug#26503; Package guile. (Wed, 19 Apr 2017 09:51:01 GMT) Full text and rfc822 format available.

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

From: ludo <at> gnu.org (Ludovic Courtès)
To: Andy Wingo <wingo <at> pobox.com>
Cc: 26503 <at> debbugs.gnu.org
Subject: Re: bug#26503: Local variables reclaimed early vs. finalizers
Date: Wed, 19 Apr 2017 11:50:22 +0200
Andy Wingo <wingo <at> pobox.com> skribis:

> ludo <at> gnu.org (Ludovic Courtès) 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 ‘ptr’ when ‘gc’ is called since it has become
>> unreachable at that point; that’s what 2.2.0 does, as explained in
>> ‘NEWS’.
>>
>> However, there’s a finalizer here so collecting ‘ptr’ has an observable
>> side effect.  This side effect makes the semantic change visible: the
>> “expected” semantics would be that ‘ptr’ is not subject to GC while it’s
>> 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 ‘ptr’ would happen to be finalized later, but that’s
“out of luck”.)

>> (In 2.0 the finalizer is not called until ‘ptr’ is no longer in scope.)
>>
>> I’m not sure this counts as a bug, but it’s 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-Memory-Management.html
>
> Would you like to add something there?

Hmm, I don’t 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’.




Reply sent to Andy Wingo <wingo <at> igalia.com>:
You have taken responsibility. (Wed, 19 Apr 2017 12:25:02 GMT) Full text and rfc822 format available.

Notification sent to ludo <at> gnu.org (Ludovic Courtès):
bug acknowledged by developer. (Wed, 19 Apr 2017 12:25:02 GMT) Full text and rfc822 format available.

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

From: Andy Wingo <wingo <at> igalia.com>
To: ludo <at> gnu.org (Ludovic Courtès)
Cc: 26503-done <at> debbugs.gnu.org
Subject: Re: bug#26503: Local variables reclaimed early vs. finalizers
Date: Wed, 19 Apr 2017 14:23:59 +0200
On Wed 19 Apr 2017 11:50, ludo <at> gnu.org (Ludovic Courtès) writes:

> I need to chew a bit more on this, but the conclusion is probably that
> my expectations were incorrect, indeed.  :-)

OK I close this bug in the meantime then :)  Feel free to reopen if
there is a thing to do!

Andy




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 18 May 2017 11:24:03 GMT) Full text and rfc822 format available.

This bug report was last modified 8 years and 120 days ago.

Previous Next


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